Posts

Showing posts from November, 2015

Open app in Play Store

Write below code when you want to open any app programmatically,  String appPackageName = getPackageName(); // getPackageName() from Context or Activity object appPackageName = "com.whatsapp"; try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); } Here, appPackageName = "com.whatsapp" is static. If you comment that line, your current app open in play store.

Android Date formatter and Time formatter

Date formatter and Time formatter are the ways to convert your date to various date formats, like: 03/01/2015 to 03/01/2015, Tue 03/01/2015 to 03/01/2015, Tuesday 03/01/2015 to 03 Jan, 2015 16:30:00 to 04:30 PM 04:30:00 to 04.30 AM and many more.. SimpleDateFormat class is used to change your format of date and time. Lets look at example,  String currentDate = "03/01/2015"; String inPattern = "dd/MM/yyyy"; String outPattern = "dd/MM/yyyy, EEE"; SimpleDateFormat input = new SimpleDateFormat(inPattern); SimpleDateFormat output = new SimpleDateFormat(outPattern); Date date = null; String convertedDate = ""; try { date = input.parse(currentDate); convertedDate = output.format(date); } catch (ParseException e) { e.printStackTrace(); } output: convertedDate = 03/01/2015, Tue Explanation: currentDate is the original date. inPattern in the dateformat of currentDate DateFormat outPattern in the dateformat of your convert

Shared Preference or Session Management (Login-Logout)

Image
Android provides many ways to store data in application. One of those ways is to use of Shared Preference . Shared Preference allows you to save and retrieve data in the form of key-value pair. For getting shared preference data of your app, you have to call a method getSharedPreference() , that will return SharedPreference instance that contains key-value pairs of preference. SharedPreferences preferences = getSharedPreferences("login", Context.MODE_PRIVATE); Remember this name "login" remains same for fetching all data related to login task and also its key: Suppose, we put "isUserLogin" boolean true after login success, then first time app launch, in Launcher Screen we have to check same key "isUserLogin". if (preferences.contains("isUserLogin")) { Intent intent = new Intent(SplashActivity.this, HomeActivity.class); startActivity(intent); } else { Intent intent = new Intent(SplashActivity.this, LoginActivity.class);