Hi, I would like to see only the Hamburger icon but my app crashes. I used this theme Theme.AppCompat.Light.DarkActionBar.
how can I do?
Thanks.
@davix10 post your logcat
With such a few info we can't help you.
I'm sorry @AngleV, this is my logcat:
Process: com.example.torch.livetools, PID: 11706
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.torch.livetools/com.example.torch.livetools.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.setDrawerIndicatorEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2431)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5309)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.setDrawerIndicatorEnabled(boolean)' on a null object reference
at com.example.torch.livetools.MainActivity.onCreate(MainActivity.java:128)
at android.app.Activity.performCreate(Activity.java:6865)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2431)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.access$900(ActivityThread.java:154)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Handler.dispatchMessage(Handler.java:102)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Looper.loop(Looper.java:135)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.main(ActivityThread.java:5309)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invoke(Native Method)
@davix10
it seems that your drawer has not been initialized before you call the method.
Take a look here how to implement it
https://github.com/mikepenz/MaterialDrawer#code---show-the-hamburger-icon
@AngleV
I used this
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
but it doesn't work.
@davix10 as i said before make sure you have initialize the drawer
//Create the drawer
result = new DrawerBuilder()
.withActivity(this)
.........
@AngleV This is my code:
result1 = new DrawerBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withSavedInstance(savedInstanceState)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withName(R.string.drawer_item_free_play).withIcon(FontAwesome.Icon.faw_gamepad),
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog),
new SecondaryDrawerItem().withName(R.string.drawer_item_help).withIcon(FontAwesome.Icon.faw_question).withEnabled(false),
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github),
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_bullhorn)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem instanceof Nameable) {
Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
}
return false;
}
}).build();
// getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result1.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@davix10
remove this .withActionBarDrawerToggle(false)
@AngleV
I tried but it still does not work
@davix10
working example
result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.build();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
the above gives you the hamburger icon
But I do not have the toolbar, how should I do?
@davix10
Does your activity extends AppCompatActivity ?
@AngleV
Yes
@AngleV thanks for helping.
@davix10 this is only handled when using a Toolbar, if you do not use one please look up google for a general description on how you can achieve this with a DrawerLayout.
as far as i have found the ActionBarDrawerToggle can only be used with Toolbars
@mikepenz
You can not replace the arrow icon with the hamburger icon?
@davix10 you can as mentioned. but not via the ActionBarDrawerToggle
Take a closer look in the ActionBarActvitiy of the sample app. i think you can modify this by changing one of those two lines
https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/ActionBarDrawerActivity.java#L57
But i highly recommend that you switch to a Toolbar it's better and allows you so much more flexibility, and it is implemented within 5 minutes
However through the toolbar it is good only for lollipop and later, right?
No toolbar is compatible down to API 7. The MaterialDrawer just down to API 10
Is there an example to switch to a toolbar?
Just look at the sample application. It's really easy
Thanks!
Great job!
I want the Account Header to have a rating bar just below the name . Is it possible
@VectorSmS your question does not fit into this thread. You can theoretically provide a custom layout for the header. there were already similar questions