Change Background Color of Android Menu

Android menu overlay background color

Change this:

<style name="MainActionBar.Popup" parent="Widget.AppCompat.PopupMenu">
<item name="android:textColor">@color/primary_text</item>
</style>

With:

<style name="MainActionBar.Popup" parent="ThemeOverlay.AppCompat.Light">
<item name="android:colorBackground">Your Background Color</item>
<item name="android:textColor">Your Text Color</item>
</style>

Remove the following from MainActionBar theme:

<item name="android:background">@color/primary</item>

Add this attribute to Toolbar tag:

android:background="?attr/colorPrimary"

Hope this helps.

How to change the background color of Action Bar's Option Menu in Android 4.2?

There is an easy way to change the colors in Actionbar
Use ActionBar Generator and copy paste all file in your res folder and change your theme in Android.manifest file.

How to change Background color of menuItem in android?

Firstly You need to create an Style for popmenu as you want refer below sample for that

<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:textStyle">@style/commonEditTextTheme</item>
<item name="android:popupBackground">@drawable/pop_up_menu_bg_with_shadow</item>
</style>

Place of drawable you can also replace with color as you needed or make XML Drawable into your drawable folder

<style name="commonEditTextTheme" parent="@android:style/TextAppearance.Medium">
<item name="android:fontFamily">sans-serif-light</item>
</style>

this pop menu theme add in your main application or activity theme like below

<!--My Theme-->
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
......
<item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
</style>

Android Change Navigation View Menu Background

You can set background to your NavigationView to change its color like below (set to app_white ) & for header set any color as a background of drawer_header_layout.

<android.support.design.widget.NavigationView
android:id="@+id/adcl_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/app_white"
app:headerLayout="@layout/drawer_header_layout"
app:itemIconTint="@color/app_secondary_text"
app:itemTextColor="@color/app_secondary_text"
app:menu="@menu/menu_drawer"
app:theme="@style/ThemeOverlay.AppCompat.Light" />

how to change option menu background color

There problem with the material theme

 <style name="Theme.MyApplication" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="android:itemBackground">@color/bluish_green</item>
</style>

<style name="ThemeOverlay.MyTheme" parent="Theme.MyApplication">
<!-- To change the popup menu and app text color -->
<item name="android:textColor">@color/white</item>
<!-- To change the background of options menu-->
<item name="android:itemBackground">@color/black</item>
<item name="android:popupBackground">@color/black</item>
<item name="android:drawablePadding">0dp</item>
<item name="android:dividerHeight">1dp</item>

</style>

Change background color of single specific menu items of navigationView

Change background color of single specific menu items

AFAIK Using menu this is not possible you need to create custom navigationView

When you use BackgroundColorSpan to set background to your menu item it only set the background to menu item title not whole view

OUTPUT USING BackgroundColorSpan

Sample Image

Try this way using RecyclerView


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/nav_header_main" />

<android.support.v7.widget.RecyclerView
android:id="@+id/navRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

MainActivity

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

RecyclerView navRecyclerView;
LinearLayoutManager layoutManager;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
NavigationAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(this);

navRecyclerView = findViewById(R.id.navRecyclerView);
navRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
navRecyclerView.setLayoutManager(layoutManager);

initArray();

adapter = new NavigationAdapter(this, arrayList);
navRecyclerView.setAdapter(adapter);

}

private void initArray() {

NavigationDataModel model = new NavigationDataModel();
model.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
model.setIcon(R.drawable.ic_menu_gallery);
model.setTitle("Item 1");
arrayList.add(model);

NavigationDataModel model2 = new NavigationDataModel();
model2.setColor(ContextCompat.getColor(this, R.color.colorRed));
model2.setIcon(R.drawable.ic_menu_camera);
model2.setTitle("Item 2");
arrayList.add(model2);

NavigationDataModel model3 = new NavigationDataModel();
model3.setColor(ContextCompat.getColor(this, R.color.colorGreen));
model3.setIcon(R.drawable.ic_menu_send);
model3.setTitle("Item 3");
arrayList.add(model3);

NavigationDataModel model4 = new NavigationDataModel();
model4.setColor(ContextCompat.getColor(this, R.color.colorPink));
model4.setIcon(R.drawable.ic_menu_share);
model4.setTitle("Item 4");
arrayList.add(model4);

}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

NavigationAdapter

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {

Context context;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();

public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
}

@Override
public int getItemCount() {
return arrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
ImageView navIcon;
TextView navTitle;
LinearLayout rootView;

public ViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
navIcon = itemView.findViewById(R.id.navIcon);
navTitle = itemView.findViewById(R.id.navTitle);
}
}
}

NavigationDataModel

public class NavigationDataModel {
private int icon, color;
private String title;

public int getIcon() {
return icon;
}

public void setIcon(int icon) {
this.icon = icon;
}

public int getColor() {
return color;
}

public void setColor(int color) {
this.color = color;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

OUTPUT
Sample Image

change background color single item menu

If you want to use item tags and do this,as an alternative way you can tryBackgroundColorSpan and write the logic to reformat of the length of your strings.

s.setSpan(new BackgroundColorSpan(Color.RED), 0, s.length(), 0);

out put:

Sample Image


else

If i do that i'll crate a Dialog without a Title

private Dialog fakeDialogUseInstedOfMenuItem;

fakeDialogUseInstedOfMenuItem = new Dialog(MainActivity.this);
fakeDialogUseInstedOfMenuItem.requestWindowFeature(Window.FEATURE_NO_TITLE); // no Title
fakeDialogUseInstedOfMenuItem.setContentView(R.layout.my_custom_view);

and set a fixed or scroll view for that as the requirement

my_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="200dp"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#789"
android:orientation="vertical">

<LinearLayout
android:id="@+id/first_lin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center">

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option One"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFF"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option Two"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"></LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFF"></LinearLayout>
</LinearLayout>
</ScrollView>

</LinearLayout>

and access items like this,

LinearLayout linearLayoutOneInDialog = (LinearLayout) fakeDialogUseInstedOfMenuItem.findViewById(R.id.first_lin);

out put:

Sample Image

But these things are optional ways



Related Topics



Leave a reply



Submit