-1

i have a toolbar which include one button, how to implement onclick function for this button?

this is my way to create the toolbar and button.

 <android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar_bottom"
    android:layout_width="match_parent"
    android:background="#2196F3"
    android:layout_height="wrap_content"
    >

    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:layout_gravity="left"
        />
</android.support.v7.widget.Toolbar>

this is the function call in MainActivity

Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
    toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()){
                case R.id.test:
                    Toast.makeText(MainActivity.this,"asdasd",Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });
AnthonyTang
  • 89
  • 1
  • 1
  • 9
  • Possible duplicate of [Creating a button in Android Toolbar](http://stackoverflow.com/questions/31231609/creating-a-button-in-android-toolbar) – Ankush Bist Feb 08 '17 at 10:47

3 Answers3

0
<Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:onClick="callYourMethod" // This One
        android:layout_gravity="left"
        />

Then Call this method from your Activity like this, 

public void callYourMethod(View view ) {
}
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
0

You have to find your button inside your toolbar like this:

Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
Button btnTest = (Button) toolbarBottom.findViewById(R.id.test);

Then you can call the onClick on that button:

btnTest.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        //Do what you want
      }
    });
Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50
0

Try this,

<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_bottom"
android:layout_width="match_parent"
android:background="#2196F3"
android:layout_height="wrap_content"
>

<Button
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test"
    android:layout_gravity="left"
    />
 </android.support.v7.widget.Toolbar>



 Toolbar toolbarBottom = (Toolbar)findViewById(R.id.toolbar_bottom);
 Button test = (Button) toolbarBottom.findViewById(R.id.test);

 test.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           Log.d("TAG","Button click");
        }
    });
Komal12
  • 3,340
  • 4
  • 16
  • 25