How to access a Button inside a Toolbar in Android

Viewed 2250

I'm pretty new to coding so I might not express myself correctly, please bear with me.

I have a fragment inflated inside a layout that includes a toolbar. When I open the fragment, I want to add a button (not a menu, just a button to navigate to another fragment). My issue is I don't know how to instantiate the button from my Fragment class to add an icon and an onClickListener.

Here's my xml files: The layout that contains my fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/blue"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

            <Button
                android:id="@+id/toolbar_button"
                android:layout_width="@dimen/margin"
                android:layout_height="@dimen/margin"
                android:layout_gravity="right"
                android:layout_marginRight="@dimen/innerLargeMargin"
                />

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

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</LinearLayout>

The layout in which the fragment is inflated:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:design="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="be.ac.ulg.mobulis.Activities.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:background="@color/orange">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#f1f1f1">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        design:menu="@menu/bottom_nav_items"
        android:background="@color/orange"
        android:backgroundTint="@color/white"
        design:itemIconTint="@color/white"
        design:itemTextColor="@color/white"
        design:itemBackground="@drawable/bottom_bar_tint_selector"
        />

</LinearLayout>

So what I want to do is something like

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    toolBarButton = (???).findViewById(R.id.toolbar_button)
}
6 Answers

activity_main.xml to add Button inside android.support.v7.widget.Toolbar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="#FFA000">
        <Button
            android:id="@+id/toolbarbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="right"/>
    </android.support.v7.widget.Toolbar>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button toolBarBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setTitle("Toolbar with button example");
        toolbar.setSubtitle("Toolbar subtitle");
        toolbar.setLogo(android.R.drawable.ic_menu_info_details);

        toolBarBtn = (Button)findViewById(R.id.toolbarbtn);
        toolBarBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(),
                        "Button in ToolBar clicked",
                        Toast.LENGTH_LONG).show();
            }
        });
    }


}

try this to access your button of toolbar from activity

private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) toolbar.findViewById(R.id.toolbar_button);     
setSupportActionBar(toolbar);


toolbar_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ChatActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
            }
        });

Set This onClick Listener in your parent activity of Fragment.

you have to write like below

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    toolBarButton = (Button) view.findViewById(R.id.toolbar_button);

where view is your view which you bind at your onCreateView in your Fragment.

What I do is to bind the View inside the activity and then create an interface who works with that View.

public class Activity implements ButtonListener{

//findViewbyId or Butterknife

void doSomething(){
   toolBarButton.something()
}

Interface

}
public interface ButtonListener{
   void doSomething();
}

Fragment

public class Fragment extends Fragment{
    public ButtonListener mButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
       mButton = (ButtonListener)getActivity();
       super.onCreate(savedInstanceState);
    }
}

You can access button directly without toolbar using (Button) findViewById(R.id.toolbar_button).

Toolbar toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
Button toolbar_button = (Button) findViewById(R.id.toolbar_button);     
setSupportActionBar(toolbar);
Related