Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: background

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>

Make Background Image Fitting to the UIView

1.             Too Small                                    Too Large                                What we want
Too SmallToo LargeFit

2. To make a centered and stretched image:

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];