Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Toolbar (and its children) from fragment?

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!

like image 263
dor506 Avatar asked Feb 04 '15 17:02

dor506


People also ask

How do I access the activity toolbar in fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.

How to Set Toolbar in fragment Android Example?

To do this, call setHasOptionsMenu(true) in the onCreate() method of the fragment. The Android framework calls in this case the onCreateOptionsMenu() method in the fragment class. Here the fragment can adds menu items to the toolbar.

How can we call parent activity from fragment?

Simply call your parent activity using getActivity() method.


1 Answers

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.

like image 120
javmarina Avatar answered Oct 19 '22 13:10

javmarina