change color of only ONE menu item icon in android

Molan

i want to change only one menu item in the action bar. I tried to change the same icon color instead of making it transparent into the desired color but the edges show the background of the action bar.

Here is my action bar what it looks like:

https://www.dropbox.com/s/1sjnk975dnj17kr/before.png?dl=0

And here is the goal i want to reach:

https://www.dropbox.com/s/ul7hh2gxtfox1pk/goal.png?dl=0

Here are my xml files:

menu_main.xml

<item
    android:id= "@+id/location"
    android:icon="@drawable/ic_room_white_48dp"
    android:title=""
    android:orderInCategory="2"
    app:showAsAction="always"/>


<item
    android:id= "@+id/report"
    android:icon="@drawable/report"
    android:title=""
    android:orderInCategory="3"
    app:showAsAction="always"/>


<item
    android:id= "@+id/message"
    android:icon="@drawable/ic_comment_white_48dp"
    android:title=""
    android:orderInCategory="4"
    app:showAsAction="always"/> 

styles.xml

<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">

    <item name="colorPrimary">#B9ACC1</item>

    <item name="colorPrimaryDark">#827689</item>

    <item name="colorAccent">#827689</item>

    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>

<style name="MyActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">


    <item name="android:padding">15dip</item>

</style>

Kassisdion

UPDATE1 : Adding a way to avoid xml and code duplication.

UPDATE2 : Adding static listener.

I don't know if you can achieve your goal just by changing your menu.xml but the Toolbar is a viewGroup so you can achieve your goal with a view.xml which should looks like it :

toolbar.xml :

    <LinearLayout
        android:layout_width="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_marginRight="8dp"
        android:background="#000000">

        <Button
            android:id="@+id/button_first"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:background="@android:drawable/ic_lock_idle_lock" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_marginRight="8dp"
        android:background="#000000">

        <Button
            android:id="@+id/button_second"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:background="@android:drawable/ic_lock_idle_lock" />

    </LinearLayout>

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

It can be boring to add this code on all of your xml so i advice you to to use thé <include /> attribute like this :

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


</RelativeLayout>

Then, you just have to set the Toolbar and use the Button

MainActivity.java

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

    final Button buttonFirst = (Button) findViewById(R.id.button_first);
    final Button buttonSecond = (Button) findViewById(R.id.button_second);

    buttonFirst.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonSecond.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
   });
}

If all of your button have the same listener, you can make them static into avoid the code duplication :

IconListener.java :

public class IconListener {

    public static View.OnClickListener listenerButtonOne = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //DO what you want
        }
    };

    public static View.OnClickListener listenerButtonTwo = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do what you want
        }
    };
}

So your MainActivity.java became

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

    final Button buttonFirst = (Button) findViewById(R.id.button_first);
    final Button buttonSecond = (Button) findViewById(R.id.button_second);

    buttonFirst.setOnClickListener(IconListener.listenerButtonOne)
    buttonSecond.setOnClickListener(IconListener.listenerButtonTwo);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android Navigation drawer menu item icon color

From Dev

Change menu navigation icon color in xml for Android

From Dev

Change menu navigation icon color in xml for Android

From Dev

Change the color of menu icon

From Dev

Android menu item with icon

From Dev

Icon Color in Android Slide Menu

From Dev

Android change actionBar icon menu

From Dev

Why only the last menu item has icon?

From Dev

How to change Menu Item icon from fragment?

From Dev

How to change the size of menu item icon in NavigationView?

From Dev

Android How to add icon on each listvIew list item and change the text color,Background color

From Dev

Qtreewidget item change color to Icon in Qtreewidgetitem

From Dev

get drawable from Android Menu Item Icon

From Dev

Break line after Icon in Menu Item Android

From Java

Change the color of a checked menu item in a navigation drawer

From Dev

change menu color item when active

From Dev

Change the color of selected menu item using css

From Dev

How to change the text color of context menu item

From Dev

How to change color of hovered menu item in jquery?

From Dev

Only change background color of one <a>

From Dev

Smartface android actionbar menu icon change

From Dev

jQuery - only active menu item will change

From Dev

Android - Navigation View item menu background color

From Dev

Navigation Drawer Menu Item Title Color in Android

From Dev

Android ActionBar - Item icon change continuously?

From Dev

Android - change send icon's color on ImageButton

From Dev

Cannot change navigation drawer icon color android

From Dev

android change color of an icon from EditText

From Dev

Windows 7: How to change context menu item icon?

Related Related

HotTag

Archive