Changing NavigationView background to round shape not working when pressed - android

I am setting navigation item background using app:itemBackground in layout:
<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/nav_selector_background"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
Here is nav_selector_background.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#color/colorNavItemSelected">
<item android:id="#android:id/mask">
<color android:color="#000"/> <!-- any color will do -->
</item>
<item
android:right="8dp">
<selector>
<item android:state_pressed="true" android:drawable="#drawable/nav_item_background_round"/>
<item android:state_focused="true" android:drawable="#drawable/nav_item_background_round"/>
<item android:state_checked="true" android:drawable="#drawable/nav_item_background_round"/>
<item android:state_activated="true" android:drawable="#drawable/nav_item_background_round"/>
<item android:state_active="true" android:drawable="#drawable/nav_item_background_round" />
<item android:state_hovered="true" android:drawable="#drawable/nav_item_background_round" />
<item android:state_drag_hovered="true" android:drawable="#drawable/nav_item_background_round" />
<item android:drawable="#android:color/transparent"/>
</selector>
</item>
And nav_item_background_round:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/colorNavItemSelected"/>
<corners android:bottomRightRadius="32dp"
android:topRightRadius="32dp"/>
</shape>
</item>
</layer-list>
Problem is when I press on the item the whole background is highlighted as seen in the screenshot. I only want the red part highlighted.
I uploaded the sample on Github if anybody is interested in compiling and running the app.

With the NavigationView in the MaterialComponents libarry you can use:
app:itemShapeAppearanceOverlay attribute to define a custom shape for the each item
app:itemShapeFillColor attribute to define the color used to fill the shape
Something like:
<com.google.android.material.navigation.NavigationView
app:itemShapeFillColor="#color/selector_menu"
app:itemShapeAppearanceOverlay="#style/ShapeAppearanceOverlay.Nav"
android:theme="#style/ThemeOverlay.NavigationView"
../>
where the selector is something like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.50" android:color="#color/primaryColor" android:state_pressed="true"/>
<item android:alpha="0.12" android:color="#color/primaryColor" android:state_activated="true"/>
<item android:alpha="0.12" android:color="#color/primaryColor" android:state_checked="true"/>
<item android:color="#android:color/transparent"/>
</selector>
and the shape is defined by:
<style name="ShapeAppearanceOverlay.Nav" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">50%</item>
<item name="cornerSizeBottomRight">50%</item>
<item name="cornerSizeBottomLeft">0dp</item>
<item name="cornerSizeTopLeft">0dp</item>
</style>
Also use the android:theme to override the color used by the ripple.
<style name="ThemeOverlay.NavigationView" parent="">
<item name="android:colorControlHighlight">#android:color/transparent</item>
</style>

I'm not sure what you want but I think this will solve your problem.
nav_item_background_round.xml
<?xml version="1.0" encoding="UTF-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f816a463"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" />
</shape>
</item>
</ripple>

I also had the same issue. I resolved it by
First - Adding a custom style to my navigationView app:theme="#style/NavigationDrawerStyle"
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fadingEdge="none"
android:fitsSystemWindows="true"
android:overScrollMode="never"
app:itemIconPadding="12dp"
app:itemBackground="#drawable/drawer_background_selector"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#drawable/drawer_item_icon_color"
app:itemTextColor="#drawable/drawer_item_text_color"
app:theme="#style/NavigationDrawerStyle"
app:menu="#menu/activity_main_drawer" >
Second - Then in my styles.xml created a custom drawer style NavigationDrawerStyle then
setting colorControlHighlight to transparent which removed the rectangular shape for me.
<style name="NavigationDrawerStyle" parent="ThemeOverlay.AppCompat.navTheme">
<item name="android:textSize">14sp</item>
<item name="android:fontFamily">#font/robotomedium</item>
<item name="android:colorControlHighlight">#android:color/transparent</item><!--Here Removes Rectangle-->
</style>

Related

Navigation drawer item highlight color exceeds over the radius of the corners

Navigation drawer item highlight color exceeds over the radius of the corners when I select (long press/ hold down) the item.
<...NavigationView in activity_main.xml
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="330dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="0.1dp"
android:layout_marginBottom="0.1dp"
android:background="#drawable/corner_radius_nav_main"
android:clipToPadding="false"
android:fitsSystemWindows="false"
android:paddingStart="8dp"
android:paddingEnd="8dp"
app:headerLayout="#layout/nav_header"
android:theme="#style/NavigationItemHighLightColor"
app:itemIconPadding="17dp"
app:itemTextAppearance="#style/Font.GoogleSans"
app:itemBackground="#drawable/nav_item_bg"
app:itemIconTint="#color/nav_item_color"
app:itemTextColor="#color/nav_item_color"
app:menu="#menu/drawer_menu" />
nav_item_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="2dp"
android:right="2dp"
android:top="1dp"
android:bottom="1dp">
<shape>
<solid android:color="#color/nav_item_color_bg"/>
<corners android:radius="7dp"/>
</shape>
</item>
</layer-list>
nav_item_color_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#E1EFEE"
android:state_checked="true" />
<item
android:color="#E1EFEE"
android:state_pressed="true" />
<item
android:color="#android:color/transparent"/>
</selector>
themes.xml
<style name="NavigationItemHighLightColor" parent="Theme.AppCompat.Light">
<item name="colorControlHighlight">#B2DFDB</item>
</style>
I really appreciate your help.
Thank you.
To define the item background remove app:itemBackground and use:
app:itemShapeFillColor: background item color
app:itemShapeAppearanceOverlay: item shape
Something like:
<com.google.android.material.navigation.NavigationView
app:itemShapeFillColor="#color/selector_menu"
app:itemShapeAppearanceOverlay="#style/ShapeAppearanceOverlay.Nav"
android:theme="#style/ThemeOverlay.NavigationView"
../>
where #color/selector_menu is a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.50" android:color="#color/...." android:state_pressed="true"/>
<item android:alpha="0.12" android:color="#color/...." android:state_activated="true"/>
<item android:alpha="0.12" android:color="#color/..." android:state_checked="true"/>
<item android:color="#android:color/transparent"/>
</selector>
and:
<style name="ShapeAppearanceOverlay.Nav" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">7dp</item>
</style>
<style name="ThemeOverlay.NavigationView" parent="">
<item name="android:colorControlHighlight">#android:color/transparent</item>
</style>

How do I make the rounded corners of the menu in the toolbar?

This is my toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
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/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/_40sdp"
android:background="#android:color/holo_red_dark"
android:gravity="center_vertical"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/PopUpTheme"/>
This is how I customized the popup menu in style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="PopUpTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:popupBackground">#drawable/background_popup_menu</item>
</style>
</resources>
background_popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white" />
<corners android:radius="5dp" />
</shape>
That's what I get in the end.
But I need to do like this
You can change the background of the overflow menu defining in your app theme the actionOverflowMenuStyle attribute.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionOverflowMenuStyle">#style/popupOverflowMenu</item>
</style>
With:
<style name="popupOverflowMenu" parent="#style/Widget.MaterialComponents.PopupMenu.Overflow">
<item name="android:popupBackground">#drawable/my_mtrl_popupmenu_background</item>
</style>
Then define the drawable/my_mtrl_popupmenu_background.xml with something like:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSurface"/>
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
But you have rounded corners :) Just try to increase a value of radius like <corners android:radius="25dp" />.

Android Gradient Background drawable does not work

I've created this drawable resource, backgroundactivity, to use as my app's background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<shape
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<gradient
android:angle="90"
android:startColor="#BFEFFF"
android:centerColor="#B0E2FF"
android:endColor="#82CFFD"
android:type="linear"/>
</shape>
</item>
</selector>
I want to set this background on all my activity, so I've tried to put this drawable resource in the styles.xml like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:background">#drawable/backgroundactivity</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="#android:background">#color/colorPrimary</item>
</style>
</resources>
But in this way it didn't work: the background still stay white as default. I've also tried to replace android:background with android:windowBackground, but it causes my application to be completely black.
Thanks in advance!
Try to use windowBackground instead of background
<item name="#android:windowBackground">#color/colorPrimary</item>
It worked by simply editing the background activity.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#BFEFFF"
android:centerColor="#B0E2FF"
android:type="linear" />
</shape>
</item>
</selector>

Change the color of a disabled button in android

Is there a way to change the color of a disabled button in android through styles or some other form ?
I currently have the following,
drawable/button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_default_shape"/>
</selector>
drawable/button_default_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_default_background"/>
<corners android:radius="3dp"/>
</shape>
values/styles.xml
<style name="AppTheme.Button">
<item name="android:background">#drawable/button_default</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">17sp</item>
<item name="android:textAppearance">#style/CustomFontAppearance</item>
</style>
You'll have to use a selector for different drawables in those states.
You can create a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/your_enabled_drawable" android:state_enabled="true" />
<item android:drawable="#drawable/your_disabled_drawable" android:state_enabled="false" />
<!-- default state-->
<item android:drawable="#drawable/your_enabled_drawable" />
</selector>
Specify the color in selector for android:state_enabled="false" as drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#32ff09" />
</shape>
</item>
</selector>
And apply this drawable resource to background of button
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/drawble_name"
android:enabled="false"
android:text="Selector Applied Button" />
Try this -
drawable/bg_button.xml :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_button_focused" android:state_selected="true"/>
<item android:drawable="#drawable/bg_button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_button_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/bg_button_normal"/>
</selector>
After this just set background on your button like this -
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_button"
android:text="Button"/>
UPDATE: For Android MaterialButton
For Android MaterialButton there are two solutions, one for SDK >= v21 and another for SDK < v21.
Here I am also giving how to change the text color of the button depending whether it is enabled or disabled.
First create two color selector .xml file in your color folder. One for button color and another for button text color. For example button_state_colors.xml for button color and button_text_state_colors.xml for text color.
Here are those two color files:
button_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#303F9F" />
<item android:state_enabled="false" android:color="#BBBBBB" />
</selector>
button_text_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#FFFFFF" />
<item android:state_enabled="false" android:color="#555555" />
</selector>
Now for all versions of Android SDK you can change the color of button and it's text using style like:
in style.xml:
<style name="Widget.AppTheme.MaterialButton" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/button_state_colors</item>
<item name="android:textColor">#color/button_text_state_colors</item>
</style>
in layout.xml:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
style="#style/Widget.AppTheme.MaterialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Only for SDK >= 21 you can direct change button color without using style like:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/button_state_colors"/>
Another way to achieve this is to create a color selector.
Create a file
res/color/your_color.xml
which looks like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorDisabledBackground" android:state_enabled="false" />
<item android:color="#color/colorEnabledBackground" />
</selector>
Then you may use it as a regular color
in a style:
<style name="YourStyle">
<item name="android:background">#color/your_color</item>
<item name="android:textColor">#android:color/black</item>
</style>
As well is in layouts, shapes or in code as etc.
Here is my code which works properly with button enable and disable state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/ic_button_gradient"/>
<item android:state_enabled="false" android:drawable="#color/gray"/>
</selector>

Changing button style in the whole application

I'm trying to change all the TextColor of my buttons in my app to white and also trying to make it bold. But that isn't happening, I'm overwriting the android:Widget.Button
I'm developing for Jelly Bean 4.1.2
What am I doing wrong?
Theme definition in manifest
android:theme="#style/spui" >
The theme where I like to
<style name="spui" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:buttonStyle">#style/Buttonspui</item>
</style>
The style for the button itself
<style name="Buttonspui" parent="android:Widget.Button">
<item name="android:background">#drawable/btn_default_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>
The button
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/edit"
android:id="#+id/btnEdit"
style="#style/Buttonspui"/>
For styling your Button you can use this:
Go to the drawable folder and make an XML (e.g. button.xml) file called "style" which contains the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" />
<stroke android:width="1px" android:color="#000000" />
<corners android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
</selector>
This is my code, you can make the necessary changes you want
Now call it in your layout XML (mainactivity.xml) like this
android:background="#drawable/button.xml"
Now to change the font color and style you can use the following which comes as part of the styles.xml in the values folder
<style name="buttonStyle" parent="#android:style/Widget.Button.Small">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>
Now call this in the layout XML (mainactivity.xml) like this
style="#style/buttonStyle"
The final code is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/edit"
android:id="#+id/btnEdit"
android:background="#drawable/button.xml"
style="#style/buttonStyle"
/>
Hope this helps :)
This will change the default button style for the entire app, include alert dialog button. Adjust the style based on yours.
res/values/styles.xml
<resources>
<!-- Your base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Other styles -->
<item name="buttonStyle">#style/MyCustomButton</item>
</style>
<style name="MyCustomButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/selector_button</item>
<item name="android:textColor">#color/buttonText</item>
<item name="android:textStyle">bold</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
</style>
</resources>
res/drawable/selector_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/button_selected" android:state_focused="true"/>
<item android:drawable="#drawable/button_unselected"/>
</selector>
res/drawable/button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#color/buttonSelected"/>
</shape>
res/drawable/button_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#color/buttonUnselected"/>
</shape>

Categories

Resources