Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 textviews.

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>
like image 330
Sai Avatar asked Dec 20 '15 06:12

Sai


1 Answers

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

like image 91
myQwil Avatar answered Sep 28 '22 06:09

myQwil