Android Button animation (color change) - android

I need to do an animation for my button that changes its color from inside out without any flickering.It means the color should change from the center and the button should be stable.The button should not shrink or fade.The button color should change from the center point of button to the overall button.

res/drawable/effect.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"/>
<item android:state_pressed="true" android:drawable="#color/your_red_color" />
</selector>
Add effect.xml as background of button:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/effect"
android:text="Click Me" />

Related

RadioButton styled as a normal button

I'm trying to style my RadioButton as normal buttons and support color change on the buttons, with ripple effect when one of them is clicked.
What happens now, is that all buttons are grey, they ripple in purple when they are clicked but don't change their color to purple permenently - which is the problem I'm trying to solve.
Thisi s how he buttons look right now
RadioButton in whatever.xml
<RadioButton
style="#style/Widget.AppCompat.Button"
android:id="#+id/product_size2"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="S"
android:button="#null"
android:textColor="#color/material_light_white"/>
Relevant items in style.xml
<item name="android:colorButtonNormal">#color/material_grey_700</item>
<item name="android:colorControlHighlight">#color/material_purple_500</item>
I want the buttons to stay purple when they are chosen - solution?
radiobutton_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#color/material_purple_500" />
<item android:state_checked="false" android:drawable="#color/material_grey_700" />
</selector>
And use it as a Background:
android:background="#drawable/radiobutton_background" />

Change hover image button without selector on android

I have many buttons for my view. Each button has different image resource. I want to change the click and hover effect for each button like this:
Button 1
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn1_selector"
android:text="name"
/>
btn1_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/hover_btn" android:state_pressed="true"></item>
<item android:drawable="#drawable/hover_btn" android:state_focused="true">/item>
<item android:drawable="#drawable/btn1"></item>
</selector>
Button 2
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn2_selector"
android:text="name"
/>
btn2_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/hover_btn" android:state_pressed="true"></item>
<item android:drawable="#drawable/hover_btn" android:state_focused="true">/item>
<item android:drawable="#drawable/btn2"></item>
</selector>
// etc...
The problem is that I have to create many selectors for all buttons. But I just need to change the hover state for all of them with 1 image hover_btn.png. Is there any way to have a hover state by default without creating all selectors for all buttons?

Custom button which will change text color, background color and drawable on click

I have a Button XML file in /drawable/ which changes states of the button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img_pressed"
android:state_pressed="true"/>
<item android:drawable="#drawable/img_normal"
android:state_focused="true" />
<item android:drawable="#drawable/img_normal"/>
</selector>
And this Ok if I will change background only on click event.
But now, I have a button with left drawable, and on click I have to change left drawable, text color and button background.
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My button"
android:textColor="#android:color/white"
android:drawableLeft="#drawable/btn_leftDrawable"
android:background="#android:color/transparent"
/>
Can I do this inside this XML and how? I don't want to make spaghetti code by doing these extra tasks inside Java code in setOnClickListener method.
Create three different xml selector and add them to the corresponding attributes..
Sample:
TextColor:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/click"/>
<item android:color="#color/normal"/>
Background:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/buttonBackgroundClick"/>
<item android:color="#android:color/transparent"/>
Add them to your button:
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My button"
android:textColor="#drawable/textColor"
android:drawableLeft="#drawable/btn_leftDrawable"
android:background="#drawable/background"
/>

How can add imagebutton click effect?

I am working with android. I want to add onClick() effect on ImageButton preferably a color around the sides of the button after selected. I had tried with alpha effect.But It looks dark even after get back after click.
How can add ImageButton click effect?
you can use a selector xml like as follows :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
and set it as a background of your Button.

Problem with ImageButton(default yellow color)

I have an ImageButton that it changes it's background when we tap on it to a blue color.
However, when i tap on it i have the blue color as background but i have also a yellow one(the default that Android uses in any button).
IMAGE LINK
How can i solve this problem ?
Thank you for your help.
<ImageButton android:id="#+id/test" android:layout_below="#id/button" android:src="#drawable/settings_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"/>
This XML file(settings_xml.xml) is in /res/drawable-mdpi
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/blue" /> <!-- pressed -->
<item android:drawable="#drawable/stock" /> <!-- default -->
</selector>
Have i to put the XML file in ldpi and hdpi ?
You can set Background Color of Image Button to Yellow,so whenever you tap on Image Button only yellow color will be displayed & not the Blue Color.

Categories

Resources