Skip to main content

Posts

Showing posts from February, 2019

Android : Confirmation Alert Dialog with Yes and No Button

Call this method to show confirmation alert dialog. public static void alertC onfirmation( final Activity context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.drawable.ic_lock_off); builder.setTitle( "Exit" ); builder.setMessage( "Are you sure?" ) .setCancelable( false ) .setPositiveButton( "Yes" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //TODO your yes action } }) .setNegativeButton( "No" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //TODO your no action } }); AlertDialog alert = builder.create(); alert.show(); }

Android : Custom Toast Message

public static void showCustomToast(Context context, String message, int duration) { Toast t = new Toast(context); LinearLayout view = new LinearLayout(context); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER; view.setLayoutParams(params); view.setBackgroundResource(R.drawable.popup); LayoutParams txtparam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f ); txtparam.gravity = Gravity.CENTER; TextView txt = new TextView(context); txt.setTextAppearance(context, android.R.attr.textAppearanceLarge); txt.setPadding( 20 , 0 , 20 , 0 ); txt.setTextColor(Color.WHITE); txt.setGravity(Gravity.CENTER); txt.setLayoutParams(txtparam); txt.setText(message); view.addView(txt); t.setView(view); t.setGravity(Gravity.CENTER, 0 , 0 ); t.setDuration(duration); t.show...

Android : How to check app is in foreground or at background

1. First add the GET_TASKS Manifest.permission to your AndroidManifest.xml 2. Use the below method to check if the app is in background or foreground 3. This method will return true if the app is in foreground. JAVA : protected Boolean isAppAtForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context. ACTIVITY_SERVICE ); List<activitymanager .runningtaskinfo= "" > tasks = activityManager.getRunningTasks(Integer. MAX_VALUE ); for (ActivityManager.RunningTaskInfo task : tasks) { if (context.getPackageName().equals(tasks.get( 0 ).topActivity.getPackageName())) return true ; } return false ; } KOTLIN : protected fun isAppAtForeground(context: Context): Boolean? { val activityManager = context.getSystemService(Context. ACTIVITY_SERVICE ) as ActivityManager val tasks = activityManager.getRunningTasks(Integer. MAX_VALUE ) for (task in tasks) { if (...