I am working with Navigation Drawer in Android.I have code from some examples.In my code working to change the drawer open and drawer close title but the panel is not open.I not insert the ListView array. My main Problem is not open the panel.Is anything I missed in the code?
layout_mynavigation.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="150dp"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
MyNavigation.java
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
public class MyDrawer extends AppCompatActivity {
private ActionBarDrawerToggle mActionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mydrawer);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.drawer_open,R.string.drawer_closed) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle( R.string.drawer_open );
invalidateOptionsMenu();
}
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle( R.string.drawer_closed );
invalidateOptionsMenu();
}
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged( newConfig );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return mActionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Customize your theme here. -->
</style>
DrawerLayout expects it's first child to be the actual layout and the second one to be the drawer. So, to make it work you should change your layout to (assuming that your entire drawer consists of only one ListView):
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_height="150dp"
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"/>
</LinearLayout>
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
It looks like your xml layout needs to be re-arranged. The DrawerLayout should wrap two items; 1) a View or ViewGroup to set as the page content (e.g., RelativeLayout containing a Toolbar and Fragment container); 2) a View or ViewGroup that holds your NavigationDrawer items (e.g., ListView).
<android.support.v4.widget.DrawerLayout
android:id="#+id/navigation_drawer_layout"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/your_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/your_toolbar_color"
android:minHeight="?attr/actionBarSize"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/your_toolbar"/>
</RelativeLayout>
<ListView
android:id="#+id/your_drawer_list"
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#color/your_drawer_color"
android:divider="#null"/>
</android.support.v4.widget.DrawerLayout>
You can get a more detailed explanation and examples here:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
If your list is empty and you have the height as wrap_content, the height will be 0. Changing it to match_parent will have it fill the screen regardless of the content.
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#000000"
android:layout_gravity="start"/>
Related
In google developer blog post . I read about new way to create navigation drawer using new dependency called
compile 'com.android.support:design:22.2.0'
but I didn’t found exact way to create navigation drawer using this new dependency.
In build.gradle , I have added dependency
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.0.0'
in layout file added following code (based on google blog post)
<android.support.v4.widget.DrawerLayout
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="match_parent"
android:fitsSystemWindows="true">
<!-- your content layout -->
<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>
Aslo I tried to extend class using ActionBarActivity but its deprecated?
Ref:http://android-developers.blogspot.in/2015/05/android-design-support-library.html
Any help appreciated .Thank you
Hi try following steps
Add Android design support library dependency
compile 'com.android.support:design:22.2.1'
Create a header for the navigation drawer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/header"
android:padding="16dp"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nirav Kalola\nnkDroid"
/>
</LinearLayout>
Create a menu for navigation drawer items
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="nkdroid.tutorial.navigationview.MainActivity">
<group android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_action_home"
android:title="Home"/>
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_action_info"
android:title="About Us"/>
<item
android:id="#+id/navigation_subheader"
android:title="Tutorials">
<menu>
<item
android:id="#+id/navigation_sub_item_1"
android:icon="#drawable/ic_image_looks_one"
android:title="Android Tutorials"/>
<item
android:id="#+id/navigation_sub_item_2"
android:icon="#drawable/ic_image_looks_two"
android:title="IOS Tutorials"/>
</menu>
</item>
</group>
</menu>
Create Navigation View with header and items
<android.support.v4.widget.DrawerLayout
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="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true">
<!--Main content-->
<LinearLayout
android:orientation="vertical"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/app_bar"/>
</LinearLayout>
<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
android:id="#+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
Implementing Navigation View
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private NavigationView mDrawer;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;
private int mSelectedId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setToolbar();
initView();
drawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
//default it set first item as selected
mSelectedId=savedInstanceState ==null ? R.id.navigation_item_1: savedInstanceState.getInt("SELECTED_ID");
itemSelection(mSelectedId);
}
private void setToolbar() {
toolbar= (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
private void initView() {
mDrawer= (NavigationView) findViewById(R.id.main_drawer);
mDrawer.setNavigationItemSelectedListener(this);
mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
}
private void itemSelection(int mSelectedId) {
switch(mSelectedId){
case R.id.navigation_item_1:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.navigation_item_2:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.navigation_sub_item_1:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.navigation_sub_item_2:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mSelectedId=menuItem.getItemId();
itemSelection(mSelectedId);
return true;
}
#Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
//save selected item so it will remains same even after orientation change
outState.putInt("SELECTED_ID",mSelectedId);
}
}
you can directly download source code from my blog
hello #user3739665 i am also trying to lean support library, but i don't think there is proper way(because no sample available right now). so here is my tried code, just for demonstration how to use that lib.
i change that layout to like below added main fragment, you can also add toolbar.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<!-- your content layout -->
<!--<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<include layout="#layout/about_fragment"></include>
<android.support.design.widget.NavigationView
android:id="#+id/nav_draw"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
i have create drawer.xml for menu and drawer_header.xml for user detail just like show in blog
my activity code
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
View root;
NavigationView nav_draw;
DrawerLayout drawer_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
root = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(root);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
drawer_layout = (DrawerLayout)findViewById(R.id.drawer_layout);
nav_draw = (NavigationView) findViewById(R.id.nav_draw);
nav_draw.setNavigationItemSelectedListener(this);
/* getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AboutPagerFragment())
.commit();*/
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawer_layout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.navigation_item_1) {
Snackbar
.make(root, "First item Selected", Snackbar.LENGTH_LONG)
//.setAction(R.string.snackbar_action, myOnClickListener)
.show();
}
menuItem.setChecked(true);
drawer_layout.closeDrawers();
return false;
}
/*#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_main, menu);
return true;
}*/
}
add this in you build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
}
drawer.xml
<?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/navigation_item_1"
android:checked="true"
android:title="#string/navigation_item_1">
</item>
<item
android:id="#+id/navigation_item_2"
android:title="#string/navigation_item_2" />
<item
android:id="#+id/navigation_subheader"
android:title="#string/navigation_subheader">
<menu>
<item
android:id="#+id/navigation_sub_item_1"
android:title="#string/navigation_sub_item_1" />
<item
android:id="#+id/navigation_sub_item_2"
android:title="#string/navigation_sub_item_2" />
</menu>
</item>
</group>
</menu>
drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="130dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView" />
</LinearLayout>
EDIT
added navigation item open,close, enable items selection
The "java part" can look like this:
Set your activity as a listener
navView.setNavigationItemSelectedListener(this);
And than handle events the same way you handle menu interaction
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
menuItem.setChecked(true);
drawerLayout.closeDrawers();
if (id == R.id.some_item_1) {
} else if (id == R.id.some_item_2) {
} else {
throw new IllegalStateException("Unsupported menu item");
}
return true;
}
Check this github project for all examples of using design library.
Tutorial: Navigation View Design Support Library
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
...
/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
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>
activity_main
<?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="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:context="com.azim.innovation.MainActivity">
<android.support.design.widget.AppBarLayout
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/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 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.azim.innovation.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
nav_header_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
activity_main_drawer
<?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="Import" />
<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>
</menu>
colors
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
dimens
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="nav_header_vertical_spacing">16dp</dimen>
<dimen name="nav_header_height">160dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>
MainActivity
package com.azim.innovation;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawer;
#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);
if (fab != null)
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();
}
});
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(this);
}
}
#Override
public void onBackPressed() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.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();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
}
dependencies
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
styles
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 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-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>
It's not clear exactly what your issue is (in what way did it not work?),
but perhaps you missed the fact that you muse actually define the 'drawer_header' layout and the 'drawer' menu?
If that's not the problem, please explain in more detail exactly what you're having a problem with.
Android Studio 3.4
styles.xml
<style name="Main.Theme.Tango" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here -->
<item name="colorPrimary">#color/colorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<!-- theme UI controls like buttons, checkboxes and text fields or cursor -->
<item name="colorAccent">#color/colorAccent</item>
</style>
in manifest:
<application
android:name=".main.MainApp"
android:allowBackup="true"
android:icon="#drawable/ic_app"
android:label="#string/application_name"
android:logo="#drawable/ic_app_logo"
android:theme="#style/Main.Theme.Tango" >
In activity:
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
public class MainNavigationDrawerFragmentActivity extends AppCompatActivity{
private void initialize(Bundle savedInstanceState) {
mTitle = mDrawerTitle = getTitle();
mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar, R.string.application_name,
R.string.application_name) {
public void onDrawerClosed(View view) {
mToolbar.setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
mToolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu();
loadDraftsTotalNumber();
loadActiveCart();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
and here layout:
<androidx.drawerlayout.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">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="#dc2700"
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="288dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#null"
android:listSelector="#drawable/list_selector"
android:paddingLeft="16dip"
android:paddingRight="16dip"/>
</androidx.drawerlayout.widget.DrawerLayout>
but toolbar not show:
Try this for your layout. It works for me.
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/color3"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="#dc2700"
/>
</com.google.android.material.appbar.AppBarLayout>
Try setting android:layout_width="match_parent" for toolBar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
I found solution
The toolbar AND FrameLayout must be inside LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="56dp"/>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
and here result:
I've made a custom toolbar and its Right-To-Left. and it looks like this:
I want a navigation drawer appears when I click on the right side icon. But when I add the navigation drawer to app, it crashes. I've tried many things and also searched a lot, but it didn't work.
It'd be GREAT it someone would help me.
Here is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.playpersia.mycustomizedtoolbar.MainActivity"
>
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|end" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
right_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.playpersia.mycustomizedtoolbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.zip.Inflater;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
public ListView listView;
public String[] pages;
ArrayAdapter<String> arrayAdapter;
public ImageView menu_icon;
public ImageView back_arrow;
public boolean mSlideState;
private DrawerLayout drawerLayout;
public ActionBarDrawerToggle _mDrawerTg;
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
menu_icon = (ImageView) findViewById(R.id.menu_icon);
back_arrow = (ImageView) findViewById(R.id.back_button);
drawerLayout =(DrawerLayout) findViewById(R.id.right_drawer);
pages =getResources().getStringArray(R.array.pages);
_mDrawerTg =new ActionBarDrawerToggle(this, drawerLayout,
null, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view){
mSlideState=false;
}
public void onDrawerOpened (View drawerView){
super.onDrawerOpened(drawerView);
mSlideState=true;
} };
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,pages));
drawerLayout.setDrawerListener(_mDrawerTg);
_mDrawerTg.syncState();
menu_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mSlideState){
drawerLayout.closeDrawer(GravityCompat.END);
}else{
drawerLayout.openDrawer(GravityCompat.END); } } }); }
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
_mDrawerTg.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
_mDrawerTg.onConfigurationChanged(newConfig);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (_mDrawerTg.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
UPDATE
logcat:
01-18 12:34:25.386 29949-29949/com.playpersia.mycustomizedtoolbar
E/AndroidRuntime: FATAL EXCEPTION: main Process:
com.playpersia.mycustomizedtoolbar, PID: 29949 java.lang.RuntimeException:
Unable to start activityComponentInfo{com.playpersia.mycustomizedtoolbar/com.playpersia.mycustomizedtool
bar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)at
android.app.ActivityThread.access$800(ActivityThread.java:156)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)at
android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
UPDATE 2
now I have this toolbar when I click on the right side icon the drawer opens & closes,
right side drawer
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference at
com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
In onCreate method, your listView object is null.
You need to add
listView = (ListView) findViewById(R.id.list_view);
before
listView.setAdapter(new ArrayAdapter<String (this,R.layout.drawer_list_item,pages));
UPDATE:
Change your activity_main.xml to:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
This should be your MainLayout since you are using DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your contents, for example, CoordinatorLayout with material design -->
<!-- And after main Contents, here you can use NavigationDrawer also -->
</android.support.v4.widget.DrawerLayout>
And instead of commented code, use your contents.
You did it in wrong way, Take a look:
http://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout
To add a navigation drawer, declare your user interface with a
DrawerLayout object as the root view of your layout. Inside the
DrawerLayout, add one view that contains the main content for the
screen (your primary layout when the drawer is hidden) and another
view that contains the contents of the navigation drawer.
For example, the following layout uses a DrawerLayout with two child
views: a FrameLayout to contain the main content (populated by a
Fragment at runtime), and a ListView for the navigation drawer.
Example:
<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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
You may want to take a look at the Material Design Guidelines for this implementation of course:
https://www.google.com/design/spec/patterns/navigation-drawer.html
In google developer blog post . I read about new way to create navigation drawer using new dependency called
compile 'com.android.support:design:22.2.0'
but I didn’t found exact way to create navigation drawer using this new dependency.
In build.gradle , I have added dependency
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.0.0'
in layout file added following code (based on google blog post)
<android.support.v4.widget.DrawerLayout
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="match_parent"
android:fitsSystemWindows="true">
<!-- your content layout -->
<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>
Aslo I tried to extend class using ActionBarActivity but its deprecated?
Ref:http://android-developers.blogspot.in/2015/05/android-design-support-library.html
Any help appreciated .Thank you
Hi try following steps
Add Android design support library dependency
compile 'com.android.support:design:22.2.1'
Create a header for the navigation drawer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/header"
android:padding="16dp"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:textColor="#ffffff"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nirav Kalola\nnkDroid"
/>
</LinearLayout>
Create a menu for navigation drawer items
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="nkdroid.tutorial.navigationview.MainActivity">
<group android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_action_home"
android:title="Home"/>
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_action_info"
android:title="About Us"/>
<item
android:id="#+id/navigation_subheader"
android:title="Tutorials">
<menu>
<item
android:id="#+id/navigation_sub_item_1"
android:icon="#drawable/ic_image_looks_one"
android:title="Android Tutorials"/>
<item
android:id="#+id/navigation_sub_item_2"
android:icon="#drawable/ic_image_looks_two"
android:title="IOS Tutorials"/>
</menu>
</item>
</group>
</menu>
Create Navigation View with header and items
<android.support.v4.widget.DrawerLayout
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="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true">
<!--Main content-->
<LinearLayout
android:orientation="vertical"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/app_bar"/>
</LinearLayout>
<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
android:id="#+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
Implementing Navigation View
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private NavigationView mDrawer;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;
private int mSelectedId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setToolbar();
initView();
drawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
//default it set first item as selected
mSelectedId=savedInstanceState ==null ? R.id.navigation_item_1: savedInstanceState.getInt("SELECTED_ID");
itemSelection(mSelectedId);
}
private void setToolbar() {
toolbar= (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
private void initView() {
mDrawer= (NavigationView) findViewById(R.id.main_drawer);
mDrawer.setNavigationItemSelectedListener(this);
mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
}
private void itemSelection(int mSelectedId) {
switch(mSelectedId){
case R.id.navigation_item_1:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.navigation_item_2:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.navigation_sub_item_1:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.navigation_sub_item_2:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mSelectedId=menuItem.getItemId();
itemSelection(mSelectedId);
return true;
}
#Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
//save selected item so it will remains same even after orientation change
outState.putInt("SELECTED_ID",mSelectedId);
}
}
you can directly download source code from my blog
hello #user3739665 i am also trying to lean support library, but i don't think there is proper way(because no sample available right now). so here is my tried code, just for demonstration how to use that lib.
i change that layout to like below added main fragment, you can also add toolbar.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<!-- your content layout -->
<!--<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<include layout="#layout/about_fragment"></include>
<android.support.design.widget.NavigationView
android:id="#+id/nav_draw"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
i have create drawer.xml for menu and drawer_header.xml for user detail just like show in blog
my activity code
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
View root;
NavigationView nav_draw;
DrawerLayout drawer_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
root = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(root);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
drawer_layout = (DrawerLayout)findViewById(R.id.drawer_layout);
nav_draw = (NavigationView) findViewById(R.id.nav_draw);
nav_draw.setNavigationItemSelectedListener(this);
/* getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AboutPagerFragment())
.commit();*/
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawer_layout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.navigation_item_1) {
Snackbar
.make(root, "First item Selected", Snackbar.LENGTH_LONG)
//.setAction(R.string.snackbar_action, myOnClickListener)
.show();
}
menuItem.setChecked(true);
drawer_layout.closeDrawers();
return false;
}
/*#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_main, menu);
return true;
}*/
}
add this in you build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
}
drawer.xml
<?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/navigation_item_1"
android:checked="true"
android:title="#string/navigation_item_1">
</item>
<item
android:id="#+id/navigation_item_2"
android:title="#string/navigation_item_2" />
<item
android:id="#+id/navigation_subheader"
android:title="#string/navigation_subheader">
<menu>
<item
android:id="#+id/navigation_sub_item_1"
android:title="#string/navigation_sub_item_1" />
<item
android:id="#+id/navigation_sub_item_2"
android:title="#string/navigation_sub_item_2" />
</menu>
</item>
</group>
</menu>
drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="130dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView" />
</LinearLayout>
EDIT
added navigation item open,close, enable items selection
The "java part" can look like this:
Set your activity as a listener
navView.setNavigationItemSelectedListener(this);
And than handle events the same way you handle menu interaction
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
menuItem.setChecked(true);
drawerLayout.closeDrawers();
if (id == R.id.some_item_1) {
} else if (id == R.id.some_item_2) {
} else {
throw new IllegalStateException("Unsupported menu item");
}
return true;
}
Check this github project for all examples of using design library.
Tutorial: Navigation View Design Support Library
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
...
/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
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>
activity_main
<?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="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:context="com.azim.innovation.MainActivity">
<android.support.design.widget.AppBarLayout
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/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 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.azim.innovation.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
nav_header_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
activity_main_drawer
<?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="Import" />
<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>
</menu>
colors
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
dimens
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="nav_header_vertical_spacing">16dp</dimen>
<dimen name="nav_header_height">160dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>
MainActivity
package com.azim.innovation;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawer;
#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);
if (fab != null)
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();
}
});
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(this);
}
}
#Override
public void onBackPressed() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.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();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
mDrawer.closeDrawer(GravityCompat.START);
return true;
}
}
dependencies
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
styles
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 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-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>
It's not clear exactly what your issue is (in what way did it not work?),
but perhaps you missed the fact that you muse actually define the 'drawer_header' layout and the 'drawer' menu?
If that's not the problem, please explain in more detail exactly what you're having a problem with.
Google mentioned in their Android blog that there are two possibilities if one wants to use the new Toolbar using AppCompat. One is by using it as an ActionBar and one using it standalone, but as I am using ActionBarDrawerToggle, I need the first option. So I have to extend ActionBarActivity and my theme must inherit Theme.Appcompat (or one of it's variations). Also, to use the first option I had to inherit the NoActionBar variation. And finally, I had to call setSupportActionBar(mToolbar), which I did. However, the ActionBar still does not show. Below is my code.
I have already seen this post, but I guess in my case it's SwipeRefreshLayout that ruins it. I tried different places to include the toolbar, but none of them works. How can I include the toolbar without ruining the rest of the layout? Thanks.
MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders_list);
context = getApplicationContext();
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.refreshable_content);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorSchemeResources(R.color.refresh_one,
R.color.refresh_four,
R.color.sun_flower,
R.color.carrot);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setTitle(R.string.title_activity_orders_list);
drawerToggleLeft = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close)
{
#Override
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
getSupportActionBar().setTitle(R.string.title_activity_orders_list);
findViewById(R.id.action_add_order).setVisibility(View.VISIBLE);
invalidateOptionsMenu();
syncState();
}
#Override
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("");
findViewById(R.id.action_add_order).setVisibility(View.INVISIBLE);
invalidateOptionsMenu();
syncState();
}
};
drawerLayout.setDrawerListener(drawerToggleLeft);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggleLeft.syncState();
ordersList = (ListView) findViewById(android.R.id.list);
}
theme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources >
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/turquoise</item>
<item name="colorPrimaryDark">#color/green_sea</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#color/white</item>
</style>
</resources>
custom_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/turquoise" />
activity_order_list.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<include layout="#layout/custom_toolbar" /> // here is the toolbar
<android.support.v4.widget.SwipeRefreshLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrdersListActivity"
android:background="#color/white"
android:id="#+id/refreshable_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#null">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
android:id="#+id/drawer_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:orientation="vertical">
<include layout="#layout/custom_toolbar" /> // here is the toolbar
<fragment
android:id="#+id/drawer_fragment_left"
android:name="com.ordr.view.DrawerFragmentLeft"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>