Background xml not working - android

I have buttons and i want to set background xml for them, and when a user press any button i want to change its background , i work like this
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="center"
android:background="#drawable/button_selector"
android:text="#string/b_cancel" />
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/button_bg_hover" android:state_pressed="true"/>
<item android:drawable="#drawable/button_bg_hover" android:state_pressed="false" android:state_selected="true"/>
</selector>
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding android:top="10dip"
android:bottom="10dip"/>
<!-- Gradient Bg for listrow -->
<gradient
android:useLevel="false"
android:angle="270"
android:centerColor="#000000"
android:endColor="#FFFFFF"
android:startColor="#808080" />
</shape>
button_bg_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<padding
android:bottom="10dip"
android:top="10dip" />
<!-- Gradient Bg for Button -->
<gradient
android:angle="270"
android:centerColor="#000000"
android:endColor="#FF0000"
android:startColor="#808080" />
</animation-list>
it works good as a background but when i press the button it becomes all white, inspire of my button_bg_hover , why pelase? what is the correct ?

it could be the misplaced -tag, change the line
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
.....
</animation-list>
to
<shape xmlns:android="http://schemas.android.com/apk/res/android">
.....
</shape>

Related

layout clickable selector doesn't work - why?

Create a login page. but I don't know what problem this code
login button don't work change color when I clicked!
I want to when I click then change color button
why doesn't changed that 'LOGIN' button?
how I can solve this?
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="16dp"
android:background="#drawable/btn_login"
android:clickable="true"
android:gravity="center"
android:paddingLeft="32dp"
android:paddingRight="32dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/icon_user" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="LOGIN"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
selector xml ->
btn_login.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="#4DAEEA" />
<corners android:radius="30dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#00ff00" />
<corners android:radius="30dp" />
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#999" />
<corners android:radius="30dp" />
</shape>
</item>
</selector>
you should make LinearLayout behave like a button by adding style and removing background , follow these steps
<LinearLayout
......
style="#style/btn_stand"
.......
>
</LinearLayout>
My style definition for the button:
<style name="btn_stand" parent="AppBaseTheme">
<item name="android:background">#drawable/btn_stand_sel</item>
<item name="android:textColor">#drawable/btn_stand_text_color</item>
<item name="android:minHeight">48dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">5dp</item>
</style>
My #drawable/btn_stan_sel file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:drawable="#drawable/btn_stand_disabled" android:state_enabled="false"/>
<!-- enabled and pressed state -->
<item android:drawable="#drawable/btn_stand_pressed" android:state_enabled="true" android:state_pressed="true"/>
<!-- enabled and focused state -->
<item android:drawable="#drawable/btn_stand_focused" android:state_enabled="true" android:state_focused="true"/>
<!-- enabled state -->
<item android:drawable="#drawable/btn_stand_enabled" android:state_enabled="true"/>
</selector>
My drawable file repeated for each state just with different colors for each state:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/stroke" />
<solid android:color="#color/blue" />
<corners android:radius="6dp" />
</shape>
this was an example that works for me , hope you use it and choose the color you want
Add android:focusable="true"to your linear layout xml code.
In the event that that doesn't work, you can try to replace android:background=#drawable/btn_login with android:foreground="?android:attr/selectableItemBackground".

Unable to change border color of Edit Text on focus

I am trying to color my EditText border as blue when it is having focus. I tried all possible ways as suggested in many answers in Stack Overflow but it doesn't work. It is only changing color of cursor and not of whole border when focused. Below is what I tried, let me know if I am doing incorrectly
That grey border to blue color
styles.xml
<!--Edit Text-->
<style name="EditTextTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FF1792E5</item>
<item name="colorControlHighlight">#FF1792E5</item>
</style>
edit_text_border.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke
android:width="1dp"
android:color="#FF1792E5"
/>
</shape>
</item>
</selector>
XML
<EditText
android:id="#+id/edtName"
android:background="#drawable/edit_text_border"
android:inputType="text|textCapWords"
android:maxLength="40"
android:textColor="#color/black"
android:singleLine="true" />
Manifest:
<activity
android:name=".ui.activity.MyActivity"
android:theme="#style/EditTextTheme"
android:screenOrientation="portrait" />
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="#android:drawable/edittext_pressed"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/edittext_focused_blue"
/> <!-- focused -->
<item
android:drawable="#android:drawable/edittext_normal"
/> <!-- default -->
</selector>
try this
Try this create a folder in res/drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#color/block" android:width="5dp"/>
<solid android:color="#ffffff"/>
</shape>
XML:
android:background="#drawable/yourborder"
Here is output.
Another Way:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_pressed="true" android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/twitter_im_edittext_focused" />
<item android:state_enabled="true" android:drawable="#drawable/twitter_im_edittext_normal" />
<item android:state_focused="true" android:drawable="#drawable/twitter_im_edittext_focused" />
<item android:drawable="#drawable/twitter_im_edittext_normal" />
</selector>
I hop this may help you.
try this create a edittext_focused_blue in res/drawable folder like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#ff00" />
<corners android:radius="5dp" />
</shape>
this create a edittext_normal in res/drawable folder like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
try this create a edittext_bg in res/drawable folder like this
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_focused="true"
android:drawable="#drawable/edittext_focused_blue"
/> <!-- focused -->
<item android:state_focused="false"
android:drawable="#android:drawable/edittext_normal"
/> <!-- default -->
</selector>
than apply to your editext using android:background="#drawable/edittext_bg" like this
<EditText
android:id="#+id/edtName"
android:background="#drawable/edittext_bg"
android:inputType="text|textCapWords"
android:maxLength="40"
android:textColor="#color/black"
android:singleLine="true" />
Please try below code to achieve your design:
Drawable Code:
et_border1.xml Drawable for unselected or unfocused edittext.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke
android:width="1dp"
android:color="#ffffff" />
</shape>
</item>
</selector>
et_border2.xml Drawable for selected or focused edittext.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#FF1792E5" />
</shape>
</item>
</selector>
edit_text_border drawable to show selected or unselected edittext
<?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/et_border2" />
<item android:state_focused="true" android:drawable="#drawable/et_border2" />
<item android:drawable="#drawable/et_border1" />
</selector>
In 'layout file'
<EditText
android:id="#+id/edtName"
android:inputType="text|textCapWords"
android:maxLength="40"
android:textColor="#android:color/black"
android:singleLine="true"
android:layout_height="40dp"
android:background="#drawable/edit_text_border"
android:layout_width="match_parent"/>
Add or remove color according to requirement
I hope it work for you.
I have tested below code and it works very well.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ediittext_selector"
android:hint="Demo"
/>
ediittext_selector.xml
<?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/edit_text_border_selected" />
<item android:state_focused="true" android:drawable="#drawable/edit_text_border_selected" />
<item android:drawable="#drawable/edit_text_border" />
</selector>
edit_text_border_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#737373" />
<stroke
android:color="#FF4081"
android:width="1dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
edit_text_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#737373" />
<stroke
android:color="#F78C1F"
android:width="1dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>

Add custom color to touch feedback in android

To give users visual feedback when they touch a view, I do
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" />
But ?attr/selectableItemBackground is a gray color. I want to use a different color. To do that I do
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android.com/apk/res/android">;
<item android:drawable="#color/mine" android:state_selected="true"></item>
<item android:drawable="#color/mine" android:state_pressed="true"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
but it does not work, even after I set clickable="true" for the view in question.
Follow the code
button_normal.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_light_green"/>
<corners android:radius="5dp" />
</shape>
button_selected.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_light_green_selected"/>
<corners android:radius="5dp" />
</shape>
button_background.xml
<?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_selected"/>
<item android:state_focused="true" android:drawable="#drawable/button_selected"/>
<item android:drawable="#drawable/button_normal"/>
</selector>
assign button_background.xml as background for the button by changing the colors which u desire. Hope it works!!!

Android set background color button pressed

I would like to change the background color of my ImageButton on pressed event.
Here is what i am done :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/rose_normal" />
<solid
android:state_pressed="true"
android:color="#color/rose_fonce" />
<stroke
android:width="1sp"
android:color="#color/rose_fonce" />
</shape>
My button is well in "rose_normal" color, but never in "rose_fonce" color on press.
Where is the problem ?
EDIT : PROBLEM SOLVED :
Create one xml file called background_rounded_button.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rounded_button_rose_fonce" android:state_selected="true"></item>
<item android:drawable="#drawable/rounded_button_rose_fonce" android:state_pressed="true"></item>
<item android:drawable="#drawable/rounded_button_rose_normal"></item>
</selector>
rounded_button_rose_fonce.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/rose_fonce" />
<stroke
android:width="1sp"
android:color="#color/rose_fonce" />
</shape>
rounded_button_rose_normal.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/rose_normal" />
<stroke
android:width="1sp"
android:color="#color/rose_fonce" />
</shape>
And finally, apply background for the button :
<ImageButton
android:id="#+id/all_annonce_button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/background_rounded_button.xml"
android:padding="16dp"
android:src="#drawable/ic_action_search" />
The problem is that you are not using a selector, but a shape.
Try this code (button_selector.xml, put it in your drawable folder):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/rose_fonce" android:state_selected="true"></item>
<item android:drawable="#color/rose_fonce" android:state_pressed="true"></item>
<item android:drawable="#color/rose_normal"></item>
</selector>
When setting this selector as the background of a Button, it will have the "rose_normal" color in normal state, and the "rose_fonce" color when pressed or selected.
Example:
<Button
android:background="#drawable/button_selector" />

Selector.xml is not working when clicked on the image button

I use the following selector to change image of image button, when I click on it. But this does not work most of the times. Only for few buttons it works. Don't know what the cause here is
mapselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Pressed -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/mapiconover"/>
</selector>
And in the button,
Am doing like this,
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/btnMap" android:layout_centerInParent="true" android:src="#drawable/mapselector"
android:background="#drawable/map" android:scaleType="fitXY"
android:layout_alignParentBottom="true">
</ImageButton>
but still image is not changing when I click.
Any guess for this?
you have create like this in the ImageButton xml
create xml file using the button image like this
<?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/backbutton" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add that xml file as background for IMageButton
<ImageButton
android:layout_height="50px"
android:layout_width="50px"
android:id="#+id/settings"
android:background="#drawable/settings_button" //setting_button in
the xml file
android:text="Settings"/>
Not sure if this will work but you could try changing your selector to this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Pressed -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/mapiconover"/>
<item android:drawable="#drawable/map" />
</selector>
And your image button to this:
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/btnMap"
android:layout_centerInParent="true"
android:background="#drawable/mapselector"
android:scaleType="fitXY"
android:layout_alignParentBottom="true">
</ImageButton>
I had the same problem. For me it was because of
android:state_xxxx
conflict. It solved by clarifying the conditions between enabled and pressed. I used the following code to get different states between press and normal state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" >
<shape
android:shape="rectangle">
<gradient android:startColor="#1094ff"
android:endColor="#7eb6e3"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#505050" />
</shape>
</item>
<item android:state_pressed="true" android:state_enabled="true">
<shape
android:shape="rectangle">
<gradient android:startColor="#0075d4"
android:endColor="#0070cb"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#ff0000" />
</shape>
</item>
<item android:state_enabled="false" >
<shape
android:shape="rectangle">
<gradient android:startColor="#b9b9b9"
android:endColor="#848484"
android:angle="270" />
<corners android:radius="#dimen/fab_margin" />
<stroke android:width="2px"
android:color="#505050" />
</shape>
</item>
</selector>
This example uses different gradient and stroke for different states. You can also use your drawable instead. Just create a file like "mybutton.xml" in your drawable folder. then set the background property of your button like this:
android:background="#drawable/mybutton"
I hope it be helpful.

Categories

Resources