For this type of task. You have to set first FLAG_LAYOUT_NO_LIMITS
flag in your activity or fragment as,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
Now, in your XML just set a view
at top of layout like,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/status_bar_view"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="24dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="22dp">
//main layout here.
</RelativeLayout>
</LinearLayout>
So, the top view takes the place of statusbar in layout and you can set background of view as you want. In above view
, I just used black color you can also set a gradient as a background.
EDIT
But also we have to set view
dynamically because height of statusbar is different on each device according to screen size. So, statusbar height you calculate as,
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
Above method returns the statusbar height for each device. You can set this as a height for view
. So your view, looks exactly like statusbar in size.