Reputation: 4413
I want to show Home (Hamburger) button by default when I open MyActivity. Unfortunately I don't see any buttons in the left top corner. But Home button appears after I open and close my Drawer.
I use the latest appcompat-v7:23.0.1 library:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
My activity:
public class MyActivity extends AppCompatActivity{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
}
mTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.hello_world,
R.string.app_name)
{
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
toolbar.setTitle(mTitle);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//toolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
activity_drawer.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
style="@style/MatchParent">
<!-- Your normal content view -->
<LinearLayout
style="@style/MatchParent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<FrameLayout
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start">
<include layout="@layout/navigation_drawer" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
I've tried the following variant to show Home button by default:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
But it doesn't work also.
For example, if I set only:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
then Up (arrow) button shows correctly when I open activity and transform to the Home button after I open and close my Drawer.
What could be the problem?
Thanks in advance.
Upvotes: 1
Views: 270
Reputation: 12523
If you aren't calling syncState from your Activity's onPostCreate or not calling through to onConfigurationChanged or onOptionsItemSelected corresponding to your Activity callbacks, you should do.
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
Upvotes: 1