I have a button and a selector that must change color, padding and corner radius of the button. I have no problems with changing the color, but I can never see any changes with padding and corner radius.
button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/button_disabled" android:state_enabled="false">
<shape
android:shape="rectangle"
android:padding="8dp">
<corners android:radius="4dp" />
</shape>
</item>
<item android:drawable="#color/button_pressed" android:state_pressed="true">
<shape
android:shape="rectangle"
android:padding="8dp">
<corners android:radius="4dp" />
</shape>
</item>
<item android:drawable="#color/button_focused" android:state_focused="true">
<shape
android:shape="rectangle"
android:padding="8dp">
<corners android:radius="4dp" />
</shape>
</item>
<item android:drawable="#color/colorAccent">
<shape
android:shape="rectangle"
android:padding="8dp">
<corners android:radius="4dp" />
</shape>
</item>
</selector>
code of the button:
<LinearLayout>
....
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/add_to_fav"
android:id="#+id/add"
android:background="#drawable/button_color"
android:textColor="#drawable/button_text_color" />
</LinearLayout>
I know some clumsy way to get the padding - put it into the layout, but 1) it would be "useless parent"; 2) I wouldn't get corner radius.
So what am I doing wrong so I have not shape?
Thanks!
Padding, color and corners should be inside shape
So try this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="4dp" />
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="4dp" />
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item android:state_focused="true">
<shape>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="4dp" />
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
<item>
<shape>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<solid android:color="#color/colorPrimaryDark"/>
<corners android:radius="4dp" />
</shape>
</item>
</selector>
This way all of them will work.
The android:radius value was probly just too small for the effect to be visible. The syntax for the padding on the other hand has to be changed like this:
<item android:drawable="#color/button_focused" android:state_focused="true">
<shape
android:shape="rectangle">
<corners android:radius="20dip" />
<padding
android:left="8dip"
android:top="8dip"
android:right="8dip"
android:bottom="8dip" />
</shape>
</item>
Please note: as stated in the documentation for Shape Drawable,
Padding to apply to the containing View element (this pads the position of the View content, not the shape).
Related
I just wat to create a button as shown in the image. But I am not able to make the round edges at the top corners. How we can do that..please help.
<item>
<shape android:shape="rectangle">
<solid android:color="#A32CB386" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:bottom="5px">
<shape android:shape="rectangle">
<solid android:color="#36885B" />
</shape>
</item>
Your drawable file. Let's say bg_sign_in.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:left="5dp" android:right="5dp" android:top="5dp">
<shape>
<corners android:radius="5dp" />
<solid android:color="#0F4858" />
</shape>
</item>
<item android:bottom="2dp" android:left="0dp" android:right="0dp">
<shape>
<gradient android:angle="270" android:endColor="#0F9D58" android:startColor="#0F9D58" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
</item>
Apply the following in your xml
android:background="#drawable/bg_sign_in"
Output:
You can use something like:
<com.google.android.material.button.MaterialButton
android:background="#drawable/test"
app:backgroundTint="#null"
/>
with:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<layer-list>
<item>
<!-- bottom shadow -->
<shape>
<solid android:color="#2CB386"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item
android:bottom="10px"
>
<shape>
<solid android:color="#36885B"/>
<corners
android:radius="5dp"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_pressed="true">
<layer-list>
<!-- .... -->
</layer-list>
</item>
</selector>
I have defined my button in the xml like this.
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/button_node"
android:textColor="#android:color/white"
android:backgroundTint="#color/colorAccent" />
I need to grey out this button in the code. I have used the below line of code to change it.
button.setBackgroundResource(R.drawable.button_grey);
button.setEnabled(false);
button.setClickable(false)
My button_grey.xml file is
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#color/colorGray"
android:startColor="#color/colorGray" />
<corners android:radius="4dp" />
</shape>
</item>
<item
android:bottom="2dp"
android:left="1.5dp"
android:right="0.5dp"
android:top="0dp">
<shape android:shape="rectangle">
<solid android:color="#color/cardview_shadow_start_color" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
After changing the buton looks like this
But Actually I need the button to be look like below by changing the color to grey.
Please note:- It should work from API level 18 onwards.
Try this it will work
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:right="5dp" android:top="5dp">
<shape>
<corners android:radius="3dp" />
<solid android:color="#D6D6D6" />
</shape>
</item>
<item android:bottom="2dp" android:left="2dp">
<shape>
<gradient android:angle="270"
android:endColor="#24b8eb" android:startColor="#24b8eb" />
<stroke android:width="1dp" android:color="#BABABA" />
<corners android:radius="4dp" />
<padding android:bottom="10dp" android:left="10dp"
android:right="10dp" android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
http://mobilenext.net/4-material-design-tweaks-pre-lollipop-android-devices/
http://www.techrepublic.com/article/android-lollipop-material-design-trick-offers-a-more-polished-ux/
I want create in button with light button border with little 3D shade on edges like this
create an xml in your drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:right="2dp" android:top="2dp">
<shape>
<corners android:radius="0dp" />
<solid android:color="#A09D9D"
/>
</shape>
</item>
<item android:bottom="1dp" android:left="1dp">
<shape>
<gradient android:angle="270"
android:endColor="#color/white"
android:startColor="#color/white" />
<stroke
android:width="0.5dp"
android:color="#CBCBC9" />
<corners
android:radius="0dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
and use this xml as your button background
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#color/white" />
<stroke android:width="1dip" android:color="#color/gray">
</stroke>
</shape>
Set this Xml on background
My question is really simple. That is about Button attrs.
I want to make Button which has only Top stoke. What can I set in layout xml ??
My xml file is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#android:color/transparent"/>
<stroke android:width="1dp" android:color="#color/btn_disable"
android:dashWidth="1dp" android:dashGap="0dp"/>
<padding android:left="9dp" android:top="2dp"
android:right="9dp" android:bottom="2dp"/>
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/white_pressed"/>
<stroke android:width="1dp" android:color="#color/mint"
android:dashWidth="1dp" android:dashGap="0dp"/>
<padding android:left="9dp" android:top="2dp"
android:right="9dp" android:bottom="2dp"/>
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
<item>
<shape>
<solid android:color="#null"/>
<stroke android:width="1dp" android:color="#color/mint"
android:dashWidth="1dp" android:dashGap="0dp" />
<corners android:radius="#dimen/btn_radius" />
</shape>
</item>
</selector>
I cannot determine the exact shape you are looking for, but judging that you only want the top stroke, hide all other strokes, you can use a layer-list to achieve this.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<solid android:color="your_stroke_colour" />
<corners
android:radius="your_radius" />
</shape>
</item>
<item
android:top="1dp"
android:bottom="0dp"
android:left="0dp"
android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="your_color" />
<corners
android:radius="your_radius" />
</shape>
</item>
</layer-list>
Here is my coding. Here is design blue color for TextView's 4 border. What I want is to design only for 3 borders (top, left and bottom).
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#449def" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="0dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#ffffff"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="0dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
</selector>
Below code placed at the bottom of height of 1dp, you have to play with this for other sides
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
<shape>
<solid android:color="#535353" />
</shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
<shape>
<solid android:color="#252525" />
</shape>
</item>
</layer-list>
Refer this discussion Open-sided Android stroke?
This is a quick solution (probably dumb), add android:translationX="2dp" on your TextView and you can add android:paddingRight="2dp" to make up for lost space.
Try like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:bottom="1dp" android:left="1dp" android:right="0dp"
android:top="1dp"/>
<solid android:color="#000"/>
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>