Skip to content

Instantly share code, notes, and snippets.

@sandeepyohans
Created April 8, 2019 07:17
Show Gist options
  • Save sandeepyohans/6d947ac061b4d71874f0c4c846d6dba1 to your computer and use it in GitHub Desktop.
Save sandeepyohans/6d947ac061b4d71874f0c4c846d6dba1 to your computer and use it in GitHub Desktop.
Read Version number from Gradle build file
/**
* Called when an item in the navigation menu is selected.
*
* @param item The selected item
* @return true to display the item as the selected item
*/
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.nav_version: // App Version
String version = "";
try {
PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
version = "version: " + pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
AlertDialog.Builder versionDialogBuilder = new AlertDialog.Builder(this);
versionDialogBuilder.setTitle(getString(R.string.app_name));
versionDialogBuilder.setMessage(version);
versionDialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
});
AlertDialog versionDialog = versionDialogBuilder.create();
versionDialog.show();
break;
case R.id.nav_logout: // Logout
/* Get confirmation from user before logout*/
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(getString(R.string.sign_out));
alertDialogBuilder.setMessage("Are you sure you want to logout?");
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
});
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
/* Remove the saved email ID and password*/
SharedPreferences sp = getSharedPreferences(Utils.SP_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor Ed = sp.edit();
Ed.putString("Email", "");
Ed.putString("Passwd", "");
Ed.apply();
/* Relaunch the Welcome Activity and ask user to Login */
startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
break;
}
DrawerLayout drawer = findViewById(R.id.drawer_layout_main);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment