Last active
December 19, 2015 17:06
-
-
Save kartikarora/0e6bccc5e9331d6656dc to your computer and use it in GitHub Desktop.
Theme and Layout for Toolbar as ActionBar and add a Navigation Drawer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBar; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.MenuItem; | |
import android.os.Bundle; | |
public class Activity extends AppCompatActivity { | |
DrawerLayout mDrawerLayout; | |
Toolbar mToolbar; | |
ActionBarDrawerToggle mDrawerToggle; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.toolbar); | |
mToolbar = (Toolbar) findViewById(R.id.toolbar_for_drawer); | |
setSupportActionBar(mToolbar); | |
ActionBar actionBar = getSupportActionBar(); | |
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); | |
mDrawerToggle = new ActionBarDrawerToggle(Activity.this, mDrawerLayout, mToolbar, R.string.drawer_opened, R.string.drawer_closed); | |
mDrawerLayout.setDrawerListener(mDrawerToggle); | |
if (actionBar != null) { | |
actionBar.setTitle(R.string.title_activity_main); | |
actionBar.setHomeButtonEnabled(true); | |
} | |
} | |
@Override | |
protected void onPostCreate(Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
mDrawerToggle.syncState(); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (mDrawerToggle.onOptionsItemSelected(item)) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onBackPressed() { | |
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { | |
mDrawerLayout.closeDrawers(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<FrameLayout | |
android:id="@+id/container_frame" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<FrameLayout | |
android:id="@+id/drawer_frame" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</android.support.v4.widget.DrawerLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style name="DrawerLayoutTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<item name="colorPrimary">@color/primary</item> | |
<item name="colorPrimaryDark">@color/primary_dark</item> | |
<item name="colorAccent">@color/accent</item> | |
<item name="windowActionBar">false</item> | |
<item name="android:divider">@color/divider</item> | |
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:orientation="vertical" | |
android:layout_height="match_parent"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar_for_drawer" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
android:background="?attr/colorPrimary" | |
android:minHeight="?attr/actionBarSize" | |
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> | |
<include | |
android:id="@+id/drawer_layout" | |
layout="@layout/activity_layout" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment