Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set windowTranslucentStatus=true when android lollipop or higher

I'd like to set windowTranslucentStatus to true when the user is on lollipop and above because otherwise (at least on kitkat) the app bar appears inside the system bar. On lollipop it is fine. Without making separate styles.xml for each version which apparently you shouldn't have to do anymore how can I set it in java?

I have the following code in my mainActivity but don't know hot windowTranslucentStatus... Any ideas?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // set windowTranslucentStatus = true
}
like image 990
raptor Avatar asked Mar 19 '15 14:03

raptor


2 Answers

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
like image 189
Satty Avatar answered Oct 14 '22 03:10

Satty


You can switch between translucent or not whenever you want using this :

public static void setTranslucent(Activity activity, boolean translucent){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  Window w = activity.getWindow();
  if(translucent) {
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  }
  else{
    w.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  }
}
like image 26
awsleiman Avatar answered Oct 14 '22 05:10

awsleiman