Access Toolbar (and its children) from fragment?
Asked Answered
E

6

13

I'm building an application using the v7.widget.Toolbar component.

I'm able to add the toolbar to my activity, but I don't know how (and what is the right way?) to access it from the activity's fragment.

Here's the toolbar

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
app:theme="@style/MainActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
tools:context=".MainActivity">

<ImageView
    android:background="@drawable/icon_1"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:layout_marginLeft="19dp"
    android:id="@+id/menu_1"
    android:layout_gravity="center_vertical|left"
    android:src="@drawable/menu_burger"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageView
    android:layout_marginRight="19dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:id="@+id/menu_2"
    android:layout_gravity="center_vertical|right"
    android:src="@drawable/icon_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Here's how I use it in the activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
     ...
     ...

</LinearLayout>

In my activity's onCreate:

   Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
   setSupportActionBar(actionBar);

And now, for example, I want to access "@id/menu_1" from the Toolbar in my fragment, How do I do that?

getSupportActionBar() gives me the ActionBar, but I need the Toolbar

Thanks in advance!

Excel answered 4/2, 2015 at 17:34 Comment(1)
getActivity().findViewById(R.id.toolbar)?Knew
S
15

I'm not sure if this works, but you can try it.

    final AppCompatActivity act = (AppCompatActivity) getActivity();
    if (act.getSupportActionBar() != null) {
        Toolbar toolbar = (Toolbar) act.getSupportActionBar().getCustomView();
    }

Toolbar is essentially a ViewGroup.

Spout answered 2/7, 2015 at 11:18 Comment(0)
A
18

no need to code a lot: follow this

in Kotlin:

   var toolbar = activity.findViewById<Toolbar>(R.id.toolbar)
    toolbar.setTitle("Rashid")

in Java:

 Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
 toolbar.setTitle("Rashid")
Animal answered 25/7, 2017 at 15:54 Comment(0)
S
15

I'm not sure if this works, but you can try it.

    final AppCompatActivity act = (AppCompatActivity) getActivity();
    if (act.getSupportActionBar() != null) {
        Toolbar toolbar = (Toolbar) act.getSupportActionBar().getCustomView();
    }

Toolbar is essentially a ViewGroup.

Spout answered 2/7, 2015 at 11:18 Comment(0)
B
5

The communication between Activity and Fragments (the same holds for any Java components) should be done through an Interface.

For your case define an interface

public interface IOperateOnToolbar {
    //methods allowing to communicate with toolbar
    public void setToolbarColor(Color c);
    public void setImageView1Resource(int resID);
    public void setImageView2Resource(int resID);
    //.....
}

So you have an Activity

 public class MainActivity extends AppCompatActivity implements IOperateOnToolbar {

   //onCreate, onDestroy ...

   public void setToolbarColor(Color c) {
        //implementation follows
   }
   public void setImageView1Resource(int resID) {
       imageView1.setBackgroundResource(resID);
   }
   public void setImageView2Resource(int resID) {
       imageView2.setBackgroundResource(resID);
   }
}

And your Fragment

public class MyFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {    
         IOperateOnToolbar toolbarCallback = (IOperateOnToolbar) getActivity();
    }
}

Using the interfaces for communication between components has the following benefits:

  • If you replace the "MainActivity" with another one ("ActivityX" which also implements IOperateOnToolbar), you don't have to edit anything in "MyFragment"

  • "MyFragment" has access only to those methods, which are exposed by IOperateOnToolbar interface.

Please chceck this docu, where google advises the communication (between fragments, but should be the in principle the same) using interfaces.

Bibliography answered 29/7, 2015 at 19:50 Comment(0)
G
2

this will do the job for you

((MainAcivity)this.getActivity()).getToolbar();
Goff answered 15/7, 2015 at 7:3 Comment(0)
N
2

Here you can access toolbar children:

TextView toolbarTextView  = (TextView) ((MainActivity) this.getActivity()).findViewById(R.id.toolbarTitle);
toolbarTextView.setText("Hello");
Nuzzi answered 20/8, 2015 at 10:19 Comment(0)
H
0

In my case I has an activity and a navigation drawer that replaces this activity fragment with other fragments. If you implement toolbar views (spinners, Buttons..) in the main activity, the fragments that will be replaced in this activity can see toolbar and interact with its views.

else if, you need to handle actions depending on this views, you can use: getActivity.findViewById(); inside fragment.

Hardfavored answered 7/2, 2018 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.