I am trying to make button'corners round, as well as setting a background image. Though I was able to achieve button with rounded corners, but not to set the image as the background. When I try android:drawableTop:#drawable/filename, the button is again converted to its default shape and text goes below the image, but I want the image as the background, so how to apply both the things(rounded corner and background image) to the button?
I have created an xml file named button_shape in drawable; the code for it is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#ffffff"
/>
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
<stroke android:width="3dp" android:color="#000000" />
</shape>
and the main xml file code where I have put the button is:
<Button
android:id="#+id/button1"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginTop="179dp"
android:background="#drawable/button_shape"
android:text="Button"
android:textColor="#000000"
/>
Please help me with this.
use this in shape
solid android:color="#android:color/transparent" />
and imageview should be
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scaleType="fitXY" android:background="#drawable/shape"
android:src="#drawable/yourimage" />
Use this background.xml put in Drowable and set on Button android:background="#drawable/ background" and ovel_shape is xml file in drawable where you can put code for rounded corners
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/rounded_corn"
android:drawable="#drawable/ovel_shape"/>
<item
android:id="#+id/background_img"
android:drawable="#drawable/image"/>
</layer-list>
Related
I have a text view and and I want its background to be a image and also its corners rounded. I have tried that by creating a drawable file but the background image gets spilled out.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/colorPrimaryDark"
android:background="#drawable/drawable_file"
/>
</LinearLayout>
drawable_file.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/img" />
</item>
<item>
<shape>
<stroke
android:width="1dp" />
<corners android:radius="50dp" />
</shape>
</item>
</layer-list>
Output:
Example Output:
As in official Android documentation for LayerList
A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Meaning your drawable needs to be placed after the rounded background to be visible.
I am using android studio, and I try to do some work on UI using some buttons.
I have created a background for some buttons, but when I apply this background on the buttons, the text inside the butons is not centered.
I think the marginof the button remains the same, whereas the applied background creates a smaller square than the marginn of the button. I don't know how to make the margin of the button fit the margin of the square coded in the background xml file.
Here the picture of the problem :
pic1
Here is the code of the background (bg_rectangle_grey.xml) :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="#dimen/_34sdp" android:height="#dimen/_34sdp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff6c7780" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
And here is the code of the xml file with the button :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/_15sdp"
android:style="?android:attr/buttonBarStyle">
<Button
android:style="?android:attr/buttonBarButtonStyle"
android:background="#drawable/bg_rectangle_grey"
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="M" />
I have looked for answers on google but it was never exactly what I am looking for.
If anyone can help.
Thank you in advance
Just use gravity center in button
<Button
android:style="?android:attr/buttonBarButtonStyle"
android:background="#drawable/bg_rectangle_grey"
android:id="#+id/button4"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="M" />
Please remove android:width="#dimen/_34sdp" android:height="#dimen/_34sdp" from below code and try again.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="#dimen/_34sdp" android:height="#dimen/_34sdp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ff6c7780" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
I want to create a button in an Android application which will be round with a color of my choosing and a plus sign as its text.
Is it better (in terms of space, efficiency etc.) to create an image of the above description and set it as a background image, or it it better to make a shape with a color and add that as a background?
To do that you can create a circle shape with the help of shape drawable, but the better way to do this is to use the FloatingActionButton which is circular in shape and you can provide the icon of your choice.
-First of You create drawable xml
-ic_round_shape_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#000" /><!--color code which you are use instead #000-->
<corners android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topRightRadius="8dp"
android:topLeftRadius="8dp"/>
</shape>
//Create xml and use background like below
activity_main.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_round_shape_background"
/>
please try the below code.
create a background.xml in drawable folder and paste this code.
`
<solid android:color="#color/colorPrimary"></solid>
<stroke
android:width="2dp"
android:color="#color/colorPrimary"></stroke>
<padding
android:left="5dp"
android:right="5dp"
android:top="5dp"></padding>
<corners android:radius="5dp"></corners>
`
in Layout
<LinearLayout
android:id="#+id/place_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:gravity="center"
android:orientation="horizontal">
` <ImageView
android:layout_gravity="center_vertical"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/image" />
</LinearLayout>`
How to show a textview with rounded corner rectangle as shown in the orginal image
in the above (original) picture, the button 2's left and right rounded corner are correctly shaped but in my code the left and right rounded corners are not shaped correctly
in the second picture I need to do more rounded as the 1st image. how can I do with following drawable?
drawable code (green_bg.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#19D8C8" />
<corners android:radius="3dip" />
<stroke
android:width="10dp"
android:color="#19D8C8" />
</shape>
activity_main.xml
.......
<TextView
android:id="#+id/qmap_2"
android:layout_width="35dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="2"
android:textStyle="bold"
android:textColor="#color/no_color" />
......
create a file round.xml 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="#176d7a" />
<corners android:radius="50dp" />
</shape>
now set the background of textview like
<TextView
android:id="#+id/qmap_2"
android:layout_width="35dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="2"
android:textStyle="bold"
android:background="#drawable/round"
android:textColor="#color/no_color" />
it should work
Change the corner radius to a much higher value i.e 100dp
<corners android:radius="100dip" />
I am trying to get a TextView with white background and rounded corners and text in the middle.
Something that looks like this:
So far I have this but it is not giving me the above effect.
<TextView
android:textColor="#000000"
android:background="#FFFFFF"
android:text="0" />
First of all, I would create a custom drawable resource for easy implementation of rounded corners.
(place this in res/drawable)
my_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="ffffff" />
<corners
android:radius="15dp"/>
<stroke
android:width="1dp"
android:color="#android:color/black" />
</shape>
More info on xml drawable resources can be found right here if you want to get into more advanced drawables (gradients, layer-list, animation, etc...)
Then change your TextView in xml layout file to match this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#000000"
<!--refer to your custom drawable from earlier-->
android:background="#drawable/my_bg"
<!--center text within TextView-->
android:gravity="center"
android:text="0" />
I hope this helps, Happy Coding!
Set the gravity
android:gravity="center"
http://developer.android.com/reference/android/widget/TextView.html#attr_android:gravity
You are missing the width and height attribute for TextView.
For the rounded corners use a Shape Drawable
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Under res/drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#FFFFFF"/>
<corners android:radius="7dp" />
<stroke android:width="5px" />
</shape>
Then
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#drawable/background"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
Snap