I was trying to implement the android.support.v4.app.ActionBarDrawerToggle in my app; since this class is deprecated
This class is deprecated. Please use ActionBarDrawerToggle in
support-v7-appcompat.
I've switched to android.support.v7.app.ActionBarDrawerToggle.
Before I could call the constructor in this way:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
but after I've switched to the newer v7 support library, I receive the error
"ActionBarDrawerToggle() in ActionBarDrawerToggle cannot be applied to:
toolbar: android.support.v7.widget.Toolbar
Actual arguments: R.drawable.ic_drawer (int)"
Apparently I am not introducing a proper Toolbar into the constructor, but I'm not sure to understand the difference between the two conflicting arguments. How do I get the required toolbar?
I solved my problem by importing the newer android.support.v7.app.ActionBarDrawerToggle and by using the RecyclerView instead of the ListView as shown in this example: How to make Material Design Navigation Drawer With Header View:
private ActionBarDrawerToggle mDrawerToggle;
//... ...
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.drawer_open, R.string.drawer_close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// code here will execute once the drawer is opened
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
// Code here will execute once drawer is closed
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
};
If you still have trouble check here:
How to replace deprecated android.support.v4.app.ActionBarDrawerToggle
Related
Apparently v4 api looks like this
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
But it has been depreciated in latest SDK. The new v7 API has constructor like this
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)
There is no parameter which takes in custom drawable resource file (Hamburger menu). How can I customize it? I tried setting navigationIcon of the toolbar but it kinds of disable navigation drawer itself. (Nothing happens when you click it).
I'm trying to make a simple app with Navigation Drawer with Android Studio.
Since Android Studio has this type of activity already implemented, i used it.
But now i'm unable to change the resource of image button in the top left of the ActionBar that opens the drawer.
Here's a part of my NavigationDrawerFragment.java :
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
this image could help you to understand my problem
Thanks
Use this code
actionBar.setHomeAsUpIndicator(R.drawable.menuw_24);
I strongly suggest that you use the Design Support library, which provides a NavigationView to create a Material Design Navigation Drawer. Bottom line is that it is dead simple to create one. Don't struggle with the template.
This should get you started.
I have Create a new android app with the Navigation Drawer Activity.
When i execute the app, the icon on the right corner is showing the back Arrow but it has to be the 3 line's icon.
The icon R.drawable.ic_drawer is this
In NavigationDrawerFragment class.
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
)
When i execute the App i'm getting Arrow icon on the right.
After the selection also the Icon is same.
how do i change it now ?
Hope this helps, I solved the above problem this way. #shkschneider is right. To make your code work import android.support.v7.app.ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
Sorry I'm little late but this might help other people also.
You just need to add the below line and it will work fine. :)
mDrawerToggle.setDrawerIndicatorEnabled(false);
The question says it all. I know how to implement a drawer with the drawerlayout, but i have no idea how to place a button next to the drawer (to show the drawer) that moves along with the drawer.
You have to use ActionBarDrawerToggle.
ActionBarDrawerToggle myDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, R.drawable.my_icon_for_drawer_toggle,
R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
myDrawerLayout.setDrawerListener(myDrawerToggle);
I think what you want is sliding menu.
Look at this Github project which also provides example code for it
https://github.com/jfeinstein10/SlidingMenu
Hope this helped!
I'm trying to change my action bar's home as up indicator. Right now it shows an arrow (when opened) and 3 stripes (when closed):
I want to replace this arrow with a custom drawable. The code below doesn't works:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setLogo(getResources().getDrawable(R.drawable.actionbar_logo));
getSupportActionBar().setDisplayUseLogoEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.app_name, /* "open drawer" description for accessibility */
R.string.app_name /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
//getActionBar().setTitle(mTitle);
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle(mDrawerTitle);
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.actionbar_logo));
mDrawerLayout.setDrawerListener(mDrawerToggle);
If I use <item name="displayOptions">useLogo|showHome|showTitle</item> at the style, it shows my custom drawable besides the original homeAsUpIndicator, while what I want is to replace it
How can I achieve this? Thanks!