How to Customize Item Background and Item Text Color Inside Navigationview

How to customize item background and item text color inside NavigationView?

With the MaterialComponents Library you can use these attributes:

  • app:itemShapeFillColor: background item color
  • app:itemIconTint: icon tint
  • app:itemTextColor: text color

In the layout:

<com.google.android.material.navigation.NavigationView
app:itemShapeFillColor="@color/shape_selector"
app:itemIconTint="@color/icon_tint_selector"
app:itemTextColor="@color/text_color_selector"
../>

In a custom style:

<style name="..." parent="Widget.MaterialComponents.NavigationView" >
<item name="itemShapeFillColor">@color/shape_selector</item>
<item name="itemIconTint">@color/icon_tint_selector</item>
<item name="itemTextColor">@color/text_color_selector</item>
</style>

For the itemIconTint and itemTextColor you can use a selector like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_checked="true"/>
<item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:color="?attr/colorOnSurface"/>
</selector>

For the itemShapeFillColor you can use a selector like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.12" android:color="?attr/colorPrimary" android:state_activated="true"/>
<item android:alpha="0.12" android:color="?attr/colorPrimary" android:state_checked="true"/>
<item android:color="@android:color/transparent"/>
</selector>

Just a final note.
Pay attention to use to the itemBackground.

It is set to @null to use a shaped background programmatically generated by NavigationView when itemShapeAppearance and/or itemShapeAppearanceOverlay is set (default behaviour).

This background is styled using the itemShape* attributes.
Setting itemBackground will overwrite the programmatic background and cause values set in the itemShape* attributes to be ignored.

Sample Image

change Item text color of NavigationView

thanks for the response, i just search little and i found the answer there is no any style changes, i just change in JAVA file like and it's done.

NavigationView navigation_view = (NavigationView) findViewById(R.id.navigation_view);
navigation_view.setItemTextColor(ColorStateList.valueOf(Color.BLACK));
navigation_view.setItemIconTintList(null);

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

NavigationView selected item color

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<group
android:id="@+id/menu_grp"
android:checkableBehavior="single">

Turned out checkableBehavior = "single" is what made the menu selected item checked and what was missing in my other layout

Change the color of the selected navigation item's text and icon

The problem is solved, I just changed the drawable to color. in text_selector.xml file

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/text_color_white" android:state_checked="true" />
<item android:color="@color/black_text_color" />

Android Change background color of single item in NavigationView

Create menu class :

<item
android:id="@+id/nav_targets"
android:icon="@drawable/icon_target"
android:title="Targets" />

<item
android:id="@+id/nav_testing"
android:icon="@drawable/icon_testing"
android:title="Testing" />

<item
android:id="@+id/nav_course_work"
android:icon="@drawable/icon_course_work"
android:title="Course Work" />

<item
android:id="@+id/nav_schedule"
android:icon="@drawable/icon_schedule"
android:title="Schedule" />

<item
android:id="@+id/nav_profile"
android:icon="@drawable/icon_profile"
android:title="Profile" />

now in activity class :

    MenuItem menuItem = navigationView.getMenu().findItem(R.id.menu_item);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance), 0, s.length(), 0);
//for single item
if (menuItem.getItemId()==R.id.nav_targets){
menuItem.setTitle(s);
}

res / values / styles.xml

<style name="TextAppearance">
<item name="android:textColor">text_color</item>
<item name="android:textSize">text_size</item>
</style>

This will set the single item color and textsize.
Hope it will help you!!

Android - Change the background color of a single item in the NavigationView

app:itemBackground can be used to change the background color. You can create a selector for checked state and refer the same in xml.

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- focused -->
<item android:state_checked="true" android:drawable="@color/btn_color_green_alpha"></item>
</selector>

colors.xml

<color name="btn_color_green_alpha">#3300ff00</color>

layout file

<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"
app:itemBackground="@drawable/selector" //line to be added
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer" />

Hope this helps.

If you want only one item in selected color and don't want to apply to others, you cam make other items checkable= false.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="@+id/nav_gallery"
android:checkable="false" //line added here
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>
</menu>

Changing text color of menu item in navigation drawer

 <android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:background="#000"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:itemTextColor="your color"
app:menu="@menu/drawer" />


Related Topics



Leave a reply



Submit