How to Change Option Menu Icon in The Action Bar

How to change option menu icon in the action bar?

The following lines should be updated in app -> main -> res -> values -> Styles.xml

 <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">@drawable/ic_launcher</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"Lala"</item>
</style>

This is how it can be done. If you want to change the overflow icon in action bar

How to change MenuItem icon in ActionBar programmatically

You can't use findViewById() on menu items in onCreate() because the menu layout isn't inflated yet. You could create a global Menu variable and initialize it in the onCreateOptionsMenu() and then use it in your onClick().

private Menu menu;

In your onCreateOptionsMenu()

this.menu = menu;

In your button's onClick() method

menu.getItem(0).setIcon(ContextCompat.getDrawable(this, R.drawable.ic_launcher));

How to change icons in ActionBar dynamically

Edit2:
in onCreateOptionsMenu (Menu menu)

MenuItem menuItem=menu.findItem(R.id.exit_menu);
MenuItemCompat.getActionView(menuItem);
menuItem.setIcon(R.drawable.new_icon);

xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/exit_menu"
android:title="@string/menu_string"
android:icon="@drawable/exit"
app:showAsAction="always"/>
</menu>

Changing Action Bar Menu Item Icon When Pressed in Android Studio

As Simple as it goes !!

ToobarActivity.class:

    public class ToolbarActivity extends AppCompatActivity {

private Menu menu;// Global Menu Declaration

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

//This is my Back Button View onClick on toolbar
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.main_menu, menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//This is the Answer to your problem
if (id == R.id.actionsignout) {
menu.getItem(1).setVisible(true);
menu.getItem(0).setVisible(false);
return true;
}else if (id == R.id.action) {
menu.getItem(0).setVisible(true);
menu.getItem(1).setVisible(false);
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

return super.onPrepareOptionsMenu(menu);
}
}

On res/menu/main_menu.xml:

Set the second item Visibility to false

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/actionsignout"
app:showAsAction="always"
android:icon="@drawable/ic_pause_black_24dp"
android:title="Sign Out"/>

<item android:id="@+id/action"
app:showAsAction="ifRoom"
android:visible="false"
android:icon="@drawable/ic_play_arrow_black_24dp"
android:title=""/>

How to change the Option menu icon in the Action Bar?

See more attributes from your android sdk installation, in platforms/android-XX/data/res/values/style.xml

Here is menu overflow action button style definition:

<style name="Widget.Holo.ActionButton.Overflow">
<item name="android:src">@android:drawable/ic_menu_moreoverflow_holo_dark</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">@string/action_menu_overflow_description</item>
</style>

Image Icon with Action Bar's Option Menu Item

Below code solved my issue. from this link : How To show icons in Overflow menu in ActionBar

<item
android:id="@+id/empty"
android:icon="@drawable/ic_action_overflow"
android:orderInCategory="101"
android:showAsAction="always">
<menu>
<item
android:id="@+id/action_show_ir_list"
android:icon="@drawable/ic_menu_friendslist"
android:showAsAction="always|withText"
android:title="List"/>
</menu>
</item>

How to change color of menu icon on custom action bar?

First, define a style that sets colorControlNormal, making sure to derive from one of the ThemeOverlay styles:

<style name="MyTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="colorControlNormal">@color/your_color_here</item>
</style>

Then, apply this style to your toolbar in your layout:

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/MyTheme"/>

How to make action bar icon change when clicked

I'd like to qualify with "I have not checked this using ActionBarSherlock only the standard actionbar"

That said I know this will work on the standard actionbar and see no reason why it would not in ActionBarSherlock.

In the onCreate of the activity I have (I'm using the logo attribute but it will work for MenuItems as well)

    getActionBar().setLogo(R.drawable.logo);
getActionBar().setDisplayUseLogoEnabled(true);
getActionBar().setHomeButtonEnabled(true);

In logo.xml I have a selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/logopressed"/>
<item android:drawable="@drawable/logonotpressed" />
</selector>

In logopressed.xml A layer list can be used to make the background

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@android:color/holo_purple" />
<item
android:drawable="@drawable/logo_normal" />
</layer-list>

In logonotpressed.xml switch out the background colour and/or icon drawable as desired

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@android:color/white" />
<item
android:drawable="@drawable/logo_inverted" />
</layer-list>

So the crux of this solution is to set the selector drawable and let it select a whole new drawable for each state rather than setting a selector drawable to the background attribute of a otherwise static variable

Note: you should be able to theme the items with a background instead of doing 2 layers but I haven't been able to get this to work



Related Topics



Leave a reply



Submit