Android Layout: Creating a circular 3D button? - android

In my current android layout I have 5 buttons that are transparent.
I wish to change their shapes to fully circular and add an a 3d effect to them like the shading in the accepted answer for this question
How can I do so?
Current look:
Android xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/menubackground" >
<Button
android:id="#+id/btnSearchHomeDate"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Date"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeGame"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Game"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeMedValues"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Meditation Values"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeAttValues"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Attention Values"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeScore"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Score"
android:textSize="30sp" />
</LinearLayout>
Circle2 drawable:
<?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="#0000" />
<stroke
android:width="1dp"
android:color="#android:color/black" />
<padding
android:left="0.9dp"
android:right="0.9dp"
android:top="0.9dp"
android:bottom="0.9dp"/>
<corners android:radius="8dp" />
</shape>
</item>
<item>
<shape
android:shape="rectangle" >
<solid android:color="#0000" />
<stroke
android:width="1dp"
android:color="#4a6176" />
<padding
android:left="10dp"
android:right="10dp"/>
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>

ok first make a btn_blue.xml file in your drawable folder
put this code on it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#449def" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#449def"
android:endColor="#2f6699"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
now on button side
set
android:background="#drawable/btn_blue"
it will give you that view
i hope it helps you

Related

How to make drawable circle shape with count

I really appreciate if someone can help me with using how to drawable circle view with count as like my below image.
I wrote some code for my requirement but it's showing rectangular shape but i want circular shape..
linear_layout_border(drawable file)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#color/colorPrimary" />
<solid android:color="#color/colorLightSky" />
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
xml:
<FrameLayout
android:id="#+id/group_attachment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="3dp"
android:background="#drawable/linear_layout_border">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:src="#drawable/ic_group"
android:tint="#color/rosecolor" />
</RelativeLayout>
<TextView
android:id="#+id/members_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginBottom="3dp"
android:background="#drawable/test_circle"
android:gravity="center"
android:minWidth="15dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:text="0"
android:textColor="#fff"
android:textStyle="bold"
android:visibility="visible" />
</FrameLayout>
image:-
Try this it looks as you want
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners
android:radius="10dip"/>
<solid
android:color="#2196F3" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:left="2dip"
android:right="2dip"
android:top="2dip"
android:bottom="2dip" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
Use this code to draw a circle and put it as background of your TextView and make its android:gravity="center"
Change height and width according to your need.
Try following drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item xmlns:android="http://schemas.android.com/apk/res/android">
<shape>
<solid android:color="#58050505" />
<corners
android:topLeftRadius="3dp"
android:topRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
/>
</shape>
</item>
</selector>
Use this for the TextView background
inside drawable folder
badge_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#color/purple"
android:startColor="#color/purple"></gradient>
<corners android:radius="100dp"></corners>
<stroke
android:width="0.5dp"
android:color="#color/white" />
</shape>
The TextView
<TextView
android:id="#+id/serial_no"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="top|start"
android:layout_marginTop="5dp"
android:background="#drawable/badge_background"
android:gravity="center|center_vertical"
android:maxLines="1"
android:text="1"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="bold" />

How to create square button in android for background?

img
I want to create square shape in background. Android studio shape doesn,t support square.How to create manually in border.xml?
Here is my code
border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#9294a3" />
<stroke
android:width="2dp"
android:color="#c2bbbb" />
<corners android:radius="2dp" />
</shape>
my.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#9294a3">
<TextView
android:id="#+id/tv_2"
android:layout_width="wrap_content"
android:text="2"
android:textColor="#c2bbbb"
android:textSize="30sp"
android:background="#drawable/border"
android:layout_height="wrap_content" />
</LinearLayout
Button like this
xml
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/border"
android:text="2"
android:textSize="50sp"
android:gravity="center"
android:textColor="#ffffff"
android:padding="5dp"/>
Style (drawable/border.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorGray"/>
<stroke android:color="#color/colorBlack"
android:width="4dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="15dp" />
</shape>
</item>
</selector>
<TextView
android:id="#+id/input_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="right"
android:direction="center"
android:gravity="center"/>
you should specify shape='Rectangle' for square it will be better.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#9294a3" />
<stroke
android:width="2dp"
android:color="#c2bbbb" />
<corners android:radius="2dp" />

Android: Adding shadow to all sides of button?

I have a simple Android menu in which I want to add extra shading/shadows to all sides of the buttons in order to make them more pronounced on screen. How can I do so?
Screenshot (Ignore coloured buttons in middle, they will all be transparent):
XML- layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/menubackground" >
<Button
android:id="#+id/btnSearchHomeDate"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/circle4"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Date"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeGame"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="#drawable/circle"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Game"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeMedValues"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="#drawable/circle3"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Meditation Values"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeAttValues"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="#drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Attention Values"
android:textSize="30sp" />
<Button
android:id="#+id/btnSearchHomeScore"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:background="#drawable/circle4"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Score"
android:textSize="30sp" />
</LinearLayout>
**XML for transparent button (Circle 4): **
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#0000" />
<stroke
android:width="1dp"
android:color="#4a6176" />
<padding
android:left="10dp"
android:right="10dp"
/>
<corners android:radius="8dp" />
</shape>
1) First we need to create a transparent colour so we will create a new file in values folder called colors.xml:
<resources>
<color name="yellow_transparant">#33c9ce6a</color>
</resources>
As you see the '33' before the colour number specify the opacity of this colour. in our case it is 20%. For full list of them check this Answer: How to make a background transparent in android. I use it as a reference.
2) Assign this colour to your 2nd layer of your shape:
Try this one and see if this what you are looking for:
<?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="#0000" />
<stroke
android:width="1dp"
android:color="#android:color/holo_green_light" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"/>
<corners android:radius="8dp" />
</shape>
</item>
<item>
<shape
android:shape="rectangle" >
<solid android:color="#color/yellow_transparant" />
<stroke
android:width="1dp"
android:color="#4a6176" />
<padding
android:left="10dp"
android:right="10dp"/>
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
3) Add this to your button to create a shadow for your text:
android:shadowColor="#android:color/holo_green_light"
android:shadowDx="1" <!-- put a negative value to change direction --!>
android:shadowDy="1"
android:shadowRadius="0.1"
I use a gradient for buttons to give them a sort of 3D look:
In my drawable folder (file name button_gradient.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/buttonGradientTop"
android:endColor="#color/buttonGradientBottom"
android:angle="90"/>
<corners android:radius="10dp" />
</shape>
and calling it in a layout:
android:background="#drawable/button_gradient"

Android : customized button not showing in avd

Hello I tried to customize a button in my app.So in res/drawable-xdpi in a new xml file i put the following code:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#ef4444"
android:endColor="#992f2f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
The thing is that in graphical layout the button is customized as it is supposed to be,but when i run the app on AVD the customizations are not displayed and it just shows the default buttons.Am I missing something here?
ALSO:
full xml layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#151515" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_above="#+id/button3"
android:layout_centerHorizontal="true"
android:text="#string/gram"
android:background="#drawable/blue_button"
/>
<Button
android:id="#+id/button1"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_marginBottom="50dp"
android:text="#string/schedule"
android:background="#drawable/blue_button" />
<Button
android:id="#+id/button3"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/linearLayout1"
android:layout_marginTop="54dp"
android:text="#string/announcement"
android:background="#drawable/blue_button" />
</RelativeLayout>
Yeah, I think you are, are your sure your AVD is "HDPI" ? If not, that's your problem !
The red_button.xml file should go in res/drawable folder. If it doesn't exist in your project's hierarchy, add a new folder by right-clicking on res directory and name it drawable.

Adding icon next to button text in android

I used this button generator. http://angrytools.com/android/button/
Is there an easy way to add an icon to the left of the text.
The drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="10dp"
/>
<gradient
android:angle="90"
android:startColor="#cacaca"
android:endColor="#FBFBFB"
android:type="linear"
/>
<padding
android:left="50dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="600dp"
android:height="60dp"
/>
<stroke
android:width="2dp"
android:color="#9f9f9f"
/>
</shape>
The button
<Button
android:id="#+id/angry_btn"
android:text="button"
android:textColor="#000000"
android:textSize="18sp"
android:layout_width="600dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A4A4A4"
android:shadowDx="2"
android:shadowDy="3"
android:shadowRadius="5"
/>
Use android:drawableLeft attribute in the Button XML.
use 'android:drawableLeft="#drawable/userImage"' or 'android:drawableRight="#drawable/user_image"' in Button xml attribute
Try this wount really fail
<Button
android:id="#+id/fab"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/btn_registration_login"
android:text="LOGIN"
android:paddingLeft="125dp"
android:paddingRight="125dp"
android:textColor="#color/white"
android:drawableRight="#drawable/ic_navigate_next_white_18dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>

Categories

Resources