Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: status bar

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>
Advertisements

Detect navigation bar, or get it’s height in Android

Problem: To solve different device display problem. (fitSystemWindows)

Some android device has a virtual navigation barlayout_structure_system_android1When I want the application ActionBar or content fitSystemWindows=”true”, the content shown under the Status Bar and the Navigation Bar.

It is not a solution to add padding bottom to solve this.

As the title, I try to detect if the navigation bar exist, then add padding bottom. But the detection is not reliable.

  1. Detect if has bar key
    boolean hasSoftKey = ViewConfiguration.get(context).hasPermanentMenuKey();
  2. Detect if has at least back key and home keyboolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
  3.  Get navigation bar height:The following coding get the default navigation bar height of the device. Many device have it, even they don’t have N Bar.
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    return resources.getDimensionPixelSize(resourceId);
}
return 0;     

 

Solution: Add bottom margin when I slip the UI under the Status Bar

/* Status Bar */
protected void setStatusBar(Activity activity, int imageRes) {
    // Lower then API level 19, do nothing
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return;
    }
    // Do if API level >= 21 (Android 4.4)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
    } else {
        // This is the Android 4.4 Wear for the android watch
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    // Change the background image / color of the status bar
    ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
    rootView.setFitsSystemWindows(true);
    rootView.setClipToPadding(true);

    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
    if (contentView.getChildCount() > 1) {
        contentView.removeViewAt(1);
    }

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

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

    contentView.addView(image);
}

 

Limitation: Webview cannot set padding on it, only margin works.

Show / Enable the status bar after adding a UIImagePickerController

The status bar will disappear after adding a uiimagepickercontroller to view. 
To show/enable the status bar normally on screen, 
we need to add the code to call the status bar:

- (void) viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [[UIApplication sharedApplication] setStatusBarHidden:NO animated:animated];
}

Hide the iPhone Status Bar on XCode 4

1. Project -> Target -> Info -> Right Click – Add row
Hide Status Bar 1

Read more of this post