简体   繁体   中英

Changing icon of another menuitem in Android Studio

I have a menu in ActionBar. "gecerlidil" is menu item and "ingilizce" and "turkce" are sub-menu of it.

<?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">
    <item android:id="@+id/gecerlidil"
        android:icon="@drawable/ing"
        android:title="ingilizce"
        app:showAsAction="always">
    <menu>
        <item android:id="@+id/ingilizce"
            android:title="EN"
            android:icon="@drawable/ing"/>
        <item android:id="@+id/turkce"
            android:title="TR"
            android:icon="@drawable/tur"/>
    </menu>
        </item>
</menu>

When i click ingilizce menu item or turkce menu item gecerlidil menu icon will change but i couldn't succeed it. I think i will do it in onOptionsItemSelected function. Thanks for your help

action bar is deprecated actually in android, so i cant help you...

but i recommended you use toolbar, is like action bar but you can do this

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize">

<ImageView
    android:layout_width="156dp"
    android:layout_height="20dp"
    android:src="@drawable/icon"/>

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

and you will change this ImageView or ImageButton using a menu xml and onOptionsItemSelected()

i hope that this will help you

declare a global variable private Menu menu;

In your onCreateOptionsMenu()

this.menu = menu;

then in the OptionsItemSelected menu.getItem(0).setIcon(ResourcesCompat.getDrawable(getResources(),R.drawable.ic‌​_launcher,null));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM