Last active
August 29, 2015 14:14
-
-
Save darwind/3d31906ec637990c313b to your computer and use it in GitHub Desktop.
Implementing the new ToolBar instead of the ActionBar
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
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 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 xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/toolbar" | |
| android:layout_width="match_parent" | |
| android:layout_height="100dp" | |
| android:background="@android:color/holo_red_light" | |
| android:minHeight="100dp"> | |
| </android.support.v7.widget.Toolbar> | |
| </RelativeLayout> |
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.example.toolbarexample; | |
| import android.os.Bundle; | |
| import android.support.v7.app.ActionBarActivity; | |
| import android.support.v7.widget.Toolbar; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| public class MainActivity extends ActionBarActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
| if(toolbar != null) { | |
| setSupportActionBar(toolbar); | |
| getSupportActionBar().setTitle("My custom toolbar!"); | |
| getSupportActionBar().setHomeButtonEnabled(true); | |
| getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
| } | |
| } | |
| @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; | |
| } | |
| 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> | |
| <style name="AppTheme" parent="AppTheme.Base" /> | |
| <style name="AppTheme.Base" parent="Theme.AppCompat"> | |
| <item name="colorPrimary">@android:color/holo_red_light</item> | |
| <item name="colorPrimaryDark">@android:color/holo_red_dark</item> | |
| <item name="android:windowNoTitle">true</item> | |
| <item name="windowActionBar">false</item> | |
| </style> | |
| </resources> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create a new Android project and add these files into the project and you're good to go :-)