Reputation: 379
I am not able to get Id of either Framelayout or Textview which are inside the toolbar. Please have a look :
<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/colorAppBg"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/logo"/>
<FrameLayout
android:id="@+id/fl_message_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="right|center">
<ImageView
android:id="@+id/iv_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginRight="12dp"
android:layout_marginTop="2dp"
android:src="@drawable/envelope"/>
<TextView
android:id="@+id/tv_message_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:text="10"
android:textColor="@color/colorWhite"
android:gravity="center"
android:textSize="12sp"
android:visibility="visible"
android:background="@drawable/circle_notification"/>
</FrameLayout>
</FrameLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
This is my code of finding ID of those view either in fragment or Activity which extends AppCompatActivity. Inside Fragment :
`private Activity activity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.activity =(Activity)context;
}
flMessageCountBg = (FrameLayout) activity.findViewById(R.id.fl_message_bg);`
In Activity :
FrameLayout flMessageCountBg = (FrameLayout) findViewById(R.id.fl_message_bg);
I am getting null in both case.Please help me on this. Thanks in advance.
Upvotes: 2
Views: 678
Reputation: 379
The solution was very simple of getting id (Inside Fragment) of view created within android.support.v7.widget.Toolbar.
flMessageCountBg = (FrameLayout) getActivity().findViewById(R.id.fl_message_bg);
I have created three layout folder layout,layout-large-port and layout-xlarge-port and I miss to change the ID while I am testing for 7 inch Tablet which pick Id from layout-large-port.
@Malek,@Context and @bhargav
Thanks for your help.
Upvotes: 0
Reputation: 4162
You're using a support library so I you should set the toolbar as support action bar. Then you can get the views inside the toolbar by using toolbar.findviewbyid(id)
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageView iv = (ImageView) toolbar.findViewById(R.id.iv_cart);
if you're using a fragment and your activity extends AppCompatActivity then you can set the support action bar as follows:
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
Upvotes: 2
Reputation: 1883
Get your toolbar from your activity in the fragment then you get your framelayout from your toolbar like this:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
FrameLayout flMessageCountBg = (FrameLayout) toolbar.findViewById(R.id.fl_message_bg);
Upvotes: 1
Reputation: 4954
Try this way
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
FrameLayout flMessageCountBg = (FrameLayout) toolbar.findViewById(R.id.fl_message_bg);
Upvotes: 1