I am using custom drawable to set background of edit text.Following is the code for edit text background
<?xml version="1.0" encoding="utf-8"?>
<corners android:radius="1dp" />
<stroke
android:width="1dp"
android:color="#595C65" />
<solid android:color="#ffffff" />
It displays the edit box with rectangle background .But i want to set edit text background with L shape(set left and bottom backgrounds).
Without using external images and i want to set these background via drawable xmls.
Expected output:
Current output:
I found a solution for these:
text.xml:
<?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="#000000" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<item android:bottom="3px" android:left="3px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:background="#drawable/text"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:ems="10"
android:gravity="center_horizontal"
android:padding="2dp"
android:password="true" />
</RelativeLayout>
Expected Output:
Without using external images we can get a output with the help of drawable text.xml
Using a drawable this should work :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#555555" />
</shape>
Related
I have created a custom circular button using the following :
<Button
android:id ="#+id/StartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/button_bg_round"
android:padding="15dp"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Here button_bg_round is defined as below :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:color="#ffffff" android:width="5dp" />
<solid android:color="#3aeaee"/>
<size android:width="200dp" android:height="200dp"/>
</shape>
</item>
</selector>
Now, I want to add a circular ripple/pulse effect/animation when the button is clicked. I tried to look for a possible solution on google and SO but couldn't find any related to such a button. Any help would be much appreciated.
You can make you drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/colorAccent"
tools:targetApi="lollipop"> <!-- ripple effect color -->
<item android:id="#android:id/background">
<shape android:shape="oval">
<stroke android:color="#ffffff" android:width="5dp" />
<solid android:color="#3aeaee"/>
<size android:width="200dp" android:height="200dp"/>
</shape>
</item>
</ripple>
where android:color inside ripple is the color of the effect.
I make a shape.xml file under res -> drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#ff9900" />
</selector>
And then I use it on an EditText:
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/editText"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:hint="#string/hint"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:singleLine="true"
android:background="#drawable/shape"/>
But the result is that it doesn't change the border color at all. Why, what's wrong?
Why using selector as the root tag? selector is used for applying multiple alternate drawables for different states of the view, so in this case, there is no need for selector.
Try the following code.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background Color -->
<solid android:color="#ffffff" />
<!-- Border Color -->
<stroke android:width="1dp" android:color="#ff9900" />
<!-- Round Corners -->
<corners android:radius="5dp" />
</shape>
Also It's worth mentioning that all color entries support alpha channel as well, meaning that you can have transparent or semi-transparent colors. For example #RRGGBBAA.
Step 1:Create a border.xml in Drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
/>
<solid android:color="#ffffff"
/>
<stroke
android:width="2dip"
android:color="#000" />
</shape>
Step 2: Create a EditText in XML File
<EditText
android:id="#+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="25dp"
android:hint="Enter Email"
android:padding="10dp"
android:layout_marginRight="25dp"
android:background="#drawable/border"
android:inputType="textEmailAddress"
android:singleLine="true" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#ff9900" />
</selector>
You have to remove > this from selector root tag, like below
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
As well as move your code to shape from selector.
selector is used for applying multiple alternate drawables for different status of the view, so in this case, there is no need for selector
instead use shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#ff9900" />
</shape>
i use as following for over come this matter
edittext_style.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="1dp"
android:color="#c8c8c8"/>
<corners android:radius="0dp" />
And applied as bellow
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editTextName"
android:background="#drawable/edit_text_style"/>
try like this..
Use this code on xml . i hope it will be work
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="3dp"
android:color="#4799E8"/>
<corners android:radius="5dp" />
<gradient
android:startColor="#C8C8C8"
android:endColor="#FFFFFF"
android:type="linear"
android:angle="270"/>
</shape>
Check below code may will help you,
Using stroke can make border in edit text and change it's color too as shown below...
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#color/secondary" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
Add this as background in to edit text.
Thanks!
Use root tag as shape instead of selector in your shape.xml file, and it will resolve your problem!
This is work for me:
Drwable->New->Drawable Resource File->create xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#e0e0e0" />
<stroke android:width="2dp" android:color="#a4b0ba" />
</shape>
I want to create the shape of autocomplete textview shown in the figure and text should come in center of it.
Currently I am trying to implement it as
<item>
<shape android:shape="rectangle" >
<corners android:radius="15dip" />
<solid android:color="#color/blue_text" />
<padding
android:bottom="5dip"
android:left="15dip"
android:right="15dip"
android:top="5dip" />
</shape>
</item>
<AutoCompleteTextView
android:id="#+id/autoComp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/bg_autocompletetext"
android:textColor="#color/white"
</AutoCompleteTextView>
But it would just make the corners of the textview round. It does make it completely round. Please specify any solution for it.
try this code of shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="100dp"/>
<solid
android:color="#2137FF"/>
<size
android:width="250dp"
android:height="60dp"/>
</shape>
try this tool
Its better to use 9 patch image as a background of your TextView.
With using 9 patch image you can set proper resolution for different resolution device.And it will give you better result .
You write a seperate xml layout(shape.xml) having code:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#DCDCDC" />
<stroke android:width="2.5dp"
android:color="#DCDCDC" />
<corners
android:radius="10dp"/>
</shape>
Then in your main layout add:
<AutoCompleteTextView
android:id="#+id/autoComp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/shape"
android:textColor="#color/white"
</AutoCompleteTextView>
Hope this help.
Try this, working for rounded text view
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_textview.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#284A89" />
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
</shape>
Add Following file in drawables "searchtextbox_border.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="100dp"/>
<stroke android:color="#color/light_grey"
android:width="1dp">
</stroke>
</shape>
Then Add it as background in your AutocompleteTextview as follows:
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:padding="#dimen/padding_10dp"
android:background="#drawable/searchtextbox_border"
android:hint="Search" />
I have custom designed EditText
search_page.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:background="#E1E1E1"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="City" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/rounded_edittext"
android:layout_weight=".75" />
</LinearLayout>
rounded_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
I want to use color code #2f6699 to get a border color like an outline to the EditText text box as below:
Any ideas on how to achieve this?
Use the below code in your rounded_edittext.xml
<?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="#2f6699" />
<corners
android:radius="10dp"
/>
</shape>
This should work
For EditText in image above, You have to create two xml files in res-->drawable folder.
First will be "bg_edittext_focused.xml" paste the lines of code in it
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke
android:width="2dip"
android:color="#F6F6F6" />
<corners android:radius="2dip" />
<padding
android:bottom="7dip"
android:left="7dip"
android:right="7dip"
android:top="7dip" />
</shape>
Second file will be "bg_edittext_normal.xml" paste the lines of code in it
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#F6F6F6" />
<stroke
android:width="2dip"
android:color="#F6F6F6" />
<corners android:radius="2dip" />
<padding
android:bottom="7dip"
android:left="7dip"
android:right="7dip"
android:top="7dip" />
</shape>
In res-->drawable folder create another xml file with name "bg_edittext.xml" that will call above mentioned code. paste the following lines of code below in bg_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_edittext_focused" android:state_focused="true"/>
<item android:drawable="#drawable/bg_edittext_normal"/>
</selector>
Finally in res-->layout-->example.xml file in your case wherever you created your editText
you'll call bg_edittext.xml as background
<EditText
:::::
:::::
android:background="#drawable/bg_edittext"
:::::
:::::
/>
Use the below code in your rounded_edittext.xml :
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="2dp"
android:color="#2F6699"/>
<corners android:radius="3dp" />
<gradient android:startColor="#C8C8C8"
android:endColor="#FFFFFF"
android:type="linear"
android:angle="270"/>
</shape>
android:background="#E1E1E1"
// background add in layout
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff">
</EditText>
edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="5dp"/>
<stroke android:width="2dip" android:color="#color/button_color_submit" />
</shape>
use here
<EditText
-----
------
android:background="#drawable/edit_text.xml"
/>
Use a 9-patch drawable or create a Shape drawable.
I just created a red circle using android shapes:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="4"
android:shape="ring"
android:thicknessRatio="9"
android:useLevel="false" >
<solid android:color="#FF0000" />
<size
android:height="48dip"
android:width="48dip" />
</shape>
This is really cool, but I cannot set the background color of the circle to my color. I tried android:background="#FFFFFF" but it always appear to be black in my layout. How can I set the background of the above shape?
I think a layer-list might help you:
<?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="#ffffff" />
</shape>
</item>
<item>
<shape
android:innerRadiusRatio="4"
android:shape="ring"
android:thicknessRatio="9"
android:useLevel="false" >
<solid android:color="#FF0000" />
<size
android:height="48dip"
android:width="48dip" />
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="12dp" />
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#android:color/black" />
</shape>
I am just adding helpful research as an answer. Let us suppose you have a shape as the answer described by #GeneBo and you are looking forward to re-using that shape but with a different solid color. So all you need to do in your widget is:
android:background="#drawable/your_shape_to_reuse"
android:backgroundTint="#color/new_background_color_you_need"
Ok, how about this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TextView android:text="Foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:gravity="center"
android:background="#drawable/red_circle"/>
</LinearLayout>
</LinearLayout>