Friday, November 21, 2014

Android: Custom toast example (different text size, different background color)


toast.getView().setBackgroundColor() will change the background color.
toast.getView().findViewById(android.R.id.message).setTextColor() will change the text color.

    private void makeCustomToast(String message) {
        Toast toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        toast.getView().setBackgroundColor(mContext.getResources().getColor(R.color.blue));
        v.setTextColor(mContext.getResources().getColor(R.color.white));
        toast.show();
    }

Android: Get app version programmatically example


Get version name & version code

String sVersionName;
int iVersionCode;
try {
    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    sVersionName = pInfo.versionName;
    iVersionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
    sVersionName = "Unknown";
    iVersionCode = 1;
}

Version name & version code is from what you have defined in AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example1"
    android:versionCode="14"
    android:versionName="1.2.2">