Replace ActionBar with android.support.v7.widget.Toolbar

A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method.

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.


Next:
Set title, subtitle and logo of Toolbar
Add OptionsMenu to Toolbar

Replace ActionBar with android.support.v7.widget.Toolbar Replace ActionBar with android.support.v7.widget.Toolbar Reviewed by Pendik on 14.45 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.