Android transparent status bar and actionbar -


i've done few researches on topic , couldn't find complete solution so, step step , trial , error, find out how can achieve these results: having transparent or coloured actionbar , statusbar. see answer bellow.

i'm developing app needs similar in devices >= api14 when comes actionbar , statusbar customization. i've found solution , since took bit of time i'll share save of yours. start using appcompat-21 dependency.

transparent actionbar:

values/styles.xml:

<style name="apptheme" parent="theme.appcompat.light"> ... </style>  <style name="apptheme.actionbar.transparent" parent="apptheme">     <item name="android:windowcontentoverlay">@null</item>     <item name="windowactionbaroverlay">true</item>     <item name="colorprimary">@android:color/transparent</item> </style>  <style name="apptheme.actionbar" parent="apptheme">     <item name="windowactionbaroverlay">false</item>     <item name="colorprimary">@color/default_yellow</item> </style> 


values-v21/styles.xml:

<style name="apptheme" parent="theme.appcompat.light">     ... </style>  <style name="apptheme.actionbar.transparent" parent="apptheme">     <item name="colorprimary">@android:color/transparent</item> </style>  <style name="apptheme.actionbar" parent="apptheme">     <item name="colorprimarydark">@color/bg_colorprimarydark</item>     <item name="colorprimary">@color/default_yellow</item> </style> 

now can use these themes in androidmanifest.xml specify activities have transparent or colored actionbar:

    <activity             android:name=".mytransparentactionbaractivity"             android:theme="@style/apptheme.actionbar.transparent"/>      <activity             android:name=".mycoloredactionbaractivity"             android:theme="@style/apptheme.actionbar"/> 

enter image description here enter image description here enter image description here enter image description here enter image description here

note: in api>=21 actionbar transparent need statusbar transparent too, otherwise not respect colour styles , stay light-grey.



transparent statusbar (only works api>=19):
1 it's pretty simple use following code:

protected void setstatusbartranslucent(boolean maketranslucent) {         if (maketranslucent) {             getwindow().addflags(windowmanager.layoutparams.flag_translucent_status);         } else {             getwindow().clearflags(windowmanager.layoutparams.flag_translucent_status);         }     } 

but you'll notice funky result: enter image description here

this happens because when statusbar transparent layout use height. prevent need to:

solution one:
add line android:fitssystemwindows="true" in layout view container of whatever want placed bellow actionbar:

    ...         <linearlayout                 android:fitssystemwindows="true"                 android:layout_width="match_parent"                 android:layout_height="match_parent">             ...         </linearlayout>     ... 

solution two:
add few lines our previous method:

protected void setstatusbartranslucent(boolean maketranslucent) {         view v = findviewbyid(r.id.bellow_actionbar);         if (v != null) {             int paddingtop = build.version.sdk_int >= build.version_codes.kitkat ? myscreenutils.getstatusbarheight(this) : 0;             typedvalue tv = new typedvalue();             gettheme().resolveattribute(android.support.v7.appcompat.r.attr.actionbarsize, tv, true);             paddingtop += typedvalue.complextodimensionpixelsize(tv.data, getresources().getdisplaymetrics());             v.setpadding(0, maketranslucent ? paddingtop : 0, 0, 0);         }          if (maketranslucent) {             getwindow().addflags(windowmanager.layoutparams.flag_translucent_status);         } else {             getwindow().clearflags(windowmanager.layoutparams.flag_translucent_status);         }     } 

where r.id.bellow_actionbar layout container view id of whatever want placed bellow actionbar:

...     <linearlayout             android:id="@+id/bellow_actionbar"             android:layout_width="match_parent"             android:layout_height="match_parent">         ...     </linearlayout> ... 

enter image description here

so it, think i'm not forgetting something. in example didn't use toolbar think it'll have same result. how customize actionbar:

@override     protected void oncreate(bundle savedinstancestate) {         view vg = getactionbarview();         getwindow().requestfeature(vg != null ? window.feature_action_bar : window.feature_no_title);          super.oncreate(savedinstancestate);         setcontentview(getcontentview());          if (vg != null) {             getsupportactionbar().setcustomview(vg, new actionbar.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent));             getsupportactionbar().setdisplayshowcustomenabled(true);             getsupportactionbar().setdisplayshowhomeenabled(false);             getsupportactionbar().setdisplayshowtitleenabled(false);             getsupportactionbar().setdisplayuselogoenabled(false);         }         setstatusbartranslucent(true);     } 

note: abstract class extends actionbaractivity
hope helps!