add a button to toolbar android

Solutions on MaxInterview for add a button to toolbar android by the best coders in the world

showing results for - "add a button to toolbar android"
Corra
24 May 2016
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
Fleur
11 Feb 2016
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
queries leading to this page
toolbar add buttons androidtoolbar buttons androidandroid setup toolbar buttonhow to add button to toolbar androidadd button toolbar android studio javahow to add menu button in toolbar in androidandroid set toolbar buttonappbarlayout add buttonappbar right buttons androidadd button in toolbar androidandroid add buttons in toolbarhow to add a button in toolbar 2b androidandroid add button to toolbarhow to add menu to app bar using android studioandroid attach menu item to buttonadd button to toolbar androidandroid support v7 widget toolbar add buttonandroid add button in toolbarandroid studio toolbar add buttonadd button toolbar androidbutton in top menu androidandroid toolbar add button programmaticallybutton in toolbar androidhow to add button on toolbar in androidandroid add item to action bar programmaticallyadd button to top bar androidadd toolbar button androidcan we add button in toolbar androidadd button status bar androidwhat is action bar menu bar android studioandroid toolbar add button onclickadd back button to toolbar androidaction bar show up bottonhow to add a button on the toolbar in android studioaction bar menusandroid how to add toolbar menuandroid add button to tool baradd menu option android app barandroid studio add button to toolbarhow to add a button to toolbar androidhow to add a coustom button in the support action bar androidandroid create toolbar with buttonandroid button on toolbarandroid studio id of settings button tool toolbaradd back button in toolbar androidtoolbar button androidadd button to android toolbarandroid how to make buttons on toolbarandroid add buttons to toolbarandroid add toolbar buttonandroid studio id of settings button to toolbarandroid add button toolbarput button in toolbar androidchange action bar button on clickadd a button to toolbar android