android button ripple effect border color - android

Iv've been trying to match a button solid color with its border color while also adding a ripple effect. I've have a matching border and solid color (without a ripple) but when I add the ripple it seems to add a border of its own thats slightly darker. Below are the styles and drawable,
drawable-v21/my_btn.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="#ff0000"
tools:ignore="NewApi">
<item>
<shape
android:shape="rectangle">
<corners
android:radius="2dp" />
<stroke
android:width="1dp"
android:color="#ff0000" />
<solid
android:color="#ffffff" />
</shape>
</item>
</ripple>
styles.xml
<style name="Border_Button">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">14sp</item>
<item name="android:textAllCaps">true</item>
<item name="android:layout_marginLeft">16dp</item>
<item name="android:layout_marginRight">16dp</item>
<item name="android:background">#drawable/my_btn</item>
</style>
and layout.xml
<Button
style="#style/Border_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Click" />
Does anyone know how this border color can be overridden with the ripple or is there is some other way to achieve this (only XML preferrably)?

Related

AppButton Change color on click

I am trying to have a button changing color (background and text) when I click on it. Like, by default the button has a grey background and white text color, when you click on it, the background become white and text grey. When you reclick on it, it come back to grey background and white text color.
I tried this:
androidx.appcompat.widget.AppCompatButton
style="#style/AppTheme.Button"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:minWidth="60dp"
android:padding="11dp"
android:textColor="#color/button_text_color"
android:layout_margin="7dp"/>
The AppThemeButton is :
<style name=AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="android:textSize">16sp</item>
<item name="android:letterSpacing">0.06</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#drawable/corner_shape</item>
<item name="android:textAllCaps">false</item>
</style>
then my corner_shape is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<corners android:radius="15dp" />
<solid android:color="#color/brand04" />
</shape>
</item>
<item android:state_enabled="true">
<shape>
<corners android:radius="15dp" />
<solid android:color="#color/brand01" />
</shape>
</item>
</selector>
and the button_text_color as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/brand05" android:state_enabled="true"/>
<item android:color="#color/white"/>
</selector>
I was trying to play with enable and disable which was a terrible idea as when I set the button to enable=false I cannot reverse back as I cannot act using the click listener..
Any idea. I am kind of looking of for a toogle function for the color.
Thanks

how to change border and color in TextInputLayout unfocus?

How to change border and color in TextInputLayout unfocus?
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_height="wrap_content"
app:boxStrokeWidth="4dp"
app:boxStrokeColor="#color/colorPrimary"
app:layout_constraintTop_toBottomOf="#id/a">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:hint="#string/app_name"
android:textColorHint="#color/colorPrimary"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
I try :
app:boxStrokeWidth="4dp"
app:boxStrokeColor="#color/colorPrimary"
but, not work in unfocus
If you want to have different stroke width in focused/unfocused mode you can use the boxStrokeWidth and the boxStrokeWidthFocused attributes.
<!-- The value to use for the box's stroke when in outline box mode,
or for the underline stroke in filled mode. -->
<attr format="dimension" name="boxStrokeWidth"/>
<!-- The value to use for the focused box's stroke when in outline box mode,
or for the focused underline stroke in filled mode.. -->
<attr format="dimension" name="boxStrokeWidthFocused"/>
Something like:
<com.google.android.material.textfield.TextInputLayout
app:boxStrokeWidthFocused="4dp"
app:boxStrokeWidth="1dp"
app:boxStrokeColor="#color/text_input_layout_stroke_color"
..>
For the stroke color you can define a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/..." android:state_focused="true"/>
<item android:alpha="0.87" android:color="#color/..." android:state_hovered="true"/>
<item android:color="#color/.." android:state_enabled="false"/>
<item android:color="#color/..."/> <!--unfocused-->
</selector>
Note: the boxStrokeWidthFocused requires at least the version 1.2.0-alpha04.
Changing the border;
Use drawable selector for TextInputLayout 's background
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorRipple">
<item
android:state_enabled="true"
android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke android:color="#aA0000" android:width="1dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#dddddd" />
<stroke android:color="#ff00ff" android:width="1dp"/>
</shape>
</item>
</selector>
Changing the text color;
Use drawable selector for TextInputEditText 's textcolor
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#333333" android:state_selected="true" />
<item android:color="#333333" android:state_focused="true" />
<item android:color="#009900" /> <!-- default case -->
</selector>

TextInputLayout with outline but transparent background

If boxBackgroundColor is transparent then boxStroke is invisible
<style name="OutlinedRoundedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxBackgroundColor">#android:color/transparent</item>
<item name="boxStrokeColor">#android:color/white</item>
<item name="boxStrokeWidth">4dp</item>
</style>
So is it only possible to have transparent TextInputLayout without outline using custom background?
E.g.:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#color/colorEditTextOutline" />
</shape>
</item>
</layer-list>
If you had posted a design for it then it would have been easier understand. For this solution If I understand you correctly, you need to add these two things
In style add:
<style name="InputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/text_input_box</item>
</style>
Add color resource:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red_bright" android:state_focused="true" />
<item android:color="#color/red_bright" android:state_hovered="true" />
<item android:color="#color/red_bright" />
</selector>
This solution will have something like this if this is what you want

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>

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