Android buttons with shape background or image - android

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>`

Related

How to create small round buttons that contains an icon at the centre?

I want to create buttons in my android application, just like shown in the image below
you can se all the four buttons below.
I want to give them a custom color background and then an icon on the top. There is no way I'm able to adjust the icon at the centre of button.
How can I achieve the desired effect?
You can do this with FrameLayout and for circle background you need to create a shape drawable file. Refer my answer here.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_shape"
android:clickable="true"
android:focusable="true"
android:visibility="visible"
android:foreground="?android:attr/selectableItemBackground"
android:padding="4dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_star_border_black_24dp"/>
</FrameLayout>
round_shape.xml add this file in drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:angle="270"
android:startColor="#color/profile_grey"
android:endColor="#color/profile_grey"/>
</shape>
Create Drawable and use that drawable as Button Background.
<?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="#e1e1e1"
android:width="0.5dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
</selector>
This Code create a circle with white background.

How to make button like this in Android?

I am trying to implement button in android with following layout.How to make it??
i have already implemented button like this
i used following code to make
<TextView
android:id="#+id/action_text_share"
style="#style/mediumTextSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="center"
android:singleLine="false"
android:text="Share by Text"
android:padding="10dp"
android:layout_weight="1"
android:background="#drawable/white_broder_round_with_transparent_bg"
android:textColor="#color/blackColor" />
and have drawable like this
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/removeContactRedColor"/>
<stroke android:width="2dip" android:color="#color/removeContactRedColor" />
<corners
android:radius="5dip"
/>
</shape>
You can do like this in drawable, <item android:right="-2dp"> will remove stroke from the right end.
<item android:right="-2dp">
<shape>
<corners android:radius="4dp"/>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#color/app_white" />
</shape>
</item>
Some suggestions:
Create a drawable image of the background and use this one as background.
Assuming you created this with xml, create a solid blue shape and place it over the circle.
Design a Background Image and Set it to Button with correct Padding and Margin
Set Text with The Background of Similar Above Image according to your Need

Trying to setBackgroundColor to drawable in Adapter not working as expected

I have a linearlayout background set to a drawable file in order to create that rounded corner look as such:
<LinearLayout
android:id="#+id/layout_top"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/rectangle_topcorner">
<TextView android:layout_width="fill_parent"
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:text="Impact"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:layout_marginLeft="10dip" />
</LinearLayout>
In my adapter, after I inflate the layout item, I am trying to change the backgroundColor to a 'different' drawable, in order to change the background color:
LinearLayout top = (LinearLayout) view.findViewById(R.id.layout_top);
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);
The problem is, after doing that, the rectangle loses its rounded corner look and its just a plain old square.
The 2 drawables:
rectangle_topcorner
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="#+id/background_shape" >
<corners android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<solid android:color="#005577"/>
</shape>
rectangle_topcorner_high
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="#+id/background_shape" >
<corners android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
<solid android:color="#83162D"/>
</shape>
I'm missing something to preserve the rounded corners ?
Try using:
top.setBackgroundResource(R.drawable.rectangle_topcorner_high);
setBackgroundColor are for color resources
Thought I would share my eventual fix -
As I was trying variants of
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);
The correct (working) code is:
top.setBackground(context.getResources().getDrawable(R.drawable.rectangle_topcorner_high));

Mask layout with rounded corner xml shape

I have this LinearLayout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="70dp"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#FFD800"
android:textColor="#android:color/black"
android:gravity="center"
android:text="Text View"/>
</LinearLayout>
And I want to mask it with rounded corner, like this:
I tried to put it in a FrameLayout with another layout with shape.xml on top of it,
But the most I got was:
or
I'm looking for a way to use shape.xml background,
But with transparent inside the border, and white color outside.
My shape.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#FFFFFF">
</solid>
<stroke
android:width="2dp"
android:color="#000000" >
</stroke>
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp">
</padding>
<corners
android:radius="50dp">
</corners>
</shape>
Create a nine-patch with white rounded corners outside, transparent in the middle, ("inverse nine-patch") and put it on top on your LinearLayout. This is a common practice.
Tried out below xml file
<?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="#4a6176" />
<padding
android:left="10dp"
android:right="10dp"
/>
<corners android:radius="6dp" />
</shape>
Just remove <solid> tag from your shape. Then apply that shape to the background of your LinearLayout.
Because the <solid> tag specifies the fill color of your shape.
Without that tag the center of your shape will be fully transparent.
So it will look just like a border.
make your layout background like this: android:background="#drawable/shape"

Setting rounded corners and image to a button

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>

Categories

Resources