Android - DrawerLayout and Toolbar not visible on first opening - android

I'm currently developing an android app using a Toolbar and a DrawerLayout. The main content is a custom SurfaceView where I can draw differents shapes, text ... The left menu is a NavigationView and is used as a toolbox (I select what I want to draw from the left and I draw it on the SurfaceView). Everything is working fine except for one thing : when I first try to open the left menu (by clicking the toolbar or by sliding from the left side of the screen) the items are not visible. And while I don't click on any item (which are not visible) they stay invisible. The problem is only fixed when I click on an invisible item, after that the menu is working fine. I'm using a custom theme to hide the status bar and remove the default actionbar :
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light.NoActionBar"></style>
<style name="ColladiaTheme" parent="AppBaseTheme">
<!-- Remove action bar -->
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<!-- Remove status bar -->
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<!-- Material theme -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Here are some screenshots :
Menu open but not visible
Menu visible after clicking an item
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ia04nf28.colladia.DrawColladiaView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/draw_view" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</FrameLayout>
<!-- Left navigation menu -->
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="start"
android:fitsSystemWindows="false"
app:menu="#menu/nav_view_items" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And here is the Activity code :
public class DrawActivity extends AppCompatActivity {
private static final String TAG = "DrawActivity";
private DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
private NavigationView nav;
private DrawColladiaView colladiaView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
// Change toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
colladiaView = (DrawColladiaView) findViewById(R.id.draw_view);
colladiaView.setApplicationCtx(getApplicationContext());
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// Add burger button
drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(drawerToggle);
// Removes overlay
drawer.setScrimColor(Color.TRANSPARENT);
drawer.closeDrawers();
nav = (NavigationView) findViewById(R.id.nav_view);
setUpDrawerContent(nav);
}
public void setUpDrawerContent(NavigationView nav)
{
nav.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
selectDrawerItem(item);
return true;
}
}
);
}
public void selectDrawerItem(MenuItem item)
{
switch(item.getItemId())
{
case R.id.nav_home:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
break;
default:
Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString());
if (newElement != null) colladiaView.insertNewElement(newElement);
drawer.closeDrawers();
break;
}
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}

It looks like you should be overriding onOptionsItemSelected instead of onBackPressed:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// This will toggle the drawer if android.R.id.home is clicked
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle any other menu item selections...
return super.onOptionsItemSelected;
}
To be honest, I do not see where your drawer is ever being opened. It should still respond to a swipe from the left edge though. This code will set it to be toggled when android.R.id.home is clicked (the back/menu button).

Related

open and close DrawerLayout null exception

I am trying to add DrawerLayout on my app but the items are not responding to click. I have tried logging the output on the console but nothing show up when the items are clicked. Here is my code
MainActivity
public class DashboardActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawal);
drawerLayout = (DrawerLayout) findViewById(R.id.drawal_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
Log.i("info", String.valueOf(id));
switch (id){
case R.id.dashoard:
Toast.makeText(DashboardActivity.this, "This is dasbhard", Toast.LENGTH_SHORT).show();
case R.id.hire_mechanic_id:
Intent intents = new Intent(DashboardActivity.this, LIkelyProblemsActivity.class);
startActivity(intents);
break;
case R.id.tow_van_id:
Toast.makeText(getApplicationContext(),"Hire a Toyin Van", Toast.LENGTH_LONG).show();
break;
}
drawerLayout.closeDrawer(Gravity.START);
return true;
}
}
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawal_layout"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<include layout="#layout/activity_dashboard" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:id="#+id/navigation_view"
android:layout_gravity="start"
android:fitsSystemWindows="false"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:headerLayout="#layout/activity_navigation_header"
app:menu="#menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
item.xml
<item android:title="Dashboard">
<menu>
<item
android:title="item 1"
android:icon="#drawable/mechanic"
android:id="#+id/dashoard"/>
<item
android:title="item 2"
android:icon="#drawable/mechanic"
android:id="#+id/hire_mechanic_id"/>
<item
android:title="item 3"
android:icon="#drawable/spareparts"
android:id="#+id/spare_parts_id"/>
<item
android:title="item 4"
android:id="#+id/tow_van_id"
android:icon="#drawable/toyin_van" />
</menu>
</item>
</menu>
I have checked the drawerlayout, menu and the main activity but I can't really figure out what the problem is. Anyone one who can help me.
You forget to call navigationView.setNavigationItemSelectedListener listener that's why navigation item not being called, just add this line
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this)
Hope it will help you!!

Align menu item at Navigation to the right side in android

I made the navigation drawer at android app and make everything correctly to appear it from the right side but only I face one problem at the alignment of menu item to the right side but I couldn't , how I can do that?
The picture show navigation with menu items in the left side
This is the main_activity layout code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
and this is the activity_main_drawer menu items xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single" >
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Camera" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
The problem in these items I need to display it at right side not at the left side because my navigation opens from right to left
MainActivity code :
public class ParentMainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_parent);
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "You have chosen mail option", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageButton menuRight = (ImageButton) findViewById(R.id.menuRight);
menuRight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
drawer.openDrawer(GravityCompat.END);
}
}
});
/*ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();*/
NavigationView navigationView1 = (NavigationView) findViewById(R.id.nav_view);
navigationView1.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
String text = "";
if (id == R.id.nav_camera) {
text = "camera";
} else if (id == R.id.nav_gallery) {
text = "gallery";
} else if (id == R.id.nav_slideshow) {
text = "slideshow";
} else if (id == R.id.nav_manage) {
text = "tools";
} else if (id == R.id.nav_share) {
text = "share";
} else if (id == R.id.nav_send) {
text = "send";
Toast.makeText(this, "You have chosen " + text, Toast.LENGTH_LONG).show();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.END);
return true;
}
}
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
android:layoutDirection="rtl" // set layoutDirection to 'rtl
app:itemIconTint="#drawable/drawer_selectcolor_imagecolor"
app:itemTextColor="#drawable/drawer_selectcolor_textcolor"
app:menu="#menu/nav_menu"/>
If you project is using API level 17 or higher you can add android:supportsRtl to you manifest.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
...
</application>
This attribute declares whether your application is willing to support right-to-left (RTL) layouts. If set to true and targetSdkVersion is set to 17 or higher, various RTL APIs will be activated and used by the system so your app can display RTL layouts. If set to false or if targetSdkVersion is set to 16 or lower, the RTL APIs will be ignored or will have no effect and your app will behave the same regardless of the layout direction associated to the user's Locale choice (your layouts will always be left-to-right).
EDIT:
The answer in this post shows that exists a way to force RTL mode if it is available. In onCreate() method call forceRTLIfSupported().
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}

Navigation drawer menu and setting menu not working on Android kitkat and its lower version

Navigation drawer menu and setting menu not working on Android kitkat and its lower version. But these menu options are completely functional on Android lollipop and its higher version. Even menu popup for setting does not display on kitkat or its lower version but it displays on android lollipop or its higher version. Following is the code :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appbar"/>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="56dp"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.vinitmandlesha.myins.MainActivity"
android:background="#f2374a">
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.vinitmandlesha.myins.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
styles.xml
<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>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
styles.xml(v21)
<resources>>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (id == R.id.nav_home) {
// Handle the camera act
} else if (id == R.id.nav_about_us) {
FragmentPersonal frag = new FragmentPersonal();
transaction.add(R.id.drawer_layout,frag, "About");
transaction.commit();
} else if (id == R.id.nav_contactus) {
} else if (id == R.id.nav_faq) {
} else if (id == R.id.nav_personal) {
} else if (id == R.id.nav_commercial) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
First when you are updating your toolbar it always hide your default menu. What you have to do is create your on menu with an icon and set is properties e.g ifroom. On the other hand for your navigation drawer you have to set your a theme with action bar activity and if you are using it in devices which are lollipop version than you will have to use AppCompat theme. I will reference the tutorial of slidenerd which are available on you tube which covers the material design it have details you need about navigation drawer in lollipop and pre-lollipop devices .

How to get the new NavigationView to play nice with status bar scrim?

I've been playing with Google's new design support library and it's a blast! I'm just a little stumped though on the navigation view. All the things I read say that the NavigationView is smart enough to handle transparent scrim on its own. (The android-developers post, for one; search for scrim). Anyway, when I tried to do it I get the following result:
Which is great; Exactly what I want. Except for one thing. When the drawer is closed, the scrim is an ugly dark grey, not my primaryColorDark . . .
Here's my layout:
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<fragment
android:id="#+id/fragment"
android:name="com.gmail.rixx.justin.envelopebudget.HomeFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_home" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/accent"
android:src="#drawable/ic_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
The Activity code:
public class Home extends ActionBarActivity {
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private Context mContext = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setUpToolbar();
setUpNavDrawer();
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(mContext, NewTransactionActivity.class));
}
});
}
private void setUpToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
}
}
private void setUpNavDrawer() {
if (mToolbar != null) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And v21/styles:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
I modeled after Chris Banes's CheeseSquare app on github but I'm not getting the behavior I want.
I've tried removing the windowDrawsSystemBarBackgrounds and statusBarColor from v21/styles, and I get the proper color, but the status bar never goes transparent:
Help would be appreciated. This is new stuff, So I know we're all learning.
Thanks for reading!
-Justin
After struggling with this for several more hours, and copiously comparing my code to the cheesesquare app, I found the following: The DrawerLayout must have the attribute android:fitsSystemWindows="true", and the NavigationView as well, but the CoordinatorLayout should not. Once I made those changes, it worked.
Thanks all, and hopefully this is helpful to somebody!
You can look at my code for the layout here.
-Justin
set this to your navigationView android:fitsSystemWindows="false"
You should check newer SDK examples.
actually they are using Coordinatorlayout in activity that you want to draw under statusbar and have different theme for that layout, because if you put transparent statusbar for other activities where you don't have root layout Coordinatorlayout it will show white uncolored statusbar.
set this to your navigationView android:fitsSystemWindows="true" this will ensure that the navigation will be behind the statusbar and add colorAccent to your style in values folder.
In your style file try to use AppCompact theme as parent ...
<style name="AppTheme" parent="Base.Theme.AppCompat">
I hope it helps you..

Notification Bar is grey after implementing Nav Drawer

I am trying to learn implementations of Navigation Drawer in Android.
In one activity, i have made the Navigation Drawer come under the Status Bar(transparent) and over the App Bar and everything works fine.(left Screenshot)
In another activity in the same App, i am trying to create Navigation Drawer that pulls up under the App Bar. But here, the status bar turns grey for some reason(whether nav drawer is open or closed)(Right Screenshot). Other than this, everything seems fine.
Below is the screenshot:
The green Nav Drawer is a fragment.
What i want to know is how to make the status normal(darker shade of. Please remember, if i click that twin arrow icon in the App Bar, it will take me to another activity which contains another Nav Drawer which works like the one in Material Design(Full height of the screen and comes under a transparent Status Bar). This is shown in the left side of the screenshot.
Below is the code:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
DrawerLayout xDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alt);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
xDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
xDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark));
NavDrawerFragment mfragment = (NavDrawerFragment) getFragmentManager().findFragmentById(R.id.nav_drawer_fragment);
mfragment.SetUp(xDrawerLayout, toolbar, R.id.nav_drawer_fragment);
}
...
activity_alt.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
<fragment
android:id="#+id/nav_drawer_fragment"
android:name="com.rt.droid.mattest.NavDrawerFragment"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_nav_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
NavDrawerFragment.java:
public class NavDrawerFragment extends Fragment {
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
public DrawerLearner yDrawerLearner;
public NavDrawerFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fView = inflater.inflate(R.layout.fragment_nav_drawer, container, false);
//fView.setFitsSystemWindows(true);
fView.setBackgroundColor(getResources().getColor(R.color.accent));
return fView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void SetUp(DrawerLayout drawerLayout, Toolbar toolbar, int frag_id) {
this.mDrawerLayout = drawerLayout;
View drawerFrag = getActivity().findViewById(frag_id);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
yDrawerLearner = new DrawerLearner(mDrawerLayout, drawerFrag, getActivity());
yDrawerLearner.execute();
this.mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark));
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
}
fragment_nav_drawer.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rt.droid.mattest.NavDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
app_bar.xml:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary"
app:popupTheme="#style/AppTheme.PopupMenu"
android:theme="#style/AppTheme.Toolbar">
</android.support.v7.widget.Toolbar>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/accent</item>
</style>
<style name="AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:background">#color/accent</item>
</style>
</resources>
styles.xml(v21):
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
Colors.xml(screenshot showing color samples):
Note: I do not want to compromise on the styles etc that would render my other navigation drawer not working. In other words, i prefer a solution wherein both types of navigation bar works as expected in the same app.
Please let me know if you need any info and i shall edit if required.
Edit: added app_bar.xml & colors.xml for clarity.
But here, the status bar turns grey for some reason
It's the translucent (25% black) status bar background over the white background of activity underneath it.
What i want to know is how to make the status normal
You need to disable translucent status bar for the second activity. It is activated by this line in your theme definition:
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">#color/primary_dark</item>
So either define a child theme and override the flag with false or clear the flag programmatically by calling this from your activity:
if (Build.VERSION.SDK_INT >= 21) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark);
}

Categories

Resources