How to Override material design statepressed in for Android Button - android

Hello Learned Friends,
I am using Material Design Theme but I need to overried its statePressed so that when the button is clicked it changes color (highlighted for a moment) as demonstrated below.
For this I have a drawable which I set on the buttons as follows
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape>
<solid android:color="#color/primaryColor" />
<stroke android:color="#color/primaryDarkColor" android:width="#dimen/stroke_width"/>
<corners android:radius="#dimen/corner_radius" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="#color/secondaryColor" />
<stroke android:color="#color/secondaryLightColor" android:width="#dimen/stroke_width" />
<corners android:radius="#dimen/corner_radius" />
</shape>
</item>
I also got a ThemeOverlay to override the state pressed
<style name="ThemeOverlay.Red.UnitedStates" parent="">
<item name="android:colorPressedHighlight">#color/secondaryColor</item>
</style>
Unfortunately when I click the buttons the highlight doesn't happen.
What could I be missing?
This is Material Theme XML
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryVariant">#color/primaryLightColor</item>
<item name="colorPrimaryDark">#color/primaryDarkColor</item>

You can use just the app:backgroundTint attribute:
<com.google.android.material.button.MaterialButton
app:backgroundTint="#color/custom_button_selector"
../>
with:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red600" android:state_pressed="true"/>
<item android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>
Normal state:
Pressed state:

Related

Add custom shape ripple with selector on navigationview menu item

I want to add a ripple effect with rounded corners rectangle with a selector for navigation view item. But it keeps adding the gray rectangle ripple effect.
navigation view
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:itemIconTint="#color/drawer_item"
app:itemTextColor="#color/drawer_item"
app:itemBackground="#drawable/drawer_item_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="#menu/bottom_app_bar_menu"/>
drawer_item_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/drawer_selected_item_bg"/>
<item android:state_checked="false" android:drawable="#android:color/transparent"/>
<item android:state_pressed="true" android:drawable="#drawable/custom_ripple_navitem"/>
<item android:state_selected="true" android:drawable="#drawable/custom_ripple_navitem"/>
<item android:state_focused="true" android:drawable="#drawable/custom_ripple_navitem"/>
custom_ripple_navitem.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#360074FF">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
</shape>
</item>
The result is
With the Material Components you can use something like:
<com.google.android.material.navigation.NavigationView
app:itemShapeAppearanceOverlay="#style/ShapeAppearanceOverlay.NavItem.Rounded"
app:itemShapeFillColor="#color/selector_navitem"
android:theme="#style/ThemeOverlay.NavItem.Ripple"
../>
With the app:itemShapeAppearanceOverlay attribute you can define a custom shape for the each item:
<style name="ShapeAppearanceOverlay.NavItem.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
With the app:itemShapeFillColor attribute you can define the color used to fill the shape:
<?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>
Finally use the android:theme to override the color used by the ripple.
<style name="ThemeOverlay.NavItem.Ripple" parent="">
<item name="android:colorControlHighlight">#android:color/transparent</item>
</style>
Note: it requires the version 1.1.0 of the Material Components library.
"Solution"
I started experimenting with ripple effect when I found this question, and after some time I finished with this code.
Note: It is not real ripple effect, just a fake one. Let me know if you'll make an improved version of it.
#layout/your_layout.xml
<android.support.design.widget.NavigationView
...
android:theme="#style/Widget_NavigationItem_NoRipple"
app:itemBackground="#drawable/drawer_item_background"/>
#values/styles.xml
<style name="Widget_NavigationItem_NoRipple">
<item name="android:colorControlHighlight">#android:color/transparent</item>
</style>
#drawable/drawer_item_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawer_item_shape" android:state_checked="true"/>
<item android:drawable="#drawable/drawer_item_ripple" android:state_pressed="true"/>
<item android:drawable="#android:color/transparent" android:state_pressed="false"/>
</selector>
#drawable/drawer_item_shape
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="#color/selectedItemBackgroundColor"/>
</shape>
</item>
</layer-list>
#drawable/drawer_item_ripple
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/transparent">
<item>
<shape android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="?attr/colorControlHighlight"/>
</shape>
</item>
</ripple>

Android - Put button style into theme

i created this drawable to use in my buttons in my application:
btnprimary.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btnprimary_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/btnprimary_pressed" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="#drawable/btnprimary_focused" android:state_enabled="true" android:state_focused="true" />
<item android:drawable="#drawable/btnprimary_enabled" android:state_enabled="true" />
</selector>
Every state has a drawable too, so, for example, this is the file for btnprimary_enabled
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="5dp"
/>
<gradient
android:angle="90"
android:startColor="#335BB8"
android:endColor="#6397FF"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#878787"
/>
</shape>
So, i can use this in a button like this, using the background property:
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btnprimary"
android:text="#string/login"
android:textColor="#FFFFFF"
android:gravity="center"/>
And i have my theme on styles.xml, but is empty now. Like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
My Question is, can i put those configuration that i created on drawable into my theme, and all buttons has this style, without need to add the background property?
Have you tried this code ? In your style file, define the button style
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">#style/yourButton</item>
</style>

Background colour of tabs

I am trying to add colours to tabs of action bar with the help of following link-https://github.com/thecodepath/android_guides/wiki/ActionBar-Tabs-with-Fragments
I added following statements to style.xml-
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabStyle">#style/MyTheme.ActionBar.TabView</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:background">#12ABEE</item>
<item name="android:actionMenuTextColor">#ffffff</item>
<item name="android:titleTextStyle">#style/myTheme.ActionBar.Text</item>
</style>
<style name="myTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
<item name="android:gravity">center</item>
</style>
<style name="MyTheme.ActionBar.TabView" parent="android:style/Widget.Holo.ActionBar.TabView">
<item name="android:background">#drawable/tab_bar_background</item>
</style>
Also i created tab_bar_background.xml in drawable-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom indicator color for the UNSELECTED tab state -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#65acee" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
<!-- SELECTED TAB STATE -->
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Tab background color for the SELECTED tab state -->
<item>
<shape>
<solid android:color="#cef9ff"/>
</shape>
</item>
<!-- Bottom indicator color for the SELECTED tab state -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#5beea6" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
With these steps in place it should show tab with light blue backgroung(#cef9ff) and green bottom line(#5beea6) on selected and blue bottom line and normal background on unselected.But unfortunately while running i see that bottom line appears fine but the background colour of tab appears black both for selected and unselected tabs.I am not able to figure out wht went wrong..
Updating tab_bar_background.xml to this solved my problem
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom indicator color for the UNSELECTED tab state -->
<item >
<shape >
<stroke android:color="#e8e8e8" android:width="25dp" />
</shape>
</item>
</layer-list>
</item>
<!-- SELECTED TAB STATE -->
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Tab background color for the SELECTED tab state -->
<!-- Bottom indicator color for the SELECTED tab state -->
<item>
<shape >
<stroke android:color="#ffd866" android:width="25dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>

Change tab color, ActionBarCompat

I want to change the color of my tab, this is my code
action_bar_stacked_background.xml
<!-- STATES WHEN BUTTON IS NOT PRESSED -->
<!-- Non focused states -->
<item android:drawable="#drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:drawable="#drawable/tab_unselected" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_selected" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
My styles.xml
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/action_bar_background</item>
<item name="background">#color/background_lista</item>
<item name="actionModeBackground">#color/actionbar_color</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
<item name="subtitleTextStyle">#style/MyActionBarSubtitleText</item>
<item name="homeAsUpIndicator">#drawable/ic_actionbar_back_button</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">#style/MyActionBarTabs</item>
Support library compatibility -->
<!-- <item name="background">#drawable/actionbar_background</item> -->
</style>
<style name="MyActionBarTabs" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/action_bar_stacked_background</item>
</style>
My values-v11/styles.xml
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/action_bar_background</item>
<item name="background">#color/actionbar_color</item>
<item name="android:actionModeBackground">#color/actionbar_color</item>
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<item name="android:subtitleTextStyle">#style/MyActionBarSubtitleText</item>
<item name="android:homeAsUpIndicator">#drawable/ic_actionbar_back_button</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabs</item>
<!-- Support library compatibility -->
<!-- <item name="background">#drawable/actionbar_background</item> -->
</style>
<style name="MyActionBarTabs" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/action_bar_stacked_background</item>
</style>
tab_selected.xml
<!-- draw bottom line to fill the spaces between tabs -->
<item android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#898989" />
</shape>
</item>
<!-- leave bottom line only 1px of height, the rest is masked out -->
<item
android:bottom="1px"
android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#color/color_background_stacked" />
</shape>
</item>
<!-- draw tab background -->
<item
android:left="#dimen/tab_space"
android:right="#dimen/tab_space">
<shape android:shape="rectangle" >
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="#dimen/corner_radius"
android:topRightRadius="#dimen/corner_radius" />
<gradient
android:angle="90"
android:endColor="#ccc"
android:startColor="#color/color_background_stacked" />
<stroke
android:width="1px"
android:color="#898989" />
</shape>
</item>
<!-- mask out the bottom line of the tab shape -->
<item
android:left="#dimen/tab_space_plus1"
android:right="#dimen/tab_space_plus1"
android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#color/color_underline_stacked" />
</shape>
</item>
and tab_unselected.xml
<item android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#898989" />
</shape>
</item>
<item
android:bottom="1px"
android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#color/color_background_stacked" />
</shape>
</item>
<item
android:left="#dimen/tab_space"
android:right="#dimen/tab_space">
<shape android:shape="rectangle" >
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="#dimen/corner_radius"
android:topRightRadius="#dimen/corner_radius" />
<gradient
android:angle="90"
android:endColor="#aaa"
android:startColor="#ddd" />
<stroke
android:width="1px"
android:color="#898989" />
</shape>
</item>
The tabs are shown with grey bcakground and blue underline, I just want to change de underline color. How can I solve it?
Greetings.
actionBarTabStyle attribute should be with the base Application/Activity theme , not under ActionBar style
Create a custom theme like this
<style name="AppTheme" parent="Theme.Base.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabView</item>
<item name="actionBarTabStyle">#style/MyActionBarTabView</item>
</style>
And, custom ActionBarTab theme like this
<style name="MyActionBarTabView" parent="Widget.AppCompat.Base.ActionBar.TabView">
<item name="android:background">#drawable/action_bar_stacked_background</item>
</style>
Set the AppTheme to whole Application theme or for an Activity . Let me know how it works.

How do I style RelativeLayout from styles.xml - Android

I am trying to style a RelativeLayout from styles.xml because I have different themes and I am trying to change it's background. How do I do that?
I know that styling a ListView goes like this (for example):
<style name="Theme_Day" parent="Theme.Sherlock.Light">
<item name="android:textColor">#color/def_actionbar_submenu_text</item>
<item name="android:listViewStyle">#style/def_ListView</item>
<item name="vpiTabPageIndicatorStyle">#style/CustomTabPageIndicator</item>
</style>
<style name="def_ListView" parent="#android:style/Widget.ListView">
<item name="android:background">#drawable/listview_bg</item>
<item name="android:textColor">#color/def_listview_title_text</item>
<item name="android:divider">#color/def_listview_itemdivider_bg</item>
<item name="android:dividerHeight">#dimen/listview_divider_height</item>
</style>
but when I try to find "android:relativeLayoutStyle" or parent="#android:style/Widget.RelativeLayout" I cant find anything..
How do I do that?
This question has been asked before a long time ago, but you can always try like this:
<style name="RelativeLayoutStyle" parent="#android:style/Widget">
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginTop">7dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:background">#android:color/holo_green_dark</item>
</style>
Now you can do:
<RelativeLayout
style="#style/RelativeLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
Hope this helps someone.
You don't need any parent just write
<style name="def_RelativeLayout">
<item name="android:background">#drawable/listview_bg</item>
</style>
and set it in your style attribute of every relative layout
Create a drawable file and then set android:background with the #drawable/layout_style property.
For example, this drawable file is used to create a Google Card like design:
background_card.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp" />
<solid android:color="#android:color/white" />
<padding android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
</item>
</layer-list>
And from the layout XML file, you need to set the android:background property:
android:background="#drawable/backgorund_card"
_
<RelativeLayout
android:id="#+id/layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgorund_card" >

Categories

Resources