Wednesday 18 January 2017

Key Hash for Facebook Login implementation in Android

Key Hash for Facebook Login implementation in Android

For implementing Facebook login in android app, you need this keyhash value. For seeing this blog you will generate the keyhash value programatically.

Create a new project in android studio.


Next, select target device


After that select activity


And then click finish button.



Now, Open your MainActivity.java file and write this code.

PackageInfo info;

try {
    info = getPackageManager().getPackageInfo("com.you.name", PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String something = new String(Base64.encode(md.digest(), 0));
        //String something = new String(Base64.encodeBytes(md.digest()));
        Log.e("hash key", something);
    }
} catch (NameNotFoundException e1) {
    Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
    Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
    Log.e("exception", e.toString());
}



Full Code:

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filed_activity);

        // Add code to print out the key hash
        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "Package name",  //Replace your package name here
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {

        }
   }

}


Then, run the project you will able to see the keyhash value in your android monitor(Log).


For youtube video see the below link

https://www.youtube.com/watch?v=PHddSXeBfk8






















Sunday 1 January 2017

Share action for Android Application

Share action for Android Application

In this blog, you can learn how to add share button on app. If user clicks in share button many items will available there to share like email, Gmail, Facebook, twitter  and so on.


Create a new android project Share Button. Open your app main XML layout file and add a button or text view with onClick attribute. I have added a TextView with onClick attribute. Following is the complete content of XML layout file.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ddd"
        android:clickable="true"
        android:gravity="center"
        android:onClick="shareText"
        android:padding="18dp"
        android:text="Click to open sharing option" />
</LinearLayout>

Now open your java activity file and If you want to share something when user click the button you need to add little bit code in android button onClick like the below. Following is the complete code of java activity file.

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void shareText(View view) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
        intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your default message");
        startActivity(Intent.createChooser(intent, "Choose sharing method"));
    }
}


Full Code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ddd"
        android:clickable="true"
        android:gravity="center"
        android:onClick="shareText"
        android:padding="18dp"
        android:text="Click to open sharing option" />
</LinearLayout>


MainActivity.xml

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void shareText(View view) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
        intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your default message");
        startActivity(Intent.createChooser(intent, "Choose sharing method"));
    }
}