20160912

//manifest/////////////

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.kunihiro.mytoolbarapplication">

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

 

//MainActivity//////////////////////

package com.example.kunihiro.mytoolbarapplication;

 

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.Toolbar;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);

        setSupportActionBar(my_toolbar);

 

        getSupportActionBar().setTitle(R.string.my_tb_title);

        getSupportActionBar().setSubtitle(R.string.my_tb_subtitle);

        getSupportActionBar().setIcon(R.drawable.ic_toolbar);

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

 

        getMenuInflater().inflate(R.menu.main_menu, menu);

 

        return super.onCreateOptionsMenu(menu);

    }

 

    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

 

        switch (item.getItemId()) {

 

            case R.id.menu_1:

                Toast.makeText(MainActivity.this, "Option 1 clicked!", Toast.LENGTH_SHORT).show();

                break;

 

            case R.id.menu_2:

                Toast.makeText(MainActivity.this, "Option 2 clicked!", Toast.LENGTH_SHORT).show();

                break;

            case R.id.menu_3:

                Toast.makeText(MainActivity.this, "Option 3 clicked!", Toast.LENGTH_SHORT).show();

                break;

 

            case R.id.menu_4:

                Toast.makeText(MainActivity.this, "Option 4 clicked!", Toast.LENGTH_SHORT).show();

                break;

            case R.id.menu_5:

                Toast.makeText(MainActivity.this, "Option 5 clicked!", Toast.LENGTH_SHORT).show();

                break;

 

        }

 

        return super.onOptionsItemSelected(item);

    }

}

 

//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"

    tools:context="com.example.kunihiro.mytoolbarapplication.MainActivity">

 

    <include layout="@layout/toolbar"

        android:id="@+id/my_toolbar"/>

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!"

        android:layout_below="@id/my_toolbar"/>

</RelativeLayout>

 

//toolbar.xml/////////////////////////

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:background="@color/colorPrimary"

    app:theme="@style/ThemeOverlay.AppCompat.Dark"

    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v7.widget.Toolbar>

 

//main_menu.xml///////////////

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/menu_1"

        android:title="@string/menu_1" />

 

    <item

        android:id="@+id/menu_2"

        android:title="@string/menu_2" />

    <item

        android:id="@+id/menu_3"

        android:title="@string/menu_3" />

 

    <item

        android:id="@+id/menu_4"

        android:title="@string/menu_4" />

    <item

        android:id="@+id/menu_5"

        android:title="@string/menu_5" />

</menu>

 

//strings.hml////////////////////////

<resources>

    <string name="app_name">My Toolbar Application</string>

    <string name="my_tb_title">Maintitle</string>

    <string name="my_tb_subtitle">Subtitle.</string>

    <string name="menu_1">Option 1</string>

    <string name="menu_2">Option 2</string>

    <string name="menu_3">Option 3</string>

    <string name="menu_4">Option 4</string>

    <string name="menu_5">Option 5</string>

 

</resources>