Created
October 22, 2014 10:39
Toolbar Animation with Navigation Drawer
This file contains hidden or 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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:minHeight="?attr/actionBarSize" /> | |
<android.support.v4.widget.DrawerLayout | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<!-- The main content view --> | |
<FrameLayout | |
android:id="@+id/content_frame" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<!-- The navigation drawer --> | |
<ListView | |
android:id="@+id/left_drawer" | |
android:layout_width="240dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
android:background="#111" | |
android:choiceMode="singleChoice" | |
android:divider="@android:color/transparent" | |
android:dividerHeight="0dp" /> | |
</android.support.v4.widget.DrawerLayout> | |
</LinearLayout> |
This file contains hidden or 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"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.sample.myapplication" > | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:theme="@style/Theme.AppCompat.Light.NoActionBar" | |
android:name="com.sample.myapplication.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or 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
package com.sample.myapplication; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.widget.Toolbar; | |
import android.view.MenuItem; | |
import android.view.View; | |
public class MainActivity extends ActionBarActivity { | |
private DrawerLayout mDrawerLayout; | |
private ActionBarDrawerToggle mDrawerToggle; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); | |
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, | |
toolbar, R.string.drawer_open, R.string.drawer_close) { | |
/** Called when a drawer has settled in a completely closed state. */ | |
public void onDrawerClosed(View view) { | |
super.onDrawerClosed(view); | |
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() | |
} | |
/** Called when a drawer has settled in a completely open state. */ | |
public void onDrawerOpened(View drawerView) { | |
super.onDrawerOpened(drawerView); | |
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() | |
} | |
}; | |
// Set the drawer toggle as the DrawerListener | |
mDrawerLayout.setDrawerListener(mDrawerToggle); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
getSupportActionBar().setHomeButtonEnabled(true); | |
} | |
@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); | |
} | |
} | |
This file contains hidden or 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
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light"> | |
<!-- Customize your theme here. --> | |
</style> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is very helpful to me. Thanks :)