Share Coding

Tutorials, Problems, Stuffs …

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 overlapping EditText box

This method only work after API level >= 21.
Add the following attribute to your activity. Every thing will be fine!

android:windowSoftInputMode="adjustResize"

Add the following into your root Layout at the xml file

android:fitsSystemWindows="true"

 

We should use AndroidBug5497Workaround.assistActivity to help us in
API level >= 19 && < 21, it can resize the windows automatically.
Add these code in your activity.

// Solve keyboard overlap textView problem after 4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
        && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    AndroidBug5497Workaround.assistActivity(this);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // ADJUST_RESIZE can solve most of the device
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

Add the following into your root Layout at the xml file.

android:fitsSystemWindows="true"
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity(Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        mChildOfContent = ((FrameLayout) activity.findViewById(android.R.id.content)).getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return r.bottom;
    }

}

Besides, we need to add this style in your v19 and v21/style.xml
Add style in every dialog, don’t use default dialog.

<style name="CustomDialogTheme" parent="@style/Base.Theme.AppCompat.Light.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:fitsSystemWindows">true</item>

    <!-- <item name="android:colorBackgroundCacheHint">#00000000</item> -->
    <item name="android:background">#00000000</item>

</style>

Android keyboard overlay the content

Write this on your activity, done.

Android:windowSoftInputMode = "adjustResize"

jQuery scroll to top not working

var $container = $('html, body, .divProductList');	

$scrollToTop.on('click',function(){
    
    $container.animate({
        scrollTop : 0
    }, 300);
    return false;
});

The magic is in the $container selector. Put your list, html, body into the target.

Error “Copying file obj\Debug\build.force…” and Warning “Source file ‘obj\Debug\\TemporaryGeneratedFile…” in VS2013

Error 5 Copying file obj\Debug\build.force to obj\Debug\Package\PackageTmp\obj\Debug\build.force failed. Could not find file 'obj\Debug\build.force'.
Warning	3 Source file 'obj\Debug\\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs' specified multiple times	
Warning	4 Source file 'obj\Debug\\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs' specified multiple times	
Warning	2 Source file 'obj\Debug\\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs' specified multiple times

I encountered above messages in VS2013. I tried to clean solution, rebuild project and also run VS as administrator, but those doesn’t work for me.

My solution is open xxxxxx.csproj with a notepad and delete the lines from csproj file:

<Content Include="obj\Debug\build.force" />
<Compile Include="obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs" />
<Compile Include="obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs" />
<Compile Include="obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs" />