I try to put a border for a button in android but unfortunately android:background="" dosen't give any chance to put a drawable resource file. Also it dosen't any chance to put a background color.
This is my button ,
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/space1"
android:text="Submit"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="25dp"
android:layout_centerInParent="true"
android:background="#drawable/button_border" />
IDE(Android Studio) brings the error in the last line of the button xml.
And this is my button_border.xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="5px" android:color="##662a48" />
</shape>
Button should be like below
How to correct this error ?
Have any ideas about this problem .
Thank you.
Because you are providing the wrong value i.e. android:color="##662a48"
try this..
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="5px" android:color="#662a48" />
</shape>
Change your border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="5px" android:color="#662a48" />
</shape>
remove one # # you stroke color
that is, change
#662a48 instead of ##662a48
You don't need to use extra XML for that. You can use the default functionality like,
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeColor="#662a48"
android:text="Submit"
app:strokeWidth="2dp"
app:cornerRadius="10dp"/>
Related
This drawable :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#70628b" />
<stroke android:width="12dp"
android:color="#4c3f6e"
/>
<corners android:radius="8dp"/>
</shape>
This is my textview
<TextView
android:id="#+id/tv_disabled_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/disabled_transparent_frame"
android:fontFamily="#font/inter_medium"
android:gravity="center"
android:padding="#dimen/dp_24"
android:text="02 : 45"
android:textColor="#color/white"
android:textSize="#dimen/dp_32"
android:textStyle="bold"
android:visibility="visible" />
I am trying to apply circular drawable in inside and outside but given code its showing only outer :
Current screen using this code :
Expected screen
I am unable to do circular please help me in this .
try this code
It can show nested corner
<?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="#4c3f6e" />
<corners android:radius="8dp" />
<padding
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#70628b" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
note that your "expected screen" has some small shadow under inner color area, you won't achieve this with common drawable, even with layer-list
I would advice you to use CardView
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/outer_background"
android:padding="12dp">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="4dp"
android:visibility="gone"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="#70628b">
<TextView ...
adjust elevation param for more/less shadow
outer_background.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#4c3f6e" />
<corners android:radius="8dp"/>
</shape>
be pixel-perfect, build beautiful apps!
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>
Can i give the gradient color on stroke(Border) of a button.
Please help me soon.
<item>
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#6053565B" />
<size android:height="40dp"/>
<stroke android:width="2px" android:color="#6053565B" />
</shape>
</item>
Create a drawable shape in your drawable folder like this, and called it button
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#eecc68" />
</shape>
and in your .xml put like below,
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="52dp"
android:layout_marginTop="39dp"
android:background="#drawable/button"
android:text="Button" />
This is a solution for your problem. You can use this if your button background is a fixed color. Im still searching for the same with a transparent button and a gradient border.
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 wonder if I can draw rectangle in XML.
I know how to draw using drawRect method programmatically.
Yes you can and here is one I made earlier:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<stroke android:width="2dp" android:color="#ff207d94" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#ffffffff" />
</shape>
You can create a new XML file inside the drawable folder, and add the above code, then save it as rectangle.xml.
To use it inside a layout you would set the android:background attribute to the new drawable shape. The shape we have defined does not have any dimensions, and therefore will take the dimensions of the View that is defined in the layout.
So putting it all together:
<View
android:id="#+id/myRectangleView"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/rectangle"/>
Finally; you can set this rectangle to be the background of any View, although for ImageViews you would use android:src. This means you could use the rectangle as the background for ListViews, TextViews...etc.
Create rectangle.xml using Shape Drawable Like this put in to your Drawable Folder...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/transparent"/>
<corners android:radius="12px"/>
<stroke android:width="2dip" android:color="#000000"/>
</shape>
put it in to an ImageView
<ImageView
android:id="#+id/rectimage"
android:layout_height="150dp"
android:layout_width="150dp"
android:src="#drawable/rectangle">
</ImageView>
Hope this will help you.
Quick and dirty way:
<View
android:id="#+id/colored_bar"
android:layout_width="48dp"
android:layout_height="3dp"
android:background="#color/bar_red" />
try this
<TableRow
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_height="wrap_content">
<View
android:layout_width="15dp"
android:layout_height="15dp"
android:background="#3fe1fa" />
<TextView
android:textSize="12dp"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1700 Market Street"
android:id="#+id/textView8" />
</TableRow>
output
Use this code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:radius="0.1dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<solid android:color="#Efffff" />
<stroke
android:width="2dp"
android:color="#25aaff" />
</shape>
create resource file in drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#3b5998" />
<cornersandroid:radius="15dp"/>
//Put this code inside the drawable file
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<stroke android:width="2dp" android:color="#C4C4C4" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="15dp" />
<solid android:color="#FFFFFF" />
</shape>