Custom view on status bar - android

Facebook Messenger shows this view for a few seconds when resuming the app (it also hides the status bar icons). Does anyone know how to do this?

You will need to use the WindowInsets (SDK >= 20) to get the StatusBar's height. Create a FrameLayout, apply the LayoutParams(MATCH_PARENT, statusBarHeight). Set the layout background color to colorPrimaryDark.
Hide de Status Bar by using the method setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN).
To add the status text, create a LinearLayout, apply the LayoutParams(WRAP_CONTENT, MATCH_PARENT, Gravity.CENTER_HORIZONTAL), Orientation = HORIZONTAL and add a Canvas and TextView.
Finally, add the LinearLayout as the child of the FrameLayout and add the FrameLayout as the first child of the main layout.

Related

Only keep shadow at the bottom of the view

have this layout -
<Stepsview
android:background="#color/white"
android:elevation="#dimen/margin_16"/>
this cast shadow at the bottom as well as top :
and what i need is :
Steps view and ActionBar are 2 different views, one way I see is to combine these 2 views inside a view group and set elevation on the view group only.
Is there any other way only controlling steps view.
Achieved it by dynamically adding the steps to the top abbbar ( container for toolbar ) and setting the elevation to the app bar itself.

How to change all layouts background color in android

I am new in android. I want change my all relative layout background color on selection of navigation drawer item (change Background) at runtime.
You just need to get a reference from your relative layout and call the
method setBackgroundColor()
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.id);
layout.setBackgroundColor(getResources().getColor(android color or your color));

fitSystemWindows programmatically for status bar transparency

My app has one activity that hosts different fragments for each section. I have recently made the status bar translucent by setting fitSystemWindows to true, which has set it to the background colour of the app. This is fine for fragments that have a toolbar, where the colours match, like so:
However one of my fragments has a photo and a translucent toolbar, so I'd like to have the photo occupy the space of the status bar too, rather than the background colour.
I believe the solution is to set fitSystemWindows to false for that fragment only, and manually add padding to the translucent toolbar. Doing this programmatically seems to have no effect, what could I be doing wrong?
Here is my main activity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<!-- Container for various fragment layouts, including nav drawer and toolbar -->
</RelativeLayout>
And from within my fragment's onCreateView():
RelativeLayout daddyLayout = (RelativeLayout)getActivity().findViewById(R.id.main_parent_view);
daddyLayout.setFitsSystemWindows(false);
daddyLayout.invalidate();
This seems to have no effect, like so:
If I set fitSystemWindows to false in the main_parent_view, the status bar padding is gone and it works but obviously affects every fragment.
Well, you are in dilemma situation there, because from one hand you need to apply insets (because Toolbar should be correctly padded), and on the other hand you should not apply insets (because you want ImageView to be drawn under status bar).
Turns out there's a nice API provided by the framework for that case:
ViewCompat.setOnApplyWindowInsetsListener(toolbar, (v, insets) -> {
((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin =
insets.getSystemWindowInsetTop();
return insets.consumeSystemWindowInsets();
});
Assuming your root layout has android:fitsSystemWindows="true", now appropriate insets would be applied to your Toolbar only, and not the ImageView.
But, there's a problem.
The problem is that your root layout is RelativeLayout, which doesn't dispatch its children any information about insets. Neither do its sibling layouts (LinearLayout, FrameLayout).
If you had as a root layout one of "materialish" layouts (CoordinatorLayout, DrawerLayout), then children would be dispatched those window insets.
The other option is to subclass RelativeLayout and dispatch WindowInsets to
children manually.
#TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
#Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
int childCount = getChildCount();
for (int index = 0; index < childCount; index++)
getChildAt(index).dispatchApplyWindowInsets(insets); // let children know about WindowInsets
return insets;
}
You can see this answer for a detailed explanation with precisely same requirement you have.
I have resolve this question in 4.4
if(test){
Log.d(TAG, "fit true ");
relativeLayout.setFitsSystemWindows(true);
relativeLayout.requestFitSystemWindows();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}else {
Log.d(TAG, "fit false");
relativeLayout.setFitsSystemWindows(false);
relativeLayout.requestFitSystemWindows();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
You can use a CoordinatorLayout as your activity root view and then setFitsSystemWindows(boolean) will work.
This is because, as explained in this blog post , DrawerLayout and CoordinatorLayout both have different rules on how fitsSystemWindows applies to them - they both use it to inset their child Views, but also call dispatchApplyWindowInsets() on each child, allowing them access to the fitsSystemWindows="true" property.
This is a difference from the default behavior with layouts such as FrameLayout where when you use fitsSystemWindows="true" is consumes all insets, blindly applying padding without informing any child views (that's the 'depth first' part of the blog post).
Saw the same issue. Solved it in my app by removing fitSystemWindows from the activity declaration and adding paddingTop to the fragment. Obviously not an ideal solution but seems to be working.
simply put
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
and for status bar color use:
getActivity().getWindow().setBackgroundDrawableResource(R.color.green);

Views default background - when should you write #null?

I was wondering which views have background #null by default and on which views it's recommended to add it in order to reduce the number of overlays? For example in the following construct:
relativelayout
relativelayout
framelayout
view
If I want a white background (where only one view in the xml has this background - no overlaying of white on white), and I only define 'background white' for the highest relativelayout - will all the other layouts have a background "layer" or not (if none is specified)?

List header/section color becomes dark while scrolling

I am using the this code to have section/headers in list.
http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/
I am setting the color with 50% alpha to list header and my window background color is transparent. So while scrolling header color becomes dark. Any idea how to overcome this.
and i have also set android:cacheColorHint="#00000000".
I had the same problem. I set the background color of the header view to the same background colour as the list's parent and it worked.
So I had a LinearLayout with a background colour of 'grey'.
The list is contained within this layout.
The list had android:cacheColorHint="#00000000" set in the XML and no background colour set.
The header view is loaded using LayoutInflater, inside my activity and set before the call to the list's setListAdapter.
The parent View (or main View) of the list header was also a LinearLayout with a background colour of 'grey'.
Hope this works for you.

Categories

Resources