Home » » Implement transparent status bar for android.support.v7.widget.Toolbar

Implement transparent status bar for android.support.v7.widget.Toolbar

Posted by Rom Flash Media on Sunday, February 19, 2017

Implement transparent status bar for android.support.v7.widget.Toolbar:

- Follow the steps in "Replace ActionBar with android.support.v7.widget.Toolbar" to implement a simplest toolbar.


- Edit styles.xml to add the item of "android:windowTranslucentStatus".
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>

</resources>


Edit MainActivity.java to set the padding (with the height of status bar) of the status.
package com.blogspot.android_er.androidtoolbarex2;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.setPadding(0, getStatusBarHeight(), 0, 0);

}

private int getStatusBarHeight() {
int height;

Resources myResources = getResources();
int idStatusBarHeight = myResources.getIdentifier(
"status_bar_height", "dimen", "android");
if (idStatusBarHeight > 0) {
height = getResources().getDimensionPixelSize(idStatusBarHeight);
Toast.makeText(this,
"Status Bar Height = " + height,
Toast.LENGTH_LONG).show();
}else{
height = 0;
Toast.makeText(this,
"Resources NOT found",
Toast.LENGTH_LONG).show();
}

return height;
}

}


After finished:





0 comments:

Post a Comment

Popular Posts

Blog Archive

.comment-content a {display: none;}