Navigation drawer toggle doesn't change state - android

I begin a new project, just copy/paste code from an old project where everything is ok. In this one toggle button doesn't change states(it is only arrow and is not working when I am pressing it) like:
to
open/close drawer is working fine using swipe touch
Have to notice that I am using only one Activity that have layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/base_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/base_container_for_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent">
//elements for screen
</RelativeLayout>
<!--that is just for drawer-->
<ListView
android:id="#+id/navigationDrawerList"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#color/color_red_style" />
in my activity:
//declaring vars:
private ListView mDrawerList;
protected DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
in onCreate() :
dataList = new ArrayList<DrawerItem>();
mDrawerLayout = (DrawerLayout) findViewById(R.id.base_layout);
mDrawerList = (ListView) findViewById(R.id.navigationDrawerList);
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
addDrawerItems() is working ok
protected void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer, R.string.close_drawer) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(getResources().getString(R.string.bar_menu));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle("produse");
// creates call to onPrepareOptionsMenu()
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
and of course I put handle toggle in onOptionsItemSelected(MenuItem item)
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
So what's the problem,I just can't realize... Any ideas will be appreciated.

omg I forgot to sync button toggle state :)
I had to add this in my MainActivity() :
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

Related

AppCompat v7-r23. Home button in Toolbar doesn't show by default

I want to show Home (Hamburger) button by default when I open MyActivity. Unfortunately I don't see any buttons in the left top corner. But Home button appears after I open and close my Drawer.
I use the latest appcompat-v7:23.0.1 library:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
My activity:
public class MyActivity extends AppCompatActivity{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
}
mTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.hello_world,
R.string.app_name)
{
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
toolbar.setTitle(mTitle);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//toolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
activity_drawer.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
style="#style/MatchParent">
<!-- Your normal content view -->
<LinearLayout
style="#style/MatchParent"
android:orientation="vertical">
<include layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<FrameLayout
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start">
<include layout="#layout/navigation_drawer" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
I've tried the following variant to show Home button by default:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
But it doesn't work also.
For example, if I set only:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then Up (arrow) button shows correctly when I open activity and transform to the Home button after I open and close my Drawer.
What could be the problem?
Thanks in advance.
If you aren't calling syncState from your Activity's onPostCreate or not calling through to onConfigurationChanged or onOptionsItemSelected corresponding to your Activity callbacks, you should do.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#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 (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}

Android don't see ActionBarDrawerToggle

I am trying to add DrawerLayout to my app. Here is my layout:
<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">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:listSelector="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
And activity code:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setupNavigationDrawer();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
private void setupNavigationDrawer() {
DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.string.hello_world,
R.string.hello_world
) {
public void onDrawerClosed(View view) {
//Snackbar.make(view, R.string.drawer_close, Snackbar.LENGTH_SHORT).show();
}
public void onDrawerOpened(View drawerView) {
//Snackbar.make(drawerView, R.string.drawer_open, Snackbar.LENGTH_SHORT).show();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
I can swipe from the left side and see my drawer menu, but I don't see any ActionBar button to the left of activity title that toggles drawer menu. How can I add something like a "hamburger" button to my activity?
Have you tried to put following statement:
//calling sync state is necessary or else your hamburger icon won't show up
mDrawerToggle.syncState();
I have set following ic_menu (attached png image so may you not able to see) drawable as indicator... It works for me perfectly.
/**
* Setting of Actionbar
*/
private void setupToolbar() {
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
}
See following function:
/**
* In case if you require to handle drawer open and close states
*/
private void setupActionBarDrawerToggle() {
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
//Snackbar.make(view, R.string.drawer_close, Snackbar.LENGTH_SHORT).show();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
//Snackbar.make(drawerView, R.string.drawer_open, Snackbar.LENGTH_SHORT).show();
}
};
//Setting the actionbarToggle to drawer layout
mDrawerLayout.setDrawerListener(mDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
mDrawerToggle.syncState();
}
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
Drawer.setDrawerListener(mDrawerToggle);// use this line to set Drawer toggle
You would be better off using NavigationView, from the support package. Much easier to use, less code, etc.
http://android-developers.blogspot.no/2015/05/android-design-support-library.html
http://antonioleiva.com/navigation-view/
https://developer.android.com/reference/android/support/design/widget/NavigationView.html

Navigation drawer on lollipop is empty after adding Toolbar

I had a navigation drawer working a while back, but then I decided to update it to use a Toolbar instead of the default support action bar. Now, I have a situation where when I click the hamburger menu, the navigation drawer doesn't open. However, I can slide from left to right and open it. The problem is, it's now empty.
values-v21/styles.xml
<style name="Material" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/app_green</item>
<item name="colorPrimaryDark">#color/app_green_dark</item>
<item name="android:textColorPrimary">#color/action_bar_text</item>
<item name="android:textColor">#color/secondary_text_color</item>
<item name="android:color">#color/secondary_text_color</item>
<item name="android:colorAccent">#color/app_green</item>
<item name="android:editTextColor">#color/secondary_text_color</item>
<item name="textHeaderMaxLines">#integer/text_header_max_lines</item>
<item name="trackAbstractMaxLines">#integer/track_abstract_max_lines</item>
<item name="activatableItemBackground">#drawable/activatable_item_background</item>
<!-- ActionBar Styles -->
<item name="android:windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="android:windowActionBar">false</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">?android:attr/colorPrimaryDark</item>
<!-- Global UI Assignments -->
<item name="android:spinnerStyle">#style/Material.Widget.Spinner</item>
<item name="android:buttonStyle">#style/Material.Widget.Button</item>
<item name="android:checkboxStyle">#style/Material.Widget.Checkbox</item>
<item name="android:textAppearance">#android:style/TextAppearance</item>
<item name="android:popupWindowStyle">#style/Material.Window.Popup</item>
<!-- ViewPager -->
<item name="vpiCirclePageIndicatorStyle">#style/Material.Activity.Login.ViewPagerIndicator.CustomCircle</item>
<item name="buttonBarStyle">?android:buttonBarStyle</item>
<item name="buttonBarButtonStyle">?android:buttonBarButtonStyle</item>
<item name="indeterminateProgressStyle">?android:indeterminateProgressStyle</item>
</style>
navigation_drawer.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- The main content view. Must be first child because XML ordering implies stacking
context -->
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<!-- Our App Bar. Placed here so we can draw over it with the navigation drawer. -->
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary" />
<!-- Our actual content view. -->
<FrameLayout
android:id="#+id/drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
<!-- The navigation drawer itself. -->
<ListView
android:id="#+id/drawer_list"
android:layout_width="300dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_gravity="start"
android:background="#android:color/white"
android:fitsSystemWindows="true"
/>
NavigationDrawerActivity.java
public class NavigationDrawerActivity extends ActionBarActivity
implements AdapterView.OnItemClickListener {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private LayoutInflater mInflater;
private NavDrawerItemAdapter mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
// Our "App bar". This will only be populated if the current SDK is >= 21. Otherwise, we'll use an
// action bar that will be populated when the app first starts.
private Toolbar mAppBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setupNavigationDrawer();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#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 (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Toggles the state of the navigation drawer (i.e. closes it if it's open, and opens it if
* it's closed).
*/
public void toggleNavigationDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
closeNavigationDrawer();
} else {
openNavigationDrawer();
}
}
/**
* Opens the navigation drawer.
*/
public void openNavigationDrawer() {
mDrawerLayout.openDrawer(GravityCompat.START);
}
/**
* Closes the navigation drawer.
*/
public void closeNavigationDrawer() {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
/**
* Initializes items specific to the navigation drawer.
*/
private void setupNavigationDrawer() {
mDrawerLayout = (DrawerLayout) mInflater.inflate(R.layout.navigation_drawer, null); // "null" is important.
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.app_green));
// We want to use the toolbar if the version is 21 or greater, because is has more flexibility
// than the standard ActionBar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mAppBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mAppBar);
}
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* Our context (Activity that hosts this drawer) */
mDrawerLayout, /* The DrawerLayout where the nav drawer will be drawn */
mAppBar, /* The Toolbar that is being used as an app bar. */
R.string.drawer_open, /* Description of "open drawer", for accessibility */
R.string.drawer_close /* Description of "close drawer", for accessibility */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
supportInvalidateOptionsMenu();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);
NavDrawerGroup userToolsGroup = new NavDrawerGroup(R.string.drawer_group_usertools, false, 1);
NavDrawerGroup toolBoxGroup = new NavDrawerGroup(R.string.drawer_group_toolbox, true, 2);
NavDrawerGroup supportGroup = new NavDrawerGroup(R.string.drawer_group_support, true, 3);
NavDrawerItem[] navDrawerItems = {
new NavDrawerItem(R.string.drawer_item_main, R.drawable.ic_main, NavDrawerGroup.DEFAULT_GROUP),
new NavDrawerItem(R.string.drawer_item_tool_one, R.drawable.ic_tool_one, NavDrawerGroup.DEFAULT_GROUP),
new NavDrawerItem(R.string.drawer_item_tool_two, R.drawable.ic_tool_two, userToolsGroup),
};
mAdapter = new NavDrawerItemAdapter(this, navDrawerItems);
mDrawerList.setAdapter(mAdapter);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(this);
}
}
I know the NavDrawerItem stuff looks kinda wonky, but I have verified that it works with the old navigation drawer (pre-Toolbar), and all it is really doing is some logic to populate the ListView items with an icon and place them in an appropriate grouping. So, I am fairly confident that this is not the problem.
Even if it was, I should get something other than the empty white background, because the layout for the nav drawer actually sets a header for the ListView that has a green background, which I am not seeing.
Edit:
As per #blacksh33p's comments below, I removed the code that handled different API levels differently, and changed the mDrawerToggle constructor to:
Hm, no joy. I removed the conditional detecting the API level, and just initialized the Toolbar, and then changed the mDrawerToggle to the following:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* Our context (Activity that hosts this drawer) */
mDrawerLayout, /* The DrawerLayout where the nav drawer will be drawn */
mAppBar, /* The Toolbar that is being used as an app bar. */
R.string.drawer_open, /* Description of "open drawer", for accessibility */
R.string.drawer_close /* Description of "close drawer", for accessibility */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
supportInvalidateOptionsMenu();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
But, I end up with the same result. :(
Remove code from onCreate and add into setContentView
refer this DrawerActivity it may help..!!
public class DrawerActivity extends ActionBarActivity {
protected ActionBarDrawerToggle mDrawerToggle;
private String[] mModulesTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private Intent intent;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#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 boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
intent = new Intent(this, JoinLeagueActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
return true;
case R.id.action_add:
intent = new Intent(this, CreateLeagueActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
return true;
case R.id.contactUs:
FragmentManager fm = this.getSupportFragmentManager();
DialogFragment dialog = new ContactUsDialogFragment(); // creating new object
dialog.show(fm, "dialog");
return new ContactUsDialogFragment() != null;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void setContentView(final int layoutResID) {
View fullLayout = (DrawerLayout) getLayoutInflater().inflate(
R.layout.layout_drawer, null);
FrameLayout actContent = (FrameLayout) fullLayout
.findViewById(R.id.content_frame);
mModulesTitles = getResources().getStringArray(R.array.modules_array);
mDrawerLayout = (DrawerLayout) fullLayout
.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) fullLayout.findViewById(R.id.left_drawer);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(mModulesTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(mModulesTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(mModulesTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(mModulesTitles[3], navMenuIcons.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(mModulesTitles[4],-1));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(mModulesTitles[5], -1));
navDrawerItems.add(new NavDrawerItem(mModulesTitles[6], -1));
// Recycle the typed array
navMenuIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mModulesTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
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);
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
}
};
Log.e("DRAWERLAYOUT###", "" + mDrawerLayout);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getLayoutInflater().inflate(layoutResID, actContent, true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
super.setContentView(fullLayout);
}
public class DrawerItemClickListener implements
ListView.OnItemClickListener {
public DrawerItemClickListener() {
// TODO Auto-generated constructor stub
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mModulesTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
if (position == 0) {
Intent intent = new Intent(this, DashboardActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
if (position == 1) {
Intent intent = new Intent(this, LeagueActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
if (position == 2) {
Intent intent = new Intent(this, MatchupActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
if (position == 3) {
Intent intent = new Intent(this, HelpActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
if (position == 4) {
Intent intent = new Intent(this, PrivacyActivity.class);
intent.putExtra("from_register", false);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
if (position == 5) {
Intent intent = new Intent(this, TermsActivity.class);
intent.putExtra("from_register", false);
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
if (position == 6) {
SharedPreferenceUtil.putValue("loginStatus", false);
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
this.finish();
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
}
}
}
Since you're extending ActionBarActivity, there's no need for you to handle the API levels differently, you are able to use the support Toolbar down to API 7.
I think if you use the other ActionBarDrawerToggle constructor your code will work. Try this:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* Our context (Activity that hosts this drawer) */
mDrawerLayout, /* The DrawerLayout where the nav drawer will be drawn */
R.string.drawer_open, /* Description of "open drawer", for accessibility */
R.string.drawer_close /* Description of "close drawer", for accessibility */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
supportInvalidateOptionsMenu();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
I'm developing an app using drawerLayout with my own Toolbar, and it works so well on my device (android 5.0.1). To do that, I have a xml layout like that:
<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
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#F78B1E"
android:minHeight="?attr/actionBarSize" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<!-- 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_navigation"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

Change Icon Of Navigation Drawer

I am having trouble with changing my navigation drawer icon to a custom one. I've currently had to implement the standard drawer icon which has 3 horizontal lines on top, but now I want to replace this with my custom drawer icon.
This is how my mDrawerToggle is at the moment:
mDrawerToggle=new ActionBarDrawerToggle(this,
mDrawerLayout,
R.drawable.app_icon,
R.string.drawer_open) {
// My code
};
Use below code,it's working for V7 ActionBarDrawerToggle
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.your_custom_icon);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
});
Here is the sample code taken from
Creating a Navigation Drawer
Activity.class
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
...
public void onCreate(Bundle savedInstanceState) {
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#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 (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
...
}
This is main activity
final 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);
toggle.setDrawerIndicatorEnabled(false);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});
toggle.setHomeAsUpIndicator(R.drawable.menuicon);
You could use this format for your mDrawerToggle:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.CUSTOM_ICON, // Navigation menu toggle icon
R.string.DRAWER_OPEN, // Navigation drawer open description
R.string.DRAWER_CLOSE // Navigation drawer close description
)
Change your drawable and make sure it is the same name as the one in the code.
This is the main layout file
<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" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:choiceMode="singleChoice"
android:divider="#color/black"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
This is the main Activity
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.menuicon, // nav menu toggle icon
R.string.app_name, // nav drawer open - description for
// accessibility
R.string.app_name // nav drawer close - description for
// accessibility
) {
public void onDrawerClosed(View view) {
// getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
finally at R.drawable.menuicon(you can give your image id) it will work.

NavigationDrawer is not working

I have read the documentation for NavigationDrawer and tried to create it in my own application. But there is a trouble: the ListView that should be used as menu just floats over the main content and I can't perform any actions with it (e.g. close by swipe). What is the problem?
<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:id="#+id/mainPageLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
android:id="#+id/mainScreenViewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/navigationDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:gravity="start" >
</ListView>
</android.support.v4.widget.DrawerLayout>
And the drawer initialization:
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
Thanks.
Here is a rough sample of codes that work for me, they're a combination of things from android developers site and StylingAndroid blog:
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layoutxml_id);
// must initialize mDrawerLayout and mDrawerToggle in main thread
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// do something
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// do something
}
};
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if(item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
return super.onOptionsItemSelected(item);
}

Categories

Resources