I'm working on an Android project where the whole layout is created dynamically. Now I need to change the buttons to have rounded edges. So I can't do this in the xml but must do it in the code. Anyone knows how to do this?
Use
button.setBackgroundResource(R.drawable.custom_rouded_button_background);
custom_rouded_button_background.xml is defined in your drawable folder.
You need a xml based drawable resource like this:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1dp"
android:color="#000000" />
<corners android:radius="5dp" />
<solid android:color="#FFFFFF" />
and change background of button. Make it to background.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
<solid
android:color="#0C094F"/>
<size
android:width="dp"
android:height="62dp"/>
<stroke
android:width="3dp"
android:color="#878787"/>
</shape>
Related
How can we display the colors as shown in the image where we can select more than one color ? The colored boxes must be checkboxes according to me but I am not able customize them
You need to define an xml resource in the drawable folder and use it in an imageView for instance for your question above it may look like this. Where by you use a shape called rectangle, giving it a solid colour as below and then also making the corner radius circular with 8dp. Then to display it you use an imageView and set it as a src. I have also added a stroke. Remove it if you dont need it!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#DFDFE0" />
<size
android:height="8dp"
android:width="16dp" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
<stroke
android:width="3dp"
android:color="#2E3135" />
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp"/>
<gradient
android:startColor="#2E3135"
android:centerColor="#000000"
android:endColor="#ffffff"
android:angle="180"/>
</shape>
Change the colour as your need.
I want create one shape such as below image, but I want create this with XML codes (in drawable) and I do not want create this with 9.patch images!
How can I create this shape with xml code?
Here is a work- around
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
Tweak the corner radius to get your upper part curved!
or overlap this oval shape to get the desired output
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--circle filling color-->
<solid android:color="#ffffff" />
<stroke
<!--radious can define in here-->
android:width="1dp"
android:color="#ffffff" />
<corners
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>
But basically, this is not the correct way to do this! Use another way around, use 9 patch
I have EditText. How can I set its border width and colour only and in xml? I can't figure it out and all I've found so far didn't explain that in a simple way. But I believe that so such a simple thing there must be a simple way to do it.
Create a drawable xml for your EditText with named EditTextStyle.xml in your Drawable Folder like below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
Change your color and width according to your requirement and add this as the background of your EditText of your Layout xml
android:background="#drawable/EditTextStyle"
You can create Customize editText xml in Drawable folder like
custom_edttext.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#641E1E" />
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
</shape>
and use custom_edittext.xml as background of editText
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_edittext"/>
I don't have enough karma to say this as a comment. So typing as answer. See this link. Just create an xml file in drawable(here rounded_edittext.xml in the example) and set background for the EditText as
android:background="#drawable/rounded_edittext"
The <stroke> in the rounded_edittext.xml is used to set the thickness and color of the border of the EditText.
Here's the rounded_edittext.xml as shown in the link.
<?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:topLeftRadius="10dp"
android:topRightRadius="10dp" android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
I hope this answer match with your needs.
<?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="#76b385" />
<corners android:radius="12dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="1dp"
android:right="1dp">
<shape android:shape="rectangle" >
<gradient
android:angle="-90"
android:centerColor="#ffffff"
android:centerX="50%"
android:endColor="#ffffff"
android:startColor="#ededed"
android:type="linear" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
Modify this xml with your needs.
put this file in your drawable folder
edit_text_back.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#641E1E" />
<corners
android:radius="5dp" />
</shape>
and add use this drawable as background to your edittext as
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/edit_text_back"/>
If you need any assistance.Please let me know and mark this answer up.if it helps.Happy coding :)
How we can do this like the below image on EditText and Button in material design on Android ?
Cheers!
Create a xml with the below code in drawable and set it as your edittext bg..
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#00FFFFFF"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
<stroke android:width="1dp" android:color="#FFFFFFFF" />
</shape>
You may find what you want here:
http://developer.android.com/guide/topics/ui/controls/button.html#Style
Use this xml for the EditText, remove transparent and add color for Button:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="30dp" />
<stroke android:width="5px" android:color="#android:color/white" />
</shape>
I am trying to make a drawable to use for a button. I would like it to have this coloring, with a 2px border around it.
Everything works just fine except I cannot get the border to show up...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient android:startColor="#color/bar_clicked_dark"
android:endColor="#color/bar_clicked_light"
android:angle="90"/>
<corners android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topRightRadius="0dp" />
<stroke android:width="2dp"
color="#ff00ffff" />
</shape>
It looks like you forgot the prefix on the color attribute. Try
<stroke android:width="2dp" android:color="#ff00ffff"/>
If you want make a border in a shape xml. You need to use:
For the external border,you need to use:
<stroke/>
For the internal background,you need to use:
<solid/>
If you want to set corners,you need to use:
<corners/>
If you want a padding betwen border and the internal elements,you need to use:
<padding/>
Here is a shape xml example using the above items. It works for me
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#D0CFCC" />
<solid android:color="#F8F7F5" />
<corners android:radius="10dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
</shape>
We can add drawable .xml like below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/color_C4CDD5"/>
<corners android:radius="8dp"/>
<solid
android:color="#color/color_white"/>
</shape>