Prevent Snackbar from closing on Android

Snackbar is now part of the new Android Support Design library. you can use LENGTH_INDEFINITE as duration if you want to show it indefinitely. . You should drop the third party library you are using for it

Old answer

final SnackBar tmp = new SnackBar(ActSplash.this,
      "Do you want change color of this button to red?",
      "yes", 
       new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ButtonFlat btn = (ButtonFlat) findViewById(R.id.buttonSnackBar);
             //btn.setTextColor(Color.RED);
        }
});
tmp.setIndeterminate(true);
tmp.show();

References
https://stackoverflow.com/questions/29921663/how-to-prevent-snackbar-from-closing

Color animation using ValueAnimator on Android

int colorFrom = getResources().getColor(R.color.red);
int colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(250); // milliseconds
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        textView.setBackgroundColor((int) animator.getAnimatedValue());
    }

});
colorAnimation.start();

References
https://stackoverflow.com/questions/2614545/animate-change-of-view-background-color-on-android

Prevent services of Android app from being killed using Intent.ACTION_TIME_TICK

// register intent time tick receiver which ticks every minute
registerReceiver(timeTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    /**
     * restart service every minute if it's killed
     */
    private BroadcastReceiver timeTickReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                boolean isServiceRunning = false;

                if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {

                    ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);

                    if (manager != null) {
                        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                            if ("net.pupli.monitoring.MyMqttService".equals(service.service.getClassName())) {
                                isServiceRunning = true;
                            }
                        }
                    }

                    if (!isServiceRunning) {
                        Intent i = new Intent(context, MyMqttService.class);
                        context.startService(i);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };

References
https://llin233.github.io/2015/11/16/How-to-prevent-service/
https://developer.android.com/reference/android/content/Intent#action_time_tick
https://medium.com/google-developers/who-lives-and-who-dies-process-priorities-on-android-cb151f39044f
http://sourabhsoni.com/how-to-use-intent-action_time_tick/

Android Notifications

 public void createNotification(View view) {
        // Prepare intent which is triggered if the
        // notification is selected
        Intent intent = new Intent(this, NotificationReceiverActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Subject")
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pIntent)
                .setTicker("Subject")
                .addAction(R.drawable.icon, "Call", pIntent)
                .addAction(R.drawable.icon, "More", pIntent)
                .addAction(R.drawable.icon, "And more", pIntent).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

    }

or start foreground service

startForeground(101,notification);

References
http://www.vogella.com/tutorials/AndroidNotifications/article.html
https://questdot.com/android-foreground-service/

Keep the screen awake on Android

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

or

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>

References
https://developer.android.com/training/scheduling/wakelock

Updating the UI from a Timer using Handler on Android

private Handler handler = new Handler();
handler.postDelayed(runnable, 100);
private Runnable runnable = new Runnable() {
   @Override
   public void run() {
      /* do what you need to do */
      foobar();
      /* and here comes the "trick" */
      handler.postDelayed(this, 100);
   }
};

If you want it to stop, you can just call handler.removeCallback(runnable) and it won’t start again, until you tell it to

References
http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/

Right To Left SnackBar on Android

final Snackbar snackbar = Snackbar.make(loginMainLayout, "نام کاربری یا کلمه عبور نادرست است", Snackbar.LENGTH_INDEFINITE);
//TextView snackbarTextView = (TextView)snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
//snackbarTextView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

snackbar.getView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
snackbar.getView().setTextDirection(View.TEXT_DIRECTION_RTL);

References
https://stackoverflow.com/questions/35251791/right-to-left-snackbar

Integrating Google Sign-In into Android App

In your sign-in activity’s onCreate method

// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .build();

// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

Start the sign-in flow

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.sign_in_button:
            signIn();
            break;
        // ...
    }
}
private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);

        // Signed in successfully, show authenticated UI.
        updateUI(account);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
        updateUI(null);
    }
}

References
https://developers.google.com/identity/sign-in/android/sign-in

Android Facebook SDK get Email, Date of Birth and gender of User

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                   JSONObject object,
                   GraphResponse response) {
                // Application code
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();

References
https://developers.facebook.com/docs/android/graph
https://stackoverflow.com/questions/29295987/android-facebook-4-0-sdk-how-to-get-email-date-of-birth-and-gender-of-user