How to make android round buttons - android

I have tried to search this a lot however I was not able to make the same buttons as in the picture for android.
I have been trying to use the following code but the button is not transparent and it doesn't have that nice rounding on the sides:
<?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" />
</shape>
Currently it looks like this
I want the EditTexts and the buttons to be transparent with a white border
It would be a great great help if someone could assist me on this.

Defining shape is proper approach. If you want it to be transparent with white border you need to avoid using solid tag and use stroke (that is the border).
So for example you will have this shape (let's call it bg_button.xml under drawable folder):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#android:color/white" android:width="1dp" />
<corners android:radius="15dp" />
</shape>
Apply then this to your views as background. This is an example:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:background="#drawable/bg_button"/>
Please try this not just on Android Studio editor but also on your emulator / device. Rendering of rounded corners on Android Studio editor can create problems.

Say you have a xml drawable called rounded_button
<?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="#6E6E6E"/>
<corners
android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
You then set the background of the button to the #drawable resource you created.

Related

how to design layout button to be half one color, half other

I need to design button for android app to be rectangle, and to be half one color, half other. In XML of course.
I have tried this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:left="7dp"
android:top="15dp"
android:right="7dp"
android:bottom="15dp" />
<gradient
android:startColor="#a241cb"
android:endColor="#a241cb"
android:type="linear"
android:angle="90"/>
<stroke>
android:width="1dip"
android:color="#a241cb" />
<corners android:radius="0dp" />
</shape>
Any help is appreciated
Using a 9 patch like this (save it in your drawable folder as bw_bg.9.png):
You can get a result like this:
Example layout:
<?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:background="#f00f"
>
<Button
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="#drawable/bw_bg"
/>
</RelativeLayout>
Don't mind the size (it's running on a Froyo 2.8 inch screen).
Also don't mind the notification (it's from another running app).
Try this:
frist create background.xml in drwable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#4A225E"
android:endColor="#4C4064"
android:centerColor="#B4AFC5"/>
</shape>
and then set button backgrond its work for me
you can achieve this by using gradients as background. use this
create a new XML file under res/drawable folder and name it as button_bg.xml.
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#D5DDE0"
android:centerColor="#e7e7e8"
android:endColor="#CFCFCF"
android:angle="270" />
</shape>
now use it for your Button like this:
android:background="#drawable/button_bg"
Hope it helps. :)

Custom View with Layer-List background drawable renders black screen

I'm trying to build a custom android view and apply a background drawable that is a layer-list.
The layer-list has two items:
A background color (white)
A simple shape drawable that is a stroked rectangle with rounded-corners
here's the drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white"/>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1dp" />
<stroke
android:width="2dp"
android:color="#color/background_green" />
</shape>
</item>
</layer-list>
The custom view is a class derving from Android.view.View that currently has NO functionality except the required measuring overloads.
I'm applying the background in the view definition in an activity layout:
<view
android:layout_width="match_parent"
android:layout_height="300dp"
class="com.example.widget.TestView"
android:id="#+id/view2"
android:layout_gravity="center_horizontal"
android:background="#drawable/rect_sig_cap"
android:layout_margin="20dp" />
What I expect to see is a view with a white background and a green border. What I actually see when I deploy the project is a view with a black background and a green border.
Interestingly it appears correctly in the designer preview in Android Studio. It's only when I deploy it to a device that it renders black.
Am I missing something obvious here?
For those interested, I found the solution.
I had defined the shape drawable as including only a stroke definition. Without any other inputs, this causes the fill color to be inferred as black.
In the end, a Layer-List drawable is not required at all. Instead, add a solid fill definition to the shape layer with color Transparent and it works just fine.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="#dimen/corner_radius" />
<stroke android:width="1dp"
android:color="#color/background_green" />
<solid android:color="#android:color/transparent" />
</shape>
Try this one.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/white" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1dp" />
<stroke
android:width="2dp"
android:color="#color/background_green" />
</shape>
</item>
</layer-list>

How to change the shapes of the corners of textview, button, edit text by applying themes dynamically?

This is my code. I have never seen any changes in my application. I have created this in resources/drawable. How should I apply themes in a style dynamically for all components by changing text color, fonts, shapes etc.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="20dp">
<solid android:color="#FFA500"/>
<corners android:bottomRightRadius="50dp" android:bottomLeftRadius="50dp"
android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
<stroke android:color="#FFA500" android:dashWidth="50dp" />
</shape>
Create an shape.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
android:shape="rectangle">
<gradient
android:startColor="#99CCFF"
android:endColor="#99CCFF"
android:angle="45"/>
<padding android:left="6dp"
android:top="6dp"
android:right="6dp"
android:bottom="6dp" />
<corners android:radius="30dp" />
</shape>
Now for your Button code add android:background as shown below.
<Button
android:id="#+id/bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape"/> // shape is name of xml file created
Your code above works absolutely fine. Maybe the problem is with the way you declared the background: are you doing it in XML file or dynamically by Java code?

Android button styling

I am quite new to Android dev, but not Java dev, so doing the logic behind a button is no issue, but styling it is not as easy as css. I have read a couple tutorials on shapes / styles so I kind of know how to do the custom borders and round corners, but I was hoping to see some really good quality examples, like the buttons on the Twitter app http://i.stack.imgur.com/Gip2s.png or the 'debossed' ones on the facebook app.
What I suppose I am really interested in, are using shadows to create effects. Are these done with images, or can you style this in the app?
thanks
for rounded corners create a shape drawable eg. ronded_corner.xml and the angle must be a multiple of 45 degree
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#SomeGradientBeginColor" android:endColor="#SomeGradientEndColor"
android:angle="225"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
then set this Background by android:background:#drawable/ronded_corner
Whenever you create the button in the layout just set the background property of the button as XML, Put this XML file in the drawable folder.
sample XML code i will post here. you can modify and learn in that only.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:startColor="#B80000"
android:endColor="#900405"
android:type="linear"
/>
<stroke android:width="1dp" android:color="#900405"/>
</shape>
For rounded corner button, define inset as shown below and adjust radius.
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="6dp"
android:insetRight="4dp"
android:insetBottom="6dp">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="#0091ea">
<corners android:radius="10dp" />
<solid android:color="#1a237e" />
<padding android:bottom="6dp" />
</shape>
</item>
</ripple>
</inset>
For more information http://www.zoftino.com/android-button

Android create iphone-like Gradient

I need to create black iphone like gradient in my android application. Please view black gradient at the top in the image below. How to do it? Thanks
Something like this, perhaps?
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#FF000000"
android:endColor="#FF8E8E8E"
android:angle="270"/>
</shape>
I've posted a blog about an Iphone look an Android:
http://www.dibbus.com/2011/06/android-gradient-toolbar/
You can find some examples of gradients and what is possible using xml and/or 9-pacth images.
You can create gradients in XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial" android:gradientRadius="400"
android:startColor="#88cce7" android:endColor="#075e81"/>
</shape>
This example is a round gradient but by changing type param you can create others.
Include this code in a xml file in your drawable folder and you can refer to that file when you set a background. ie. android:background="your drawable file"
I know this is an old question, but I was messing around to get this same effect for a customer who wants a copy of his iPad app on Android, this is what I came up with:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:centerColor="#FF333333"
android:centerY="2%"
android:endColor="#FF000000"
android:startColor="#FF8E8E8E" />
<corners android:radius="5dp" />
</shape>

Categories

Resources