Share Coding

Tutorials, Problems, Stuffs …

Category Archives: BahBahBah

Android status bar background color

To change status bar background color, setup the following in your v21/style.xml, inside your style.
Yes, they are only working after API 21.

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:statusBarColor">@color/bgStatusBar</item>

 

For API level > 19
In older version, API level 19, cannot change background color on status bar.
But you can setup a ImageView behide it, and make the status bar full transprant.
Write down the following in your v19/style.xml, inside your style.

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>

In your Activity, add those code at onCreate()

// Lower then API level 19, do nothing
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    return;
}
// Change the background image / color of the st
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);

    if (contentView.getChildCount() > 1) {
        contentView.removeViewAt(1);
    }

    int res = resources.getIdentifier("status_bar_height", "dimen", "android");
    int height = 0;
    if (res != 0)
        height = resources.getDimensionPixelSize(res);

    ImageView image = new ImageView(activity);
    image.setLayoutParams(
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height)
    );
    image.setImageResource(imageRes);
    image.setScaleType(ImageView.ScaleType.FIT_XY);

    contentView.addView(image);
}

 
Moreover, it is suggested to keep the fitsSystemWindows at style or the root layout.
To use adjustResize function to solve keyboard overlap edittext problem, it must be kept.

<item name="android:fitsSystemWindows">true</item>

Android keyboard overlap with the content

Write this on your activity, done.

android:windowSoftInputMode = "adjustResize"

If not working, your root element at xml must contain the following attribute.

android:fitSysytemWindow="true"

Div has equal width and space

Example: three or two span share the equal space in a row. (inline-block)

space

Read more of this post

Drawable localization

test

 

Open a folder with name ‘drawable-en’ when the local setting is ‘en’.
My default language is ‘zh’, so the image with chines word store at ‘drawable’ folder.
At the same time, we should have ‘values’ and ‘values-en’ folders.

Read more of this post

Open PDF file at Inent

// Check if file download state = complete && file exists
if (property.isTutorDLComplete() && tutorFile.exists()) {
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(tutorFile), "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    } catch (ActivityNotFoundException activityNotFoundException) {
        // Error occur when there are no pdf reader
        activityNotFoundException.printStackTrace();
        // Ask for download PDF Reader
        showSimpleConfirmDialog(R.string.ask_dl_pdfreader, ACTION_DL_PDF_READER);
    }
} else {
    showSimpleAlertDialog(R.string.error_tutor);
}
// After confirm dialog, this function will be executed
private void goToDLPDFReader(){
    // Download adobe reader
    String url = "https://play.google.com/store/apps/details?id=com.adobe.reader";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(i);
}