Skip to content

Instantly share code, notes, and snippets.

@SyllaJay
Created March 15, 2015 14:21
Show Gist options
  • Save SyllaJay/ebfb7848aa162fa4eacd to your computer and use it in GitHub Desktop.
Save SyllaJay/ebfb7848aa162fa4eacd to your computer and use it in GitHub Desktop.
Toolbar
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:theme="@style/Theme.Toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">
</FrameLayout>
<ListView
android:id="@+id/left_drawer_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_red_light" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">@android:color/holo_orange_dark</color>
<color name="primaryDark">@android:color/holo_orange_light</color>
</resources>
package com.tryear.jay.toolbarmaterialdesign;
import android.content.Context;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Context mContext = this;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView list;
private String[] listContent = new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
"Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12",};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
list = (ListView) findViewById(R.id.left_drawer_listview);
list.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, listContent));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, "Selected Item " + (position + 1), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(list);
}
});
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
syncState();
}
};
drawerLayout.setDrawerListener(drawerToggle);
// drawerToggle.syncState();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == android.R.id.home) {
if (drawerLayout.isDrawerOpen(list)) {
drawerLayout.closeDrawer(list);
} else {
drawerLayout.openDrawer(list);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(list)) {
drawerLayout.closeDrawer(list);
} else {
super.onBackPressed();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
<item name="android:navigationBarColor">@color/primary</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment