android shape does not change size - android

Please help me for a layout on Android.
flat_btn_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#f45d5d"
android:centerColor="#f45d5d"
android:endColor="#f45d5d"/>
<size android:height="#dimen/activity_bottom_height_hover"/>
<stroke
android:width="2dp"
android:color="#f45d5d"/>
</shape>
flat_btn_bt_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#232323"
android:centerColor="#232323"
android:endColor="#232323"/>
<size android:height="#dimen/activity_bottom_height"/>
<stroke
android:width="1dp"
android:color="#3e3e3e"/>
</shape>
flat_btn_bt.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/flat_btn_bt_normal" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/flat_btn_bt_normal" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/flat_btn_hover" />
<item android:drawable="#drawable/flat_btn_bt_normal"/>
</selector>
Then, I set the background for the Button:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/flat_btn_bt"/>
But when working, it changes the color and don't change size layout.
Can you tell me how to change the size?

The shape scales to the size of the container View proportionate to the dimensions defined here, by default.
Remove your size attribute

Related

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!!!

How do I create a selector with drawable and color

Here is the code I have that is presently not working. How do I get it to work? I don't want to use two files such as a shape file in addition to the selector file. Can I do this in one file?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img1" android:state_selected="true"/>
<item android:color="#color/my_col"/>
</selector>
I also tried
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img1" android:state_selected="true"/>
<item><shape android:shape="rectangle">
<solid android:color="#color/my_col" />
</shape></item>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#3f68c2" />
<corners android:radius="20dp" />
</shape></item>
<item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#3f68c2" />
<corners android:radius="20dp" />
</shape></item>
It turns out mine works. I was calling the wrong drawable into the layout file.

Apply gradient to rows in listview in android

i've a list view with different colors depending on if it's an even or odd row:
My custom adapter:
if ( position % 2 == 0)
convertView.setBackgroundResource(R.layout.listview_selector_even);
else
convertView.setBackgroundResource(R.layout.listview_selector_odd);
list_selector_even:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#layout/even_row" android:state_enabled="true"/>
<item android:drawable="#layout/even_row" android:state_pressed="true"/>
<item android:drawable="#layout/even_row" android:state_focused="true"/>
</selector>
list_selector_odd:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#layout/odd_row" android:state_enabled="true"/>
<item android:drawable="#layout/odd_row" android:state_pressed="true"/>
<item android:drawable="#layout/odd_row" android:state_focused="true"/>
</selector>
even_row:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>
odd_row:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#F0F0F0"/>
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
</shape>
I'm a little bit lost because i cant get the even and odd rows colors working with the gradient effect. For the preessed effect gradient i've the next code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#3E5260"
android:endColor="#3E5260"
android:angle="270" />
</shape>
</item>
</selector>
Thanks for the help!
You already check for the state in the selector, so for the gradient, you should do this instead:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#3E5260"
android:endColor="#3E5260"
android:angle="270" />
</shape>
Also, either change the selectors to following and define a file for each selector item, or move the selector with all available states to the odd_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#layout/odd_row_pressed" android:state_pressed="true"/>
<item android:drawable="#layout/odd_row_focused" android:state_focused="true"/>
<item android:drawable="#layout/odd_row_enabled"/>
</selector>
This version ensures that odd_row_enabled.xml is chosen if the item is not pressed and not focused. If you want another layout if the item is not enabled, you have to add one more line (before the default line). Also, it ensures that odd_row_pressed.xml is chosen in case it is pressed and enabled, because the <item> for the state pressed comes before the other lines.

How to add a selector to a View's border

I have an edittextview with a black colored border. How can I change this border color depending on the state, using a selector.
I tried this:
<EditText
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/black_rounded_borders"
/>
black_rounded_borders.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />
<stroke
android:width="2dp"
android:color="#drawable/selector_black_border" />
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
selector_black_border.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="#FF269ff5"/>
<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:color="#FF000000"/>
</selector>
EDIT:
I changed selector_black_border.xml to color folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:state_pressed="true" android:color="#FF269ff5"/>
<item android:state_selected="true" android:color="#FF269ff5"/>
<item android:state_focused="false" android:color="#FF000000"/>
<item android:state_pressed="false" android:color="#FF000000"/>
<item android:state_selected="false" android:color="#FF000000"/>
</selector>
<stroke
android:width="2dp"
android:color="#color/selector_black_border" />
But then it always remain in blue color("#FF269ff5") even when not focused/pressed.
Thank You.
you have to put the selector inside color/ and
<stroke
android:width="2dp"
android:color="#color/nameofcolor">

Background xml not working

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>

Categories

Resources