Showing posts from February, 2016

How to find default sms messaging app

From api level 19 onwards, its just a one line call to get the packageName of default sms application.
Telephony.Sms.getDefaultSmsPackage(context) Before API level 19, there is no concept of Default Sms, so what we can do is get the list of messaging apps.
private Object[] getMessagingAppPackageNames(Context context) { List messagingAppPackgeNameList = new ArrayList<>(); final PackageManager pm = context.getPackageManager(); //get metaData for installed apps List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo appInfo : packages) { String packageName = appInfo.packageName; if (packageName != null && (packageName.contains("sms") || packageName.contains("mms") || packageName.contains("message") || packageName.contains("SMS") || packageName.contains("MMS") || packageName.contains("Message") || packageName…

Dsiplay Multiple Notifications in status bar

For showing multiple notifications use unique identifier while creating notification which means use unique id in notify() method.

eg: notificationManager.notify(tag, id, notification);
Here, id should be unique.

Also, in PendingIntent "REQUEST_CODE" should be unique. use the same code as notificationId.

PendingIntent pIntent = PendingIntent.getService(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); Here also, id should be unique, use the same id itself. When clicking on notification, if you want to launch some component, to "intent" pass the desired arguments. For cancel, use both tag and id in the activity or service to cancel. First pass the tag and id to the service and then cancel using BOTH. This is very important

How to get a Color from hexadecimal Color String

To get the color int from hexadecimal string use the parseColor() methood from class

eg: int color = Color.parseColor("#FFFF0000");

DialogFragment - Where to use OnDismissListener and OnCancelListener

If you want to use OnCancelListener or OnDismissListener on dialog fragment, use it in onStart() rather than on onCreateDialog();

Snackbar and FloatingButton Behavior

Snackbar is just an extension of Toast. It is an actionable Toast. Snackbar will stay on the screen forever or for a short duration (Snackbar.LENGTH_INDEFINITE, SnackBar.LENGTH_LONG, Snackbar.LENGTH_SHORT) and we can set action as well. One example that is coming to my mind is, from a list user deleted an item, then we can give user the option to "Undo" using Snackbar.

Now we will come to the implementation part.
Snackbar snackbar = Snackbar.make(recyclerView, "Do you want to undo", Snackbar.LENGTH_LONG).setAction("Undo", snackBarClickListener); snackbar.setActionTextColor(Color.WHITE); View snackbarView = snackbar.getView(); snackbarView.setBackgroundColor(Color.DKGRAY); snackbar.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {      @Override public void onViewAttachedToWindow(View v) { // write your code here } @Override public void onViewDetachedFromWindow(View v) { // write your co…