I'm going crazy trying to figure out what I'm doing wrong and tried all the solutions on SO but still seem to be missing something. This is the first time I am creating a navigation drawer and took the help of an online tutorial.
Everything works great, except the icon shows the back arrow instead of the three lines.
The first bunch of answers deal with the v4 library tied to the drawable icon which didn't apply to me since I am using the v7 library
On the v7, the first answer I tried was to use below, but still remained as arrow.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
I also tried this trick, but same result:
drawerLayout.post(new Runnable() {
#Override
public void run() {
drawerToggle.syncState();
}
I then decided to give up and try to just force the icon and downloaded the ic_drawer icon and used below to just set it and be done, but still no go:
toolbar.setNavigationIcon(R.drawable.ic_drawer);
I then went so far as to remove everything tied to making the icon to start from scratch, but even after making my code just as below, the back arrow still persists and clicking it opens the navigation drawer:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
};
Even if I comment out the toolbar and that drawer toggle to have nothing in my onCreate, I am now still stuck with that back arrow. The navigation drawer still works great and opens when I click on it. I just don't know where that back arrow is coming from so I can kill it and try to work back the other code to get the three lines to show up.
Any suggestion would be greatly appreciated!
Below is the code in full. With all the navigation drawer stuff commented out, I can still see the back arrow, and pressing it or swiping opens the fragment perfectly. If I take the comments off, its still the same thing
public class Main extends AppCompatActivity {
public ArrayList<String> strTrips = new ArrayList<String>();
ArrayAdapter<String> adpTrips;
ActionBarDrawerToggle drawerToggle;
//String[] strDefaultList={"Click the '+' on the toolbar to add a new trip","","Once you have at least one trip, you select it to view/enter information"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//toolbar.setNavigationIcon(R.drawable.ic_drawer);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/* DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getActivity().invalidateOptionsMenu();
//drawerToggle.syncState();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
//getActivity().invalidateOptionsMenu();
// drawerToggle.syncState();
}
};*/
/* drawerLayout.addDrawerListener(drawerToggle);
*/
//drawerToggle.syncState();
/* drawerLayout.post(new Runnable() {
#Override
public void run() {
//drawerToggle.syncState();
}
});*/
subLoadTrips();
}
/*
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//drawerToggle.syncState();
}
*/
The manifest file is below. The activity where I am adding the NavigationDrawer is 'Main'.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dimbu.tripx"
android:versionCode="10"
android:versionName="1.0">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name=".AddTrip">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Main" />
</activity>
</application>
</manifest>
This one line code should help you. 😎☺
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The answer was provided by Mike M in comments.
With all the code being correct, his suggestion to clean/rebuild the project worked and then the changes took effect!
toolbar is not your app toolbar here!
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
It's just a view. Instead of this you should get current app toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_drawer);
Related
I want to show an ActionBar in my Activity while only showing a closing symbol. Therefore I used the method getSupportActionBar():
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
setTitle("Add Event");
But it throws a NullPointerExeption because apparently there's no ActionBar to call:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial/com.example.tutorial.AddEventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setHomeAsUpIndicator(int)' on a null object reference
In my MainActivity I implemented a Drawer Navigation where I already used a toolbar that works:
public class MainActivity extends AppCompatActivity {
protected DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
// Make sure to be using androidx.appcompat.app.ActionBarDrawerToggle version.
private ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// This will display an Up icon (<-), we will replace it with hamburger later
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Find our drawer view
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Setup toggle to display hamburger icon with nice animation
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
// Tie DrawerLayout events to the ActionBarToggle
mDrawer.addDrawerListener(drawerToggle);
// Find our drawer view
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
}
How do I fix that?
Thanks in advance!
Replace this line :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
with this line :
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
This should solve your problem.
You may try to add following attribute to your MainActivity item under your ActivityManifest.xml
android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
Then try to access it with the getSupportActionBar(). With this way you can choose the activities that you don't or do want to include actionbars by using relevant themes. For example:
<activity
android:name=".SecondActivity"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
android:parentActivityName=".MainActivity" />
I am using mikepenz drawer library but I want to change default humburger icon and back arrow icon with my own drawable icon.
I have tried many times but I am unable to change the icon with my own icon .
Can anyone help me ?
new DrawerBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withToolbar(toolbar)
.addDrawerItems(
//pass your items here
)
.build();
CODE TO SHOW THE HUMBURGER ICON:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
following is the code I found many times but i tried this also but it did not work
Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
And when I am searching I also come to know that you can not change the icon if you passing the toolbar in drawer builder so can anyone tell me what can I do?
I haven't tried it with that library but, try the following:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
As per this link, you need to remove the withToolbar() from the DrawerBuilder and then you will have to handle open/close completely on your own.
For that you can do some thing like that
protected void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
...
}
Also you had to add a toolbar navigation click listener to listen for click events on the custom drawer icon.
protected void onCreate(Bundle savedInstanceState) {
...
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
...
}
You can update the icon dynamically whenever required as
toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);
Hope this will help you.
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
String Drawer_Open,Drawer_Close;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set it button icon
getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
//set it makes button Clickble
getSuppotActionBar().setHomeButtonEnabled(true);
//set your own icon by using this code
getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
drawerLayout.serDrawerListener(actionBarDrawerToggle);
}
}
Again Do you have any quires get Consult me here.....,hope you got solution to your problem...
Try this by modify following:
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
to
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
this disable library default icon then change the icon...
getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);
I know a lot of questions are posted regarding this topic, but I can't find a workable solution.
I want the onBackPressed() to be called when I press the left arrow in the tootbar.
I'm using appcompat-v7:23:1:0.
The left arrow is working everywhere in my app but here. I suspect it is linked to the fact I start my activity from a fragment.
Activity_A > starts Fragment_A (extends SupportMapFragment) > which starts Activity_B.
I want to go back on Fragment_A when the back arrow is pressed from Activity_B.
For the moment, the arrow doesn't respond to the click, onBackPressed is not called form the toolbar but it is when the hardware back button is pressed.
I've tried adding
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and I've added this in the manifest:
<activity
android:name=".activity.Activity_B"
android:windowSoftInputMode="stateHidden"
android:parentActivityName=".activity.Activity_A" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.Activity_A"
/>
</activity>
(Activities names are replaced for clarification)
It appears the problem was related to the xml layout of my activity_B
I have a scrollView below the toolbar. That scrollView had a android:layout_below="#id/contact_center" whose id doesn't exists in that activity.
I know it sounds strange but having gone back and forward with the code, I'm 100% sure that this was causing the onBackPress() to not respond.
So for people having similar problems, pay attention to your xml layout, there might be an error in there causing this strange issue. Also, be very varefull with copy-pasting code from other activities ;)
Thanks for the help.
add this in your onCreateOptionMenu
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
also add this
getSupportActionBar().setDisplayShowHomeEnabled(true);
Try this
public void setUpToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Toolbar Title");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "here", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
I have created new Android project with only 1 activity. I have created custom view for ActionBar. But when I launch the app for 1-2 seconds I see blank screen and default black Actionbar with app name on it. Only after second when content is loaded my action bar is shown.
How to remove this default action bar showing on start? Code is simple:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
ActionBar supportActionBar = getSupportActionBar();
supportActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
supportActionBar.setDisplayShowHomeEnabled(false);
supportActionBar.setDisplayShowTitleEnabled(false);
supportActionBar.setDisplayShowCustomEnabled(true);
supportActionBar.setCustomView(R.layout.actionbar);
MainActivity:
private ActionBar actionabar;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
actionabar = getActionBar();
actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
In Manifest:
Inside activity element you have to add these theme
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
For Eg:
<activity
android:name="com.sit.fth.activity.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
The setup
I have an activity whose contentView is an instance of a DrawerLayout, which has a navigation drawer with a drawer indicator displayed in the action bar. The activity contains a Fragment, let's call it ListFragment, which contains a list of options. When an option is clicked, I replace the ListFragment with a DetailFragment.
At this point, I would like to display an "up" navigation option instead of the navigation drawer indicator. I'm able to display the "up" icon if I disable the drawer indicator by calling mDrawerToggle.setDrawerIndicatorEnabled(false), but this only removes the drawer icon--it does not remove the functionality--that is, when I click the caret, the navigation drawer is still opened.
Additionally, in these subviews, I would like to disable the opening of the drawer by dragging from the edge of the screen. I have tried doing this by calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) but it doesn't seem to have disabled this functionality.
I have tried extending the ActionBarDrawerToggle class to prevent opening the drawer when the indicator is clicked--however, all that happens is that the overriding action (the "up" navigation) is performed, but the drawer still opens.
I have also implemented the steps in Switching between Android Navigation Drawer image and Up caret when using fragments . It works insofar as displaying the caret goes, but despite overriding the up button functionality, the menu still opens (the app does navigate back--it just also opens the drawer).
Question
So, long story short: is there any (preferably clean and elegant, but at this point I'll go with hacky) way to achieve these things when my layout root is a DrawerLayout:
Replace the drawer indicator with an "up" caret (tentatively doable via mDrawerToggle.setDrawerIndicatorEnabled(false))
Prevent the drawer from opening when the caret is clicked, and instead override with my own "up" functionality
Prevent the drawer from opening when I drag from the edge of the screen.
Edit
All right, it looks like if I both override ActionBarDrawerToggle AND onOptionsItemSelected, the menu does not open when I click the caret. But it still opens if I drag from the edge. Help!
Short Code
public void setDrawerState(boolean isEnabled) {
if ( isEnabled ) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.syncState();
}
}
This is only part of the solution that I arrived at, but it was quite hard to figure out this bug, so I'm leaving this here for posterity's sake.
This how I was defining the ListView for my navigation drawer:
<ListView
android:id="#+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start|bottom"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
Even after calling setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) I was still able to slide the drawer open.
However, after changing the layout_gravity to "start" this problem seems to be resolved.
I was able to reproduce this issue in a sample, navigation-drawer-only app, so it does appear to be a reproducible issue not unique to my situation.
Building on sonida's answer. After calling setDrawerIndicatorEnabled(false), onNavigateUp wasn't being called still. So, I just created a new onClickListener that called it:
public void setDrawerState(boolean isEnabled) {
if ( isEnabled ) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onSupportNavigateUp();
}
});
drawerToggle.syncState();
}
}
also I think
drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED);
has been depreciated, but it works fine without it.
You need to disable swipe and disable the actionbar home button:
Use the below code that builds on the code already given to disable swipe
public void setDrawerState(boolean isEnabled) {
if ( isEnabled ) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
getActivity().getActionBar().setHomeButtonEnabled(true);
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
getActivity().getActionBar().setHomeButtonEnabled(false);
}
}
Building on answer by #sonida And after using the tweaks given by #luca992 and #jai.
I tried above suggested codes But the "up" or "Back" arrow in left side of action bar was just not showing up in my app. But luckily I was able to fix that.
I had to add this extra line of code in setNavigationDrawerState() [Ref: android.support.v7.app.ActionBarDrawerToggle.setHomeAsUpIndicator ]
toggle.setHomeAsUpIndicator(R.drawable.ic_keyboard_backspace_white_24dp);
I downloaded the drawable: ic_keyboard_backspace_white_24dp from Material.io
Here is the complete code:
MainActivity.java -> onCreate()
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Start: Code automatically generated by Android Studio
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
// End: Code automatically generated by Android Studio
// I had to add this listener as the "back" arrow was totally unresponsive
// Thanks to #luca992
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
// Start: Code automatically generated by Android Studio
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// End: Code automatically generated by Android Studio
// More custom code for other stuff
// ...
}
MainActivity.java -> setNavigationDrawerState()
public void setNavigationDrawerState(boolean isEnabled) {
if ( isEnabled ) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
}
else {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
toggle.setDrawerIndicatorEnabled(false);
// the extra line of code goes here
toggle.setHomeAsUpIndicator(R.drawable.ic_keyboard_backspace_white_24dp);
toggle.syncState();
}
MainActivity.java -> onBackPressed()
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if(getSupportFragmentManager().getBackStackEntryCount() > 0){
getSupportFragmentManager().popBackStack();
}else {
super.onBackPressed();
}
}
MainActivity.java -> startFragment() [dummy function for example]
public void startFragment(){
MyFrag myFrag = new MyFrag();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frag_container ,myFrag)
.addToBackStack(null)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
MyFrag.java --> onViewCreated()
#Override
public void onViewCreated (View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
// Say, using an implemented interface Make call to MainActivitiy's setNavigationDrawerState() passing false
// setNavigationDrawerState(false)
// ...
}
MyFrag.java --> onDestroyView()
#Override
public void onDestroyView(){
// Say, using an implemented interface Make call to MainActivitiy's setNavigationDrawerState() passing true
// setNavigationDrawerState(true)
super.onDestroyView();
}