Anand Savjani Anand Savjani - 1 month ago 6
Android Question

Access image of Imageview from fragment in toolbar

I have implement ImageView with in toolbar and want to access toolbar image in fragment. Now I want to access ImageView of toolbar.

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

<TextView
android:id="@+id/textToolHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Header"
android:textColor="@android:color/white"
android:textSize="@dimen/txt_18"/>

<ImageView
android:id="@+id/imgToolIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="@dimen/scale_10"
android:src="@drawable/user"/>

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


and my Fragment code is as below:

toolbar = (Toolbar) mView.findViewById(R.id.toolbar);
imgToolIcon = (ImageView) toolbar.findViewById(R.id.imgToolIcon);
imgToolIcon.setImageDrawable(getResources().getDrawable(R.drawable.print));


Please provide suggestion for it.

Answer

Finally I got solution of it which is as under :

ImageView imgToolIcon;
Toolbar toolbar;
TextView textToolHeader;

toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
imgToolIcon = (ImageView) toolbar.findViewById(R.id.imgToolIcon);
imgToolIcon.setImageResource(R.drawable.user);

This is exact solution which is helpful for me to set custom imageview in toolbar.