1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 android:orientation="vertical"
11 tools:context="com.blogspot.android_er.androidtoolbar.MainActivity">
12
13 <android.support.v7.widget.Toolbar
14 android:id="@+id/toolbar"
15 android:layout_width="match_parent"
16 android:layout_height="56dp"
17 android:background="#FFA000">
18 <Button
19 android:id="@+id/toolbarbtn"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:text="Button"
23 android:layout_gravity="right"/>
24 </android.support.v7.widget.Toolbar>
25 <TextView
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="Hello World!" />
29</LinearLayout>
30
1package com.blogspot.android_er.androidtoolbar;
2
3import android.os.Bundle;
4import android.support.v7.app.AppCompatActivity;
5import android.support.v7.widget.Toolbar;
6import android.view.Menu;
7import android.view.MenuItem;
8import android.view.View;
9import android.widget.Button;
10import android.widget.Toast;
11
12public class MainActivity extends AppCompatActivity {
13
14 Button toolBarBtn;
15
16 @Override
17 protected void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.activity_main);
20 Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
21 setSupportActionBar(toolbar);
22
23 getSupportActionBar().setTitle("Toolbar example");
24 toolbar.setSubtitle("Android-er.blogspot.com");
25 toolbar.setLogo(android.R.drawable.ic_menu_info_details);
26
27 toolBarBtn = (Button)findViewById(R.id.toolbarbtn);
28 toolBarBtn.setOnClickListener(new View.OnClickListener() {
29 @Override
30 public void onClick(View v) {
31 Toast.makeText(getBaseContext(),
32 "Button in ToolBar clicked",
33 Toast.LENGTH_LONG).show();
34 }
35 });
36 }
37
38 @Override
39 public boolean onCreateOptionsMenu(Menu menu) {
40 getMenuInflater().inflate(R.menu.menu_main, menu);
41 return super.onCreateOptionsMenu(menu);
42 }
43
44 @Override
45 public boolean onOptionsItemSelected(MenuItem item) {
46
47 Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
48
49 int item_id = item.getItemId();
50 switch(item_id){
51 case R.id.item_checkable1:
52 if(item.isChecked()){
53 item.setChecked(false);
54 }else{
55 item.setChecked(true);
56 }
57 break;
58 case R.id.item_checkable2:
59 if(item.isChecked()){
60 item.setChecked(false);
61 }else{
62 item.setChecked(true);
63 }
64 break;
65 }
66
67 return super.onOptionsItemSelected(item);
68 }
69}
70
71