Por fin después de tanto luchar hemos encontrado la contestación de esta obstáculo que agunos usuarios de este sitio web tienen. Si tienes algo más que aportar no dejes de aportar tu comentario.
Solución:
Si asumo que estás usando android.support.v4.widget.DrawerLayout
en su diseño, este enfoque puede funcionar para usted; Solo he probado en API 21
pero dado que utiliza principalmente las bibliotecas de soporte, deberían trabajar (últimas palabras famosas) en objetivos más bajos o más altos.
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v4.widget.DrawerLayout
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawerLayout;
private boolean mToolBarNavigationListenerIsRegistered = false;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Get DrawerLayout ref from layout
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
// Initialize ActionBarDrawerToggle, which will control toggle of hamburger.
// You set the values of R.string.open and R.string.close accordingly.
// Also, you can implement drawer toggle listener if you want.
mDrawerToggle = new ActionBarDrawerToggle (this, drawerLayout, mToolbar, R.string.open, R.string.close);
// Setting the actionbarToggle to drawer layout
drawerLayout.addDrawerListener(mDrawerToggle);
// Calling sync state is necessary to show your hamburger icon...
// or so I hear. Doesn't hurt including it even if you find it works
// without it on your test device(s)
mDrawerToggle.syncState();
/**
* To be semantically or contextually correct, maybe change the name
* and signature of this function to something like:
*
* private void showBackButton(boolean show)
* Just a suggestion.
*/
private void enableViews(boolean enable)
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,
// when you enable on one, you disable on the other.
// And as you may notice, the order for this operation is disable first, then enable - VERY VERY IMPORTANT.
if(enable)
//You may not want to open the drawer on swipe from the left in this case
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
if(!mToolBarNavigationListenerIsRegistered)
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// Doesn't have to be onBackPressed
onBackPressed();
);
mToolBarNavigationListenerIsRegistered = true;
else
//You must regain the power of swipe for the drawer.
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Remove back button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
mDrawerToggle.setToolbarNavigationClickListener(null);
mToolBarNavigationListenerIsRegistered = false;
// So, one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
La solución utiliza ActionBarDrawerToggle.setDrawerIndicatorEnabled
para alternar la visibilidad del icono de hamburguesa y ActionBar.setDisplayHomeAsUpEnabled
para la visibilidad de la Hasta botón, esencialmente haciendo uso de sus respectivos drawable
recursos.
Otros supuestos
- El tema de tu actividad se extiende
Theme.AppCompat.Light.NoActionBar
.
Encontré soluciones flexibles en la aplicación de Android Google I / O 2017.
public Toolbar getToolbar()
if (mToolbar == null)
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null)
setSupportActionBar(mToolbar);
mToolbar.setNavigationContentDescription(R.string.navdrawer_description_a11y);
mToolbarTitle = (TextView) mToolbar.findViewById(R.id.toolbar_title);
if (mToolbarTitle != null)
int titleId = getNavigationTitleId();
if (titleId != 0)
mToolbarTitle.setText(titleId);
// We use our own toolbar title, so hide the default one
getSupportActionBar().setDisplayShowTitleEnabled(false);
return mToolbar;
/**
* @param clickListener The @link android.view.View.OnClickListener for the navigation icon of
* the toolbar.
*/
protected void setToolbarAsUp(View.OnClickListener clickListener)
// Initialise the toolbar
getToolbar();
if (mToolbar != null)
mToolbar.setNavigationIcon(R.drawable.ic_up);
mToolbar.setNavigationContentDescription(R.string.close_and_go_back);
mToolbar.setNavigationOnClickListener(clickListener);
Entonces el uso es realmente simple.
setToolbarAsUp(new View.OnClickListener()
@Override
public void onClick(View v)
// onBackPressed();
// or navigate to parent or some other intent
);
puede cambiar el botón de la barra de acción con:
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_button);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);