This example show how to replace ActionBar with android.support.v7.widget.Toolbar.
- Create a new project with Minimum SDK of API 16, select template of Empty Activity.
- Edit res/values/styles.xml to use style of "Theme.AppCompat.Light.NoActionBar".
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
- Add <android.support.v7.widget.Toolbar> to /res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.blogspot.android_er.androidtoolbar.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFA000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
- Edit MainActivity.java to call setSupportActionBar(toolbar) method.
package com.blogspot.android_er.androidtoolbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
- To make the Toolbar fill the width of the layout, edit /res/values/dimens.xml to set both "activity_horizontal_margin" and "activity_vertical_margin" to 0dp.
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>
</resources>
This video show how-to in Android Studio, and how it shown on Android Emulator of API 16 and 23.
More:
- Set title, subtitle and logo of Toolbar
- Add OptionsMenu to Toolbar
- Set image on Toolbar
- Implement checkable items in OptionsMenu of Toolbar
- Add button inside Toolbar
- Implement transparent status bar for android.support.v7.widget.Toolbar
- ToolBar with ProgressBar (with video show the steps to place ToolBar using Android Studio graphical Design Tool)
1 comment:
Nicely done
Post a Comment