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.
Related
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" />
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" />
So I have a transparent button I've defined like so:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:background="#drawable/ripple"
android:id="#+id/button_login"
android:layout_centerHorizontal="true" />
And I have a ripple background defined like so:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="?attr/colorPrimary"/>
</ripple>
Finally my color highlight and primary are defined like so:
<item name="colorPrimary">#android:color/transparent</item>
<item name="colorControlHighlight">#000000</item>
This works to make my button transparent, and if I change the primary color to say green, then I get a black color splash when pressing the button.
However when I set it to transparent it seems like the splash maintains the alpha. Am I doing something wrong or else, what do I need to do to make my transparent button have a splash of color?
The simplest answer would be: Do not specify the item drawable property in your ripple definition.
Change to following:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight"
>
</ripple>
The ripple effect will be in big circular form. Hope you are not bothered about it
I set CardView and inside of it I put RelativeLayout.
Layout looks something like this:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_basket_list_card_view"
android:orientation="horizontal"
android:layout_width="match_parent"
android:clickable="true"
android:layout_margin="5dp"
card_view:cardCornerRadius="#dimen/basket_list_cardview_corner_radius"
card_view:cardElevation="#dimen/basket_list_cardview_elevation"
card_view:cardUseCompatPadding="true"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground">
...
When I click on CardView on Lollipop phones, I get a nice ripple effect and everyone is happy. But, when I click on that CardView on pre-Lollipop phones, it only gets colored with some blue semi-transparent color. It looks ok with me, but I would like to change that color.
How can I do that?
Best,
Deveti
You can use
android:background="?attr/selectableItemBackground"
to get a light grey color instead of a blue one. It then uses the App's theme, which is probably the AppCompat theme in your case.
If you want to have full control I'm afraid you have to create your own state list and override selectableItemBackground or create a new item.
Example from the link:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
The drawable can also be a RippleDrawable. You can use the folder "drawable" for the default selection drawable and "drawable-21" for the ripple effect drawable. See this answer for a good overview.
If you use android:foreground it behaves like an overlay, so a transparent black color like #20000000 gives a grey tint.
Is it possible to change the text color of a textview if the textview is pressed?
I want to achieve a flashing effect with the color change only lasting as long as the button is pressed.
I know how to change the background of the textview with a selector list and the correct state but how can I change the color of text if the user pushes a button or a simple textview?
You can define a selector for colors as well. A short example that only distinguishes between pressed and all other states is:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#FFFFFF" />
<item
android:color="#4C566C" />
</selector>
For a full documentation on the selector see this unofficial documentation.
Put every color selector in a single file and put this files in a directory called color in the resources folder of your project.
Taken from official documentation:
XML file saved at res/color/button_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML will apply the color list to a View:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:textColor="#color/button_text" />
search for color selector to use in the
android:setTexColor
attr
you can change it using the setTextColor(ColorStateList) method
myTextView.setTextColor(myColorStates);
myTextView.setTextColor( 0xFFFF0000 )