Skip to content Skip to sidebar Skip to footer

How To Access Views Insde The Actionlayout In Menu

I have three menu items which includes same app:actionLayout it has a textview, how can i access the textviews individually through code and set different text for all three textvi

Solution 1:

It would look something like this in OnCreateOptionsMenu:

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'm using LinearLayout as it's the first element in menu_text_layout.xml


Post a Comment for "How To Access Views Insde The Actionlayout In Menu"