android change menu item icon color programmatically
MainActivity.java
package com.example.espl.chnagemenuiconcolorapp;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Context context;
Menu menu_;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=this;
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Drawable drawable = menu_.findItem(R.id.action_go_home).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
}
Drawable drawable_search = menu_.findItem(R.id.action_search).getIcon();
if(drawable_search != null) {
drawable_search.mutate();
drawable_search.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_go_home:
Toast.makeText(context, "Home Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_search:
Toast.makeText(context, "Search Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
getMenuInflater().inflate(R.menu.search_menu, menu);
menu_=menu;
// You can also change the menu item color inside onCreateOptionsMenu of the MainActivity, find code commented below for same
// try {
// Drawable drawable = menu.findItem(R.id.action_go_home).getIcon();
//
// if(drawable != null) {
// drawable.mutate();
// drawable.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
// }
// }catch (Exception e){
// e.printStackTrace();
// }
}catch (Exception e){
e.printStackTrace();
}
return true;
}
}
search_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_go_home"
android:icon="@drawable/ic_home"
android:orderInCategory="100"
android:title="@string/abc_search_hint"
app:showAsAction="always" />
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:orderInCategory="100"
android:title="@string/abc_search_hint"
app:showAsAction="always" />
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="Click to hange menu item icon color programmatically "
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorWhite">#FFFFFF</color>
</resources>
for(int i = 0; i < menu.size(); i++){
Drawable drawable = menu.getItem(i).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.textColorPrimary), PorterDuff.Mode.SRC_ATOP);
}
}
MainActivity.java
package com.example.espl.chnagemenuiconcolorapp;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Context context;
Menu menu_;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=this;
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Drawable drawable = menu_.findItem(R.id.action_go_home).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
}
Drawable drawable_search = menu_.findItem(R.id.action_search).getIcon();
if(drawable_search != null) {
drawable_search.mutate();
drawable_search.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_go_home:
Toast.makeText(context, "Home Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_search:
Toast.makeText(context, "Search Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
getMenuInflater().inflate(R.menu.search_menu, menu);
menu_=menu;
// You can also change the menu item color inside onCreateOptionsMenu of the MainActivity, find code commented below for same
// try {
// Drawable drawable = menu.findItem(R.id.action_go_home).getIcon();
//
// if(drawable != null) {
// drawable.mutate();
// drawable.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
// }
// }catch (Exception e){
// e.printStackTrace();
// }
}catch (Exception e){
e.printStackTrace();
}
return true;
}
}
search_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_go_home"
android:icon="@drawable/ic_home"
android:orderInCategory="100"
android:title="@string/abc_search_hint"
app:showAsAction="always" />
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:orderInCategory="100"
android:title="@string/abc_search_hint"
app:showAsAction="always" />
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="Click to hange menu item icon color programmatically "
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorWhite">#FFFFFF</color>
</resources>
for(int i = 0; i < menu.size(); i++){
Drawable drawable = menu.getItem(i).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.textColorPrimary), PorterDuff.Mode.SRC_ATOP);
}
}
No comments:
Post a Comment