How to access views in ActionLayout in a menu

I have three menu items that include the same app:actionLayout , that it has a text view, how can I access text images individually through code and set different text for all three text views.

activity_main_drawer.xml

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <group android:checkableBehavior="single"> <item android:id="@+id/item_tracks" app:actionLayout="@layout/menu_text_layout" android:icon="@drawable/ic_play_arrow" android:title="Tracks"/> <item android:id="@+id/item_repeat" app:actionLayout="@layout/menu_text_layout" android:icon="@drawable/ic_play_arrow" android:title="Repeat"/> <item android:id="@+id/item_timer" app:actionLayout="@layout/menu_text_layout" android:icon="@drawable/ic_play_arrow" android:title="Timer"/> </group> <item android:title="More"> <menu> <item android:id="@+id/item_animation" app:showAsAction="always" android:icon="@drawable/ic_play_arrow" app:actionLayout="@layout/switch_layout" android:title="Animation"/> <item android:id="@+id/item_background" android:icon="@drawable/ic_play_arrow" android:title="Background Music"/> </menu> </item> </menu> 

menu_text_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tool:background="@android:color/white" android:gravity="center_vertical"> <TextView android:id="@+id/switchForActionBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:maxLength="10" android:ellipsize="marquee" android:fontFamily="sans-serif" android:textAppearance="?android:attr/textAppearanceSmall" android:background="@color/Color_600" android:textColor="@android:color/white" android:padding="2.5dp" android:text="Change Text"/> </LinearLayout> 
+6
source share
1 answer

In OnCreateOptionsMenu it will look something like this:

 getMenuInflater().inflate(R.menu.activity_main_drawer, menu); LinearLayout tracks = (LinearLayout) menu.findItem(R.id.item_tracks).getActionView(); LinearLayout repeat = (LinearLayout) menu.findItem(R.id.item_repeat).getActionView(); LinearLayout timer = (LinearLayout) menu.findItem(R.id.item_timer).getActionView(); TextView tvTracks = (TextView) tracks.findViewById(R.id.switchForActionBar); TextView tvRepeat = (TextView) repeat.findViewById(R.id.switchForActionBar); TextView tvTimer = (TextView) timer.findViewById(R.id.switchForActionBar); tvTracks.setText("foo"); tvRepeat.setText("bar"); tvTimer.setText("baz"); return true; 

I am using LinearLayout as this is the first item in menu_text_layout.xml

+6
source

All Articles