Thursday, December 5, 2019

Facebook Login in Android

Integrate the Facebook SDK

The Facebook Login SDK for Android is a component of the Facebook SDK for Android. To use the Facebook Login SDK in your project, make it a dependency in Maven, or download it. Choose the method you prefer with the following button.
  1. Go to Android Studio | New Project | Minimum SDK
  2. Select "API 15: Android 4.0.3" or higher and create your new project.
  3. In your project, open your_app | Gradle Scripts | build.gradle
  4. Add the Maven Central Repository to build.gradle before dependencies:
    repositories { mavenCentral() }
  5. Add compile 'com.facebook.android:facebook-android-sdk:[4,5)' to your build.gradle dependencies.

Using Maven

  1. In your project, open your_app > Gradle Scripts > build.gradle (Project) make sure the following repository is listed in the buildscript { repositories {}}:
    jcenter() 
  2. In your project, open your_app > Gradle Scripts > build.gradle (Module: app) and add the following implementation statement to the dependencies{} section to depend on the latest version of the Facebook Login SDK:
     implementation 'com.facebook.android:facebook-login:[5,6)'
  3. Build your project.

string.xml
       <string name="facebook_app_id">XXXXXXXXXXXXX</string>
       <string name="fb_login_protocol_scheme">fb22XXXXXX60580447282</string>
menifest.xml
//permission
<uses-permission android:name="android.permission.INTERNET"/>
//applicaltion tag
<activity android:name="com.facebook.FacebookActivity"
    android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name" />
<activity
    android:name="com.facebook.CustomTabActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="@string/fb_login_protocol_scheme" />
    </intent-filter>
</activity>

Initialization of CallBackManager And Button.
    CallbackManager  callbackManager = CallbackManager.Factory.create();
    Button loginButton = (Button) findViewById(R.id.facebook);
    loginButton.setOnClickListener(this);
And use this facebookIntegration() method in your onClick method.
  private void facebookIntegration() {
    
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));
    //  LoginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    
                    Bundle facebookBundle = getFacebookData(loginResult.getAccessToken());
                    
                }
                
                @Override
                public void onCancel() {
                    //  Log.d("sam", "cancel");
                    // App code
                }
                
                @Override
                public void onError(FacebookException exception) {
                    // Log.d("sam", new Gson().toJson(exception));
                    // App code
                }
            });
}

private Bundle getFacebookData(final AccessToken token) {
    final Bundle facebookBundle = new Bundle();
    facebookBundle.putString("fields", "id,name,email");
    
    GraphRequest request = GraphRequest.newMeRequest(token,
            new GraphRequest.GraphJSONObjectCallback() {
                
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    String name = "", email = "", id = "";
                    try {
                        name = object.getString("name");
                    } catch (JSONException e) {
                        name = "";
                        e.printStackTrace();
                        
                    }
                    
                    try {
                        email = object.getString("email");
                    } catch (JSONException e) {
                        email = "";
                        e.printStackTrace();
                        
                    }
                    
                    try {
                        id = object.getString("id");
                    } catch (JSONException e) {
                        id = "";
                        e.printStackTrace();
                        
                    }
                    
                    Log.d("sam", name + " " + email + " "+ id);
                    
                    Log.d("sam", "Object = " + object.toString());
                    
                }
            });
    request.setParameters(facebookBundle);
    request.executeAsync();
    
    
    return facebookBundle;
 }
And use your callbackManager.onActivityResult() method of your activity or fragment onActivityResult method.
@Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
     callbackManager.onActivityResult(requestCode, resultCode, data);
     super.onActivityResult(requestCode, resultCode, data);
  }
Note: Please Config your manifest and Gradle before using this code.

That is all. If any help related to this post please comment.

Thank you, guys. 

Enjoy coding.

Edit Text circular in Android

Here are XML // style works on 28 API onwards
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="@android:color/white">

    <!-- Mobile number -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/bg_rounded_input_field" />

        <TextView
            android:id="@+id/text_dummy_hint_mobile_number"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="28dp"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:text="Mobile Number"
            android:textSize="16sp"
            android:background="@android:color/white"
            android:visibility="invisible"/>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:hint="Mobile Number"
            android:textColorHint="@android:color/black"
            app:hintTextAppearance="@style/HintTextStyle">

            <EditText
                android:id="@+id/edit_mobile_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:maxLines="1"
                android:backgroundTint="@android:color/transparent"/>
        </android.support.design.widget.TextInputLayout>
    </RelativeLayout>

    <!-- Promo code -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">


        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:hint="Promo Code"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:textColorHint="@android:color/black">

            <EditText
                android:id="@+id/edit_promo_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:maxLines="1"/>
        </android.support.design.widget.TextInputLayout>
    </RelativeLayout>
</LinearLayout>
Here is a fragment.
public class PractiseFragment extends Fragment {
    
    
    public PractiseFragment() {
        // Required empty public constructor
    }
    
    TextView textDummyHintMobileNumber;
    EditText editMobileNumber;
    EditText editPromoCode;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_practise, container, false);
    
        textDummyHintMobileNumber = (TextView) view.findViewById(R.id.text_dummy_hint_mobile_number);
      //  textDummyHintPromoCode = (TextView) view.findViewById(R.id.text_dummy_hint_promo_code);
        editMobileNumber = (EditText) view.findViewById(R.id.edit_mobile_number);
        editPromoCode = (EditText) view.findViewById(R.id.edit_promo_code);
    
        // Mobile number
        editMobileNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
            
                if (hasFocus) {
                    new Handler().postDelayed(new Runnable() {
                    
                        @Override
                        public void run() {
                            // Show white background behind floating label
                            textDummyHintMobileNumber.setVisibility(View.VISIBLE);
                        }
                    }, 100);
                } else {
                    // Required to show/hide white background behind floating label during focus change
                    if (editMobileNumber.getText().length() > 0)
                        textDummyHintMobileNumber.setVisibility(View.VISIBLE);
                    else
                        textDummyHintMobileNumber.setVisibility(View.INVISIBLE);
                }
            }
        });
    
        // Promo code
        editPromoCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
            
                if (hasFocus) {
                    new Handler().postDelayed(new Runnable() {
                    
                        @Override
                        public void run() {
                            // Show white background behind floating label
                           // textDummyHintPromoCode.setVisibility(View.VISIBLE);
                        }
                    }, 100);
                } else {
                    // Required to show/hide white background behind floating label during focus change
                   /* if (editPromoCode.getText().length() > 0)
                        textDummyHintPromoCode.setVisibility(View.VISIBLE);
                    else
                        textDummyHintPromoCode.setVisibility(View.INVISIBLE);*/
                }
            }
        });
    
        return view;
    }
}
Below code for custom
Here is a workaround:
1. Design your layout structure as below:
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="@android:color/white">

    <!-- Mobile number -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/bg_rounded_input_field" />

        <TextView
            android:id="@+id/text_dummy_hint_mobile_number"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="28dp"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:text="Mobile Number"
            android:textSize="16sp"
            android:background="@android:color/white"
            android:visibility="invisible"/>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:hint="Mobile Number"
            android:textColorHint="@android:color/black"
            app:hintTextAppearance="@style/HintTextStyle">

            <EditText
                android:id="@+id/edit_mobile_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:maxLines="1"
                android:backgroundTint="@android:color/transparent"/>
        </android.support.design.widget.TextInputLayout>
    </RelativeLayout>

    <!-- Promo code -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/bg_rounded_input_field" />

        <TextView
            android:id="@+id/text_dummy_hint_promo_code"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="28dp"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:text="Promo Code"
            android:textSize="16sp"
            android:background="@android:color/white"
            android:visibility="invisible"/>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:hint="Promo Code"
            android:textColorHint="@android:color/black"
            app:hintTextAppearance="@style/HintTextStyle">

            <EditText
                android:id="@+id/edit_promo_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:maxLines="1"
                android:backgroundTint="@android:color/transparent"/>
        </android.support.design.widget.TextInputLayout>
    </RelativeLayout>
</LinearLayout>
2. Use below drawable bg_rounded_input_field.xml for rounded corners.
res/drawable/bg_rounded_input_field.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:color="@android:color/black"
        android:width="2dp">
    </stroke>

    <corners
        android:radius="1000dp">
    </corners>

</shape>
3. Use below HintTextStyle to TextInputLayout by adding attribute app:hintTextAppearance="@style/HintTextStyle".
res/values/styles.xml
<style name="HintTextStyle" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">16sp</item>
</style>
4. Finally, in your Activity just show/hide the text_dummy_hint_mobile_number and text_dummy_hint_promo_code during focus change.
FYI, I have used Handler with delay 100 millis to show the dummy hints TextView to sync with TextInputLayout hint text animation.
TestActivity.java
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends AppCompatActivity {

    TextView textDummyHintMobileNumber;
    TextView textDummyHintPromoCode;
    EditText editMobileNumber;
    EditText editPromoCode;

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

        textDummyHintMobileNumber = (TextView) findViewById(R.id.text_dummy_hint_mobile_number);
        textDummyHintPromoCode = (TextView) findViewById(R.id.text_dummy_hint_promo_code);
        editMobileNumber = (EditText) findViewById(R.id.edit_mobile_number);
        editPromoCode = (EditText) findViewById(R.id.edit_promo_code);

        // Mobile number
        editMobileNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                if (hasFocus) {
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // Show white background behind floating label
                            textDummyHintMobileNumber.setVisibility(View.VISIBLE);
                        }
                    }, 100);
                } else {
                    // Required to show/hide white background behind floating label during focus change
                    if (editMobileNumber.getText().length() > 0)
                        textDummyHintMobileNumber.setVisibility(View.VISIBLE);
                    else
                        textDummyHintMobileNumber.setVisibility(View.INVISIBLE);
                }
            }
        });

        // Promo code
        editPromoCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                if (hasFocus) {
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // Show white background behind floating label
                            textDummyHintPromoCode.setVisibility(View.VISIBLE);
                        }
                    }, 100);
                } else {
                    // Required to show/hide white background behind floating label during focus change
                    if (editPromoCode.getText().length() > 0)
                        textDummyHintPromoCode.setVisibility(View.VISIBLE);
                    else
                        textDummyHintPromoCode.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
}
OUTPUT:
enter image description here
That is all. If any help related to this post please comment.

Thank you, guys. 

Enjoy coding.

Stop Watch in Android with Chronometer

Divide the time into the minute , hour and second using setOnChronometerTickListener.
use this ......
Chronometer chrono  = (Chronometer) findViewById(R.id.chronomete);
chrono.setOnChronometerTickListener(new OnChronometerTickListener(){
        @Override
            public void onChronometerTick(Chronometer chronometer) {
            long time = SystemClock.elapsedRealtime() - chronometer.getBase();
            int h   = (int)(time /3600000);
            int m = (int)(time - h*3600000)/60000;
            int s= (int)(time - h*3600000- m*60000)/1000 ;
            String t = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
            chronometer.setText(t);
        }
    });
    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.setText("00:00:00");
output
EDIT
For Start
Declare globally a long variable timeWhenStopped . It is to maintain time.
private long timeWhenStopped = 0;
Start Listener... get the 'timeWhenStopped' and start from there.
 start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chrono.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
            chrono.start();
        }
    }); 
Stop Listener.... store the time in timeWhenStopped and stop.
stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timeWhenStopped = chrono.getBase() - SystemClock.elapsedRealtime();
            chrono.stop();

        }
    });
That is all. If any help related to this post please comment.
Thank you, guys.
Enjoy coding.