Toggle DrawerLayout with title only (NO App Icon)?

I have activity with DrawerLayout . I can open the box in two different ways ... by scrolling the left side of the screen to the right and clicking on the application title. I do NOT see the application icon, only the title. I implemented it exactly as Google recommended here: Creating a navigation box: open and close using the application icon

Everything works functionally, opening and closing the box itself. However, it does not display the standard DrawerLayout icon that is intended to be used. Instead, I get the usual caret (it looks like a smaller sign).

As soon as I add the application icon back to the ActionBar , it will start working as expected. The drawer layout icon displays and enlivens when the drawer is open or closed. I tried to remove the application icon both in the XML stylesheet and programmatically.

Is there a way to get the DrawerLayout icon without the application icon ???

UPDATE: I found a job, but this hack is more than a solution. I just created a transparent 1x1 pixel PNG (blank.png) and set it as my application icon in the styles.xml file. Below is all the relative code:

styles.xml

 <style name="MyCustomTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyCustomActionBar</item> <item name="android:icon">@drawable/blank</item> </style> <style name="MyCustomActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:displayOptions">showHome|showTitle|homeAsUp</item> </style> 

MainActivity → onCreate ()

 this.navDrawerToggle = new ActionBarDrawerToggle ( this, this.navDrawerLayout, R.drawable.icon_nav_drawer, R.string.nav_drawer_open, R.string.nav_drawer_closed ) { public void onDrawerClosed(View view) {} public void onDrawerOpened(View drawerView) {} }; 

MainActivity → onPostCreate ()

 super.onPostCreate(savedInstanceState); this.navDrawerToggle.syncState(); 

MainActivity → onResume ()

 this.navDrawer.setOnItemClickListener(new DrawerItemClickListener()); this.navDrawerLayout.setDrawerListener(this.navDrawerToggle); 

MainActivity → onPause ()

 this.navDrawer.setOnItemClickListener(null); this.navDrawerLayout.setDrawerListener(null); 

MainActivity → onConfigurationChanged (Configuration newConfig)

 super.onConfigurationChanged(newConfig); navDrawerToggle.onConfigurationChanged(newConfig); 

MainActivity → onOptionsItemSelected (MenuItem element)

 if (this.navDrawerToggle.onOptionsItemSelected(item)) {return true;} else { // A bunch of item click handling happens here... return true; } 
+6
source share
3 answers

getActionBar().setIcon(android.R.color.transparent);

It worked for me .....;)

0
source

I was curious, so I tried it with a sample for DrawerLayout, and it worked great. In addition, it seems that you need to use your own drawer icon, it is recommended anyway. You can download default assets from this site Create a navigation box and place them in your corresponding folder for the resource.

Here is what worked for me:

 ActionBar actionBar = getActionBar(); // Let get rid of the app icon here actionBar.setIcon(null); actionBar.setTitle("App Name"); // Setting these 3 options allows us to show the title as well as a "Home" elements // "Home" elements include the Up and/or Drawer icon. The DISPLAY_HOME_AS_UP will enable // and show the Drawer icon, we'll be "replacing" the "up" button with the drawer icon below actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP); // Get a reference of the DrawerLayout DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); drawerLayout.setDrawerListener(drawerToggle); // Setting ActionBarDrawerToggle will allow you to set the drawables for the drawer // (this will also give you the nice/smooth animation) as well as allow you to do some // other things depending on the events: onDrawerClosed & onDrawerOpened. ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle( this, /* host Activity */ drawerLayout, /* 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_closed /* "close drawer" description for accessibility */ ) { public void onDrawerClosed(View view) { actionBar.setTitle("Closed..."); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { actionBar.setTitle("Open..."); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set a listener to be notified of drawer events. drawerLayout.setDrawerListener(drawerToggle); 

UPDATE: It seems that android: displayOptions in ActionBar style are not counted. Use actionBar.setDisplayOptions (int parameters) instead.

+13
source

After setting the drawer switch, you need to call the following method:

  mDrawerToggle.syncState(); 

so your code will look like this:

  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) { actionBar.setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { actionBar.setTitle("Preview Mode"); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerToggle.syncState(); mDrawerLayout.setDrawerListener(mDrawerToggle); 
0
source

Source: https://habr.com/ru/post/947520/


All Articles