I am trying to build a custom edit text. But the problem is the icon didn't locate rightly. I want these two to locate in the center. Where am i wrong?
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="6dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
<item>
<shape>
<solid android:color="#AAAAAA" />
<corners android:radius="6dp" />
</shape>
</item>
<item
android:left="34dp"
>
<!-- left is icon size + 2x side padding around icon-->
<!-- 18 + 8 + 8 -->
<shape>
<solid android:color="#android:color/white" />
<corners android:radius="6dp"
android:topRightRadius="6dp"
android:topLeftRadius="0dp"
android:bottomRightRadius="6dp"
android:bottomLeftRadius="0dp"/>
</shape>
</item>
<item>
<shape>
<corners android:radius="6dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#BBBBBB" />
</shape>
</item>
</layer-list>
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#71B9DE">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/test"
android:drawableLeft="#drawable/bca"
android:padding="5dp" />
</LinearLayout>
#drawable/test
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="6dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
<item>
<shape>
<solid android:color="#AAAAAA" />
<corners android:radius="6dp" />
</shape>
</item>
<item android:left="34dp">
<shape>
<solid android:color="#android:color/white" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="6dp"
android:radius="6dp"
android:topLeftRadius="0dp"
android:topRightRadius="6dp" />
</shape>
</item>
<item>
<shape>
<corners android:radius="6dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#BBBBBB" />
</shape>
</item>
</layer-list>
OUTPUT
Related
Now i have next rectangle:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="28dp" />
<gradient
android:endColor="#FFFFFF"
android:gradientRadius="20dp"
android:startColor="#FFFFFF"
android:type="linear" />
<size
android:width="106dp"
android:height="106dp" />
<stroke
android:width="1dp"
android:color="#0189ff" />
</shape>
And have next result:
Q: How can I add a shadow/elevation instead of stroke?
you can not directly apply shadow in place of stroke.
For that, you need to customized your XML using <layer-list></layer-list>
Like
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding
android:bottom="#dimen/_3sdp"
android:left="#dimen/_1sdp"
android:right="#dimen/_3sdp"
android:top="#dimen/_1sdp" />
<solid android:color="#00dff2e4" />
<corners android:radius="#dimen/_5sdp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="#dimen/_3sdp"
android:left="#dimen/_1sdp"
android:right="#dimen/_3sdp"
android:top="#dimen/_1sdp" />
<solid android:color="#10dff2e4" />
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="#dimen/_3sdp"
android:left="#dimen/_1sdp"
android:right="#dimen/_3sdp"
android:top="#dimen/_1sdp" />
<solid android:color="#20dff2e4" />
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="#dimen/_3sdp"
android:left="#dimen/_1sdp"
android:right="#dimen/_3sdp"
android:top="#dimen/_1sdp" />
<solid android:color="#30dff2e4" />
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="#dimen/_3sdp"
android:left="#dimen/_1sdp"
android:right="#dimen/_3sdp"
android:top="#dimen/_1sdp" />
<solid android:color="#50dff2e4" />
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#android:color/white" />
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
</layer-list>
output
how to make this background xml for a button.
Use that xml fileinside Drawable folder and assign it to TextView as android:background="#drawable/xmlFile"
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="100dip"/>
<solid
android:color="#FFF" />
<stroke
android:width="2dip"
android:color="#F00" />
<padding
android:left="6dip"
android:right="6dip"
android:top="5dip"
android:bottom="5dip" />
</shape>
Create your button background on as a drawable resource like this.
button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#f5f5f5" />
<corners android:radius="8dp" />
<stroke android:width="4dp" android:color="#FF0000" />
<padding android:bottom="16dp" android:left="16dp" android:right="16dp" android:top="16dp" />
</shape>
</item>
<item android:state_enabled="true" android:state_focused="false">
<shape>
<solid android:color="#ffffff" />
<corners android:radius="8dp" />
<stroke android:width="4dp" android:color="#FF0000" />
<padding android:bottom="16dp" android:left="16dp" android:right="16dp" android:top="16dp" />
</shape>
</item>
</selector>
Then simply use it on your button
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:textColor="#FF0000"
android:background="#drawable/button_background" />
Try this
http://tips.androidhive.info/2013/09/android-layout-rounded-corner-border/
and
http://android--code.blogspot.in/2015/01/android-rounded-corners-button.html
Add a xml file in drawable folder and add this code
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="#a9c5ac" >
</solid>
<!-- view border color and width -->
<stroke
android:width="3dp"
android:color="#1c1b20" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
<!-- Here is the corner radius -->
<corners
android:radius="10dp" >
</corners>
</shape>
and for button change background to this xml as
android:background="#drawable/yourxml"
I am trying to achieve the above background with the following code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#dddddd" />
<size android:width="0dp" android:height="0dp" />
<stroke
android:width="3dp"
android:color="#aaaaaa" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#6bb726" />
<size android:width="3dp" android:height="0dp" />
</shape>
</item>
</layer-list>
However, setting rectangle widths does not seem to be working and the green rectangle is taking as much space as the grey rectangle.
Can anyone suggest improvements to my xml-defined shape?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="15dp">
<shape android:shape="rectangle" >
<solid android:color="#6bb726" />
<size android:width="3dp" android:height="0dp" />
</shape>
</item>
<item android:left="5dp" >
<shape android:shape="rectangle">
<solid android:color="#dddddd" />
<stroke
android:width="3dp"
android:color="#aaaaaa" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
</layer-list>
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="15dp">
<shape android:shape="rectangle" >
<solid android:color="#6bb726" />
<size android:width="3dp" android:height="0dp" />
</shape>
</item>
<item android:left="5dp">
<shape android:shape="rectangle" >
<solid android:color="#aaaaaa" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
<item android:left="5dp" android:top="3dp" android:bottom="3dp" android:right="3dp">
<shape android:shape="rectangle">
<solid android:color="#dddddd" />
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="0.1dp"
android:topRightRadius="15dp" />
</shape>
</item>
</layer-list>
This is what I came up with.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/grey" />
<padding
android:bottom="1dp" />
<corners
android:radius="0dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/white" />
<corners
android:radius="1dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</layer-list>
This is working however the bottom radius is showing up whatever values I place on it.
Actually in only takes the topLeftRadius to make it looks like this
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="5dp"
android:topRightRadius="0dp"/>
I had same problem, try it. it worked for me fine. I just add android:top="10dp" to second item. So, it causes to round just top corners.
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid
android:color="#color/dialog_title_bar_blue"/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
</item>
<item
android:top="10dp">
<shape android:shape="rectangle">
<solid
android:color="#color/dialog_title_bar_blue"/>
</shape>
</item>
</layer-list>
Hi here is your solution "Cheers :) "
first add this layout as background.
Then in the below add a view as aline.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<!-- you can use any color you want I used here gray color -->
<solid android:color="#ffffff" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
And the view
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/black"/>
Issue:
there is a bug as mentioned here
Solution:
the bug has been solved in api12.
so give it a try by providing appropriate resources.create two xml files with same name and put one of them into drawable folder and another into drawable-v12 folder.
-> In drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/grey" />
<padding
android:bottom="1dp" />
<corners
android:radius="0dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/white" />
<corners
android:radius="1dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</layer-list>
-> In drawable-v12 folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/grey" />
<padding
android:bottom="1dp" />
<corners
android:radius="0dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/white" />
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</layer-list>
I hope it will be helpful !
<item>
<shape>
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<solid android:color="#800000" />
<stroke
android:width="3dp"
android:color="#android:color/black" />
</shape>
</item>
<item android:bottom="3dp">
<shape>
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<solid android:color="#800000" />
</shape>
</item>
How to remove bottom line/stroke from view ?
I am using to set background xml file like
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/inactive_tab" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
<stroke
android:width="2dp"
android:color="#android:color/black" />
<padding
android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="0dp" />
</shape>
but it has bottom line.
Use layer-list to layer other rectangle on top of the bottom rectangle.
Try the following code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#FF000000" />
<solid android:color="#000000" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
</item>
<item
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="-1dp">
<shape android:shape="rectangle" >
<stroke
android:width="0dp"
android:color="#FFDDDDDD" />
<solid android:color="#FFDDDDDD" />
</shape>
</item>
</layer-list>
Related link:
add_border