Connecting toolbar with the navigation drawer - android

I have several activities where I want to use the same navigation drawer. Here's part of my Activity class code related to the drawer:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
I'm planning to make a DrawerActivity class and extend it from all those activities which want the Navigation drawer. Therefore, I will have to move all the drawer code to that DrawerActivity.
The problem:
toolbar is important for the initialization of ActionBarDrawerToggle. Every activity has a different toolbar depending upon the needs of that activity. Therefore, the toolbar layouts must be kept in their respective layout xml files. How would I use the toolbar then in the DrawerActivity class?
Layout xml for the drawer:
<?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">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/frame_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
app:itemBackground="#drawable/drawer_list_selector"
style="#style/NavigationDrawer"
app:headerLayout="#layout/sidenav_header"
app:menu="#menu/side_navigation_menu"
app:theme="#style/Drawer"/>
</android.support.v4.widget.DrawerLayout>
Here's the SideNavigation activity (which I'm trying to extend by those classes who need navigation drawer): pastebin.com/hheMXku5
main statement of the log says:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
Here's the link to the onCreate of one of the activity class that extends the SideNavigation class: pastebin.com/iKYXVbda

okey so as you want to use drawer with Activity. I am posting my code here. I hope this will help you out.
First of all you need to develop NavigationDrawer. I know you can do it by your self.
Now create child Activity for Example Home.java and extend it with your drawer Activity let's say MainActivity.java.
it will be something like this.
public class Home extends MainActivity{
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Now usually we perform setContentView in onCreate, bur here do something like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.home, frameLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
Where frameLayout would be your container, which you have take in MainActivity.
Note : don't forget to take another ToolBar in home Activity. because it will override MainActivity's toolbar.
EDIT
I am posting my Whole code please refer it. Although I have took my custom Navigation drawer but it can work with NavigationView too.
activity_main
<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:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false" >
<!-- ToolBar of MainActivity -->
<include
android:id="#+id/toolbar_headers"
layout="#layout/toolbar_header" />
</RelativeLayout>
<!-- here will be your NavigationView -->
<com.random.extraclasses.ScrimInsetsFrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginRight="-8dp"
android:elevation="8dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000" >
<include
android:id="#+id/drawar_layout"
layout="#layout/drawar_layout" />
</com.gujarat.property.extraclasses.ScrimInsetsFrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements OnClickListener {
protected RelativeLayout frameLayout;
protected DrawerLayout mDrawerLayout;
protected ListView mDrawerList;
protected ScrimInsetsFrameLayout mLayout;
protected ActionBarDrawerToggle mDrawerToggle;
protected static boolean isLaunch = true;
Toolbar toolbar;
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
frameLayout = (RelativeLayout) findViewById(R.id.frame_container);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list);
mLayout = (ScrimInsetsFrameLayout) findViewById(R.id.container);
toolbar = (Toolbar) findViewById(R.id.toolbar_headers);
setSupportActionBar(toolbar);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1)));
navMenuIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedPos = position - 1;
displayView(position);
}
});
mDrawerList.setAdapter(adapter);
mDrawerList.setFocusable(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name) {
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (isLaunch) {
isLaunch = false;
displayView(0);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void displayView(int position) {
mDrawerLayout.closeDrawer(mLayout);
MainActivity.position = position;
switch (position) {
case 0:
if (this.getClass().getSimpleName().equals("Home")) {
} else {
Intent i = new Intent(this, Home.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
case 1:
if (this.getClass().getSimpleName().equals("ForSale")) {
} else {
Intent i = new Intent(this, ForSale.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
case 2:
if (this.getClass().getSimpleName().equals("ForRent")) {
} else {
Intent i = new Intent(this, ForRent.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
case 3:
if (this.getClass().getSimpleName().equals("NewProjects")) {
} else {
Intent i = new Intent(this, NewProjects.class);
startActivity(i);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
break;
default:
break;
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onClick(View v) {
}
}
And finally Home.java
public class Home extends MainActivity implements OnClickListener {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.home, frameLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("");
}
#Override
public void onBackPressed() {
super.onBackPressed();
if (mDrawerLayout.isDrawerOpen(mLayout)) {
isLaunch = false;
mDrawerLayout.closeDrawer(mLayout);
} else {
finishAffinity();
}
}
And just for reference home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:background="#drawable/back51" >
<TextView
android:id="#+id/etSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#android:color/transparent"
android:text="Search Property"
android:textColor="#666666" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#color/toobar"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
</RelativeLayout>
I hope this will help you out.

Related

onCreateOptionsMenu() override not being called in BaseActivity

My base activity displays the toolbar but has no title, burger or overflow menus on the bar. I can not figure out how to get the menus to show. Any help would be much appreciated! onCreateOptionsMenu() is not being called in the base activity when I debug.
BaseActivity
public abstract class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(int layoutResID) {
final DrawerLayout fullView = (DrawerLayout)
getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContainer = (FrameLayout)
fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
}
#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) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
myActivity
public class myActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_item);
}
}
Any idea what I am missing to make this work? I have read around and am really struggling to fix this!
My 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: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"
tools:context="com.mpd.sfs.BaseActivity">
<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="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Navigation Drawer with toolbar Implementation in multiple activites latest code

i created navigation drawer in base activity and extends in main activity but there is no result in main activity
Here is my code:
BaseActivity Java code:
public class BaseActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
String[] items;
ActionBarDrawerToggle mDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
items = getResources().getStringArray(R.array.menu);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, items));
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
});
}
public void selectItem(int position){
Intent intent;
switch (position)
{
case 0:
intent = new Intent(this,OneActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(this,TwoActivity.class);
startActivity(intent);
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
And here is my Xml Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
toolbar:navigationIcon="#drawable/ic_navigation">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/colorAccent"
android:choiceMode="singleChoice"
android:divider="#android:color/holo_blue_dark"
android:dividerHeight="1dp">
</ListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
And here is MainActivity:
public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
}
}
suggest me the right answer
create a constructor in baseActivity class and call your defined functions of baseActivity class in constructor and than call that constructor in your MainActivity class function onCreate(Bundle savedInstanceState).

Change ActionBarDrawerToggle icon android in toolbar?

I have an activity with navigation drawer and toolbar
Activity
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private CustomTextViewMondaRegular tvTitle;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
tvTitle = (CustomTextViewMondaRegular) findViewById(R.id.actionTitle);
tvTitle.setText(getIntent().getStringExtra("title"));
mDrawerList.setAdapter(new NavAdapter(getApplicationContext()));
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,
R.string.title_activity_main, R.string.title_activity_main) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
// creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Logger.e("pos", position + "");
}
}
}
Activity XML
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.vf.MainActivity">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/white"
android:elevation="3dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/MyDarkToolbarStyle">
<TextView
android:id="#+id/actionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:drawableEnd="#drawable/logo"
android:drawableRight="#drawable/logo"
android:gravity="center"
android:text="XXXXXX"
android:textColor="#000000"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:choiceMode="singleChoice"
android:divider="#D8D8D8" />
The toggle icon currently shows the default icon. I need to change that icon. How can I do that?
For people who come across this SO question in future,
We have to disable drawer indicator
mDrawerToggle.setDrawerIndicatorEnabled(false);
and then set ToolBar's navigation button
mToolbar.setNavigationIcon(R.drawable.navIcon);
P.S. after that we have to set Navigation click listner on toolbar and open NavigationDrawer manualy.
like
mToolbar.setNavigationOnClickListener :D
Try the code below:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_drawer);
mDrawerToggle.setDrawerIndicatorEnabled(false);
and open the drawer with
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.openDrawer(Gravity.START);
}
});
You can set the menu icon using setHomeAsUpIndicator, pass a drawable to this method.
See code below:
ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);

drawer icon in Actionbar is show me a error when I click then

Im using Android Studio 1.2
The icon drawer in actionbar is give me an error when I click then but is work fine If I open it sliding with the hand from the left to the rigth.
this is my layout where I have my drawer list from the left; the list of options is in listView "mimenu"
<RelativeLayout xmlns:android="xxxxxx"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:background="#ffffff"
tools:context="xxxx">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="#+id/listaxx"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
>
<ListView
android:id="#+id/mimenu"
android:layout_width="match_parent"
android:layout_below="#+id/profileBox"
android:choiceMode="singleChoice"
android:layout_height="match_parent"
android:background="#ffffff"
android:divider="#eee"
android:dividerHeight="1dp"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
In my class java
public class ListaPat extends Activity {
public ArrayList<Pat> lstPat= new ArrayList<Pat>();
private Pat_Adaptador adaptador;
private LinearLayout linearLayout;
ArrayAdapter<CharSequence> navigationDrawerAdapter;
ListView leftDrawerList;
String[] leftSliderData = new String[]{"test1","test2"};
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_patx);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
leftDrawerList = (ListView)findViewById(R.id.mimenu);
navigationDrawerAdapter= new ArrayAdapter<CharSequence>( ListaPat .this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
RellenarNoticias();
InicializarLista();
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
mDrawerLayout.closeDrawer(leftDrawerList);
}
else {
mDrawerLayout.openDrawer(leftDrawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to extend either ActionbarActivity, or AppCompatActivity, and use a Toolbar widget to be in the beginning of your Activity. In your activities on create, do the following:
public class YourActivity extends ActionBarActivity{
...
...
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//Set toggle with toolbar now!
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
And for your Toolbar that lives in your activity xml (the very first element for your layout!)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#color/primary_color"
android:title="#string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="CreateSomeStyleHereIfYouNeed"
app:popupTheme="#android:style/Theme.Holo.Light"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</android.support.v7.widget.Toolbar>
I fix the problem changing this function
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
mDrawerLayout.closeDrawer(leftDrawerList);
}
else {
mDrawerLayout.openDrawer(leftDrawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
New function that fix the problem
#Override public boolean onOptionsItemSelected(MenuItem item) {if (mDrawerToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }

Android DrawerLayout doesn't work

I'm trying to use the DrawerLayout with ActionBarDrawerToggle.
see my code below:
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getName();
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
FeedsFragment feeds = new FeedsFragment();
if (findViewById(R.id.main) != null) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().beginTransaction().add(R.id.main, feeds).commit();
} else if (findViewById(R.id.content) != null) {
NavDrawerFragment nav = new NavDrawerFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.nav, nav)
.add(R.id.content, feeds)
.commit();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
mDrawerToggle.onConfigurationChanged(config);
}
}
and my view:
<?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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="#layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="#layout/nav" />
</android.support.v4.widget.DrawerLayout>
But when I run my project, the left menu (DrawerLayout) just show on my screen doesn't make the slider event.
Finaly works:
I just added the gravity="start" at my second view
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="#layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="#layout/nav" />
</android.support.v4.widget.DrawerLayout>
Uses the function onOptionsItemSelected and control the drawer event:
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID) {
case android.R.id.home: {
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.closeDrawers();
}
else {
drawerLayout.openDrawer(Gravity.LEFT);
}
}
break;
return true;
}
I hope it works for your case.

Categories

Resources