Related
I am trying to create a capsule/pill shaped button in XML. I have defined a drawable and set it as the background of my button, but in the preview, and when I run the app, it's displaying as a blue rectangle, despite the background drawable being a white oval. Does anyone know why that might be happening?
Here's the button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search_box"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/button_capsule"
android:text="#string/search"
android:textColor="#color/precipLightBlue"/>
And here's the background drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp" />
<solid android:color="#android:color/white" />
</shape>
To have a capsule/pill shaped button you can use the MaterialButton in the official Material Component library using the app:cornerRadius attribute.
<com.google.android.material.button.MaterialButton
android:layout_width="100dp"
app:cornerRadius="32dp"
..../>
With the version 1.1.0 you can also customize the shape of your component using the app:shapeAppearanceOverlay attribute
<com.google.android.material.button.MaterialButton
....
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.ButtonRounded"
.../>
In the style define:
<style name="ShapeAppearanceOverlay.ButtonRounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">32dp</item>
</style>
You can also try to use the ExtendedFloatingActionButton:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/exfab"
android:layout_width="wrap_content"
.... />
You put a shape that you didn't defined. To define an oval you should put the shape="oval" option in the tag. Although i think you want a rectangle with rounded corners as i see in your code.
Also 1000dp radius is a lot, maybe that's making an issue.
Define size too. Because that shape doesn't have any size and may not appear as you are using wrap_content in the button definition
Try this:
<corners android:radius="30dp" />
<solid android:color="#android:color/white" />
<size
android:width="200dp"
android:height="50dp"/>
If you want an oval remove the corners tag and add android:shape="oval" as property of shape tag
Here is the code regarding button drawable background.
Xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|top"
android:background="#color/colorLightPurple">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/button_bg"
android:text="Search"
android:textColor="#color/colorBlue"/>
</LinearLayout>
For button background , please add same drawable file
seems like you're doing everything right. Just a small addition, try adding android:shape="rectangle" in the shape tag.
This is what button_capsule.xml should look like.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#android:color/white" />
</shape>
Hope this works. All the best.
In Material design version 1.2.0 they have fixed this issue and added a property for setting background for Material Button
here is the dependency for the latest version
com.google.android.material:material:1.2.0
and also if u want to remove the space between the drawable top and bottom so that the drawable get the whole width and height use these 2 properties
android:insetTop="0dp"
android:insetBottom="0dp"
if want to explore more about the properties u can refer to the release of library and can check all the properties.
https://github.com/material-components/material-components-android/releases/tag/1.2.0
use this.
<androidx.appcompat.widget.AppCompatButton
.....
/>
I can't center my drawableLeft icon.
I can easily put icon on the left of text, but if I set gravity to center, then only text is centered, but no icon.
<Button
android:id="#+id/patient_list_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/patientList"
android:drawableLeft="#drawable/ic_icon_two_heads"
android:layout_marginBottom="15dp"
style="#style/darkBlueButtonWithImage"/>
This is what I want:
This is what I have:
<style name="darkBlueButtonWithImage">
<item name="android:drawablePadding">10dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:background">#drawable/radius_dark_blue_button</item>
<item name="android:gravity">center_vertical|left</item>
<item name="android:drawableTint">#color/white</item> <!-- has to be set in activity.java -->
</style>
How can I achieve this?
Finally, after all these years, we have possibility to achieve such behavior in simple and intuitive way.
All you have to do is use MaterialButton from Google Material library. Then use style with icon. After that you can use app:iconGravity property and set it to textStart
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some text"
app:iconGravity="textStart"
app:icon="#drawable/some_icon" />
The easiest solution IMO:
yourButtonView.text = SpannableString(" ${getString(R.string.your_string)}").apply {
setSpan(
ImageSpan(requireContext(), R.drawable.your_icon),
0,
1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
Is a bit hacky? Maybe. Does it work with every button (MaterialButton, AppCompatButton, Button, etc)? It does.
I prefer this rather than going with all the hassle of creating a custom view or something like that.
After giving Google's approach a try over 9000 times, I almost always ended up using a ViewGroup to put both side by side, particularly a RelativeLayout or LinearLayout and then adding an ImageView and a TextView. It's lame, but drawableStart/End have so many missing features that you'll waste a lot of time before you realize "you can't do it".
Alignments, tinting, Vectors, etc. All those things are harder or impossible with the "built in" drawable.
Using Button attributes android:drawableLeft and android:drawablePadding you won't be able to get your expected result. You can create a custom button using RelativeLayout or LinearLayout, TextView and ImageView. Use <selector> to define your button state(normal/pressed) behavior.
Here is an working example. Try this:
<!--- Custom Button -->
<RelativeLayout
android:id="#+id/patient_list_button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:clickable="true"
android:background="#drawable/custom_button_selector">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:gravity="center_vertical">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/icon_refresh"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textSize="16dp"
android:text="CUSTOM BUTTON"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</LinearLayout>
</RelativeLayout>
custom_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/bg_custom_button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/bg_custom_button_pressed" />
<item
android:state_enabled="true"
android:drawable="#drawable/bg_custom_button_normal" />
</selector>
bg_custom_button_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#01A1DD" >
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
bg_custom_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid
android:color="#303F9F">
</solid>
<!-- Here is the corner radius -->
<corners android:radius="8dp" >
</corners>
</shape>
OUTPUT:
Hope this will help~
a) Increase "paddingLeft" value and shrink or remove "drawablePadding" in your case, adjust the value to a proper one; for example:
android:paddingLeft="50dp"
android:gravity="center_vertical|left"
b) Use a custom view.
A bit hacky solution:
<TextView
android:id="#+id/tv_whatsapp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#id/tv_call"
app:layout_constraintTop_toBottomOf="#id/tv_completed_message_desc"
android:layout_width="0dp"
android:layout_height="40dp"
android:textStyle="normal"
android:layout_marginTop="#dimen/rs_dp_13"
android:layout_marginRight="#dimen/rs_dp_12"
android:background="#drawable/rs_dark_teal_rectangle_bg"
android:textSize="14sp"
android:textColor="#color/dark_teal"
**android:paddingHorizontal="38dp"**
android:includeFontPadding="false"
**android:drawableStart="#drawable/rs_user_journey_whatsapp_icon"**
**android:drawableLeft="#drawable/rs_user_journey_whatsapp_icon"**
android:visibility="visible"
android:letterSpacing="0.04"
app:layout_constraintHorizontal_chainStyle="packed"
**android:gravity="center_vertical|end"**
android:text="Whatsapp"/>
Setting horizontal padding along with gravity as center_vertical|end will achieve the required behaviour. You can increase/decrease the padding as per requirement.
I have a cardview. The button default color is grey. I want it to look like the right side image(like the blessings). This right one has been created setting backgroundtint to white but in older versions its still shows grey.
<android.support.v7.widget.CardView
android:id="#+id/programm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentBottom="true"
android:layout_margin="6dp"
card_view:cardElevation="6dp"
android:layout_gravity="end|right"
card_view:cardBackgroundColor="#color/hotpink"
android:onClick="openNextActivity">
<Button
android:layout_width="wrap_content"
android:drawable="#color/white"
android:layout_height="wrap_content"
android:text="#string/Programme"
android:textColor="#color/hotpink"
android:onClick="openNextActivity"
/>
</android.support.v7.widget.CardView>
If i change the background of the button to white the entire color changes to white:
<android.support.v7.widget.CardView
android:id="#+id/programm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentBottom="true"
android:layout_margin="6dp"
card_view:cardElevation="6dp"
android:layout_gravity="end|right"
card_view:cardBackgroundColor="#color/hotpink"
android:onClick="openNextActivity">
<Button
android:layout_width="wrap_content"
android:drawable="#color/white"
android:background="#color/white"
android:layout_height="wrap_content"
android:text="#string/Programme"
android:textColor="#color/hotpink"
android:onClick="openNextActivity"
/>
</android.support.v7.widget.CardView>
As per google search, this default grey color is set in some theme.I probably need to overwrite some theme and set some attribute. But I am not sure what do I need to change.
Try this:
<Button
android:layout_width="wrap_content"
android:text="Button Text"
android:theme="#style/myButtonTheme"
android:layout_height="wrap_content"/>
set this myButtonTheme in your styles.xml file:
<style name="myButtonTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">#color/white</item>
<item name="colorButtonNormal">#5552f1</item>
</style>
[ set your desired background color in colorButtonNormal ]
Output:
i haven't tested it for this kind of situation, but i think something like this can work
you can create a background.xml in your drawable directory and set it as background
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item>
<shape>
<!-- set the shadow color here -->
<stroke
android:width="3dp"
android:color="#77000000" />
<!-- setting the thickness of shadow (positive value will give shadow on that side) -->
<padding
android:bottom="3dp"
android:left="1dp"
android:right="3dp"
android:top="1dp" />
<corners android:radius="4dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#fff" />
<corners android:radius="4dp" />
</shape>
</item>
things you can try with this is that you can set the shadow color the color you want or something else that i had in mind was set the opacity of shadow color to zero so that it would be transparent and you can set its thickness by the paddings of the shadow
hope this helps
In yout styles define:
<style name="BlueButtonTheme" parent="#style/AppTheme">
<item name="android:buttonStyle">#style/Widget.AppCompat.Button.Colored</item>
<item name="colorButtonNormal">#color/disabledButtonColor</item>
<item name="colorAccent">#color/enabledButtonColor</item>
<item name="android:disabledAlpha">1</item>
<!-- button text color-->
<item name="android:textColor">#drawable/colored_button_text_color</item>
</style>
And in your layout xml file:
<Button
android:id="#+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:theme="#style/BlueWhiteButtonTheme"
android:text="#string/buttonLabel"/>
Just set background for button in your layout
<Button
android:background="#android:color:white"
</Button>
I know how to make Material Design button with color fill:
style="#style/Widget.AppCompat.Button.Colored"
And no-bordered transparent button:
style="#style/Widget.AppCompat.Button.Borderless.Colored"
However, is there a way to make Material design bordered (transparent inside) button? Something like below?
You can also use the Material Components for Android.
Add the dependency to your build.gradle:
dependencies { implementation 'com.google.android.material:material:1.3.0' }
In this case you can use the MaterialButton in your layout file:
<com.google.android.material.button.MaterialButton
....
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="#color/colorPrimary"/>
Apply the style #style/Widget.MaterialComponents.Button.OutlinedButton.
In your case use the app:cornerRadius attribute to change the size of corner radius. This will round off the corners with specified dimensions.
Use te attribute app:strokeColor and app:strokeWidth to change the color and the width of the border.
You can also customize the corners using ShapeApperance (it requires version 1.1.0)
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearanceOverlay">#style/MyShapeAppearance</item>
</style>
<style name="MyShapeAppearance" parent="">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSize">8dp</item>
</style>
The official doc is here and all the android specs here.
With jetpack compose you can use the OutlinedButton and the border attribute:
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
shape = RoundedCornerShape(8.dp)
) {
Text(text = "Save")
}
OLD (support library)
With the new Support Library 28.0.0, the Design Library now contains the Material Button.
You can add this button to our layout file with:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXXX"
android:textSize="18sp"
app:icon="#drawable/ic_android_white_24dp" />
You can customize the button with these attributes:
app:backgroundTint: Used to apply a tint to the background of the button. If you wish to change the background color of the button, use this attribute instead of background.
app:strokeColor: The color to be used for the button stroke
app:strokeWidth: The width to be used for the button stroke
Also
Here's how to do it correctly.
What you need to do is
1 - Create shape drawable with stroke
2 - Create ripple drawable
3 - Create selector drawable for less than v21
4 - Create a new style for button with border
5 - Apply style on button
1 - Create shape with stroke
btn_outline.xml
<?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="#color/colorAccent">
</stroke>
<solid android:color="#color/colorTransparent"/>
<corners
android:radius="5dp">
</corners>
</shape>
2 - Create ripple drawable
drawable-v21/bg_btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorOverlay">
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/colorAccent"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:id="#android:id/mask">
<shape>
<stroke
android:width="2dp"
android:color="#color/colorAccent"/>
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</ripple>
android:id="#android:id/mask" is required to have ripple touch feedback on the button. The layer that is marked as mask is not visible on screen, its just for touch feedback.
3 - Create selector drawable for less than v21
drawable/bg_btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_outline" android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorOverlay"/>
</shape>
</item>
<item android:drawable="#drawable/btn_outline" android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorOverlay"/>
</shape>
</item>
<item android:drawable="#drawable/btn_outline"/>
</selector>
4 - Create a new style for button with border
All resources that that are needed to create the style are given above, that's how your style should look like
<style name="ButtonBorder" parent="Widget.AppCompat.Button.Colored"/>
<style name="ButtonBorder.Accent">
<item name="android:background">#drawable/bg_btn_outline</item>
<item name="android:textColor">#color/colorAccent</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:singleLine">true</item>
</style>
4 - Apply style on button
<Button
style="#style/ButtonBorder.Accent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
That's pretty much it. Here's a sample of how the buttons looks now.
You could do this pretty easily by setting Material Design Button's style attribute to #style/Widget.MaterialComponents.Button.OutlinedButton and setting app:strokeColor attribute value to your preferred color.
example:
<com.google.android.material.button.MaterialButton
android:text="Rounded outlined button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/btnRound"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeColor="#color/colorPrimaryDark"/>
References:
https://material.io/develop/android/docs/getting-started/
https://material.io/develop/android/components/material-button/
Credits to #NomanRafique for the detailed answer! However, because of the custom background, we've lost a few important things:
Button's height is bigger than the default Widget.AppCompat.Button
Paddings
Enable/Disable states
In case you're wondering, here is how the default background looks like: https://android.googlesource.com/platform/frameworks/support/+/a7487e7/v7/appcompat/res/drawable-v21/abc_btn_colored_material.xml
By reusing original insets, paddings and color selectors, in a simple case we can come up with something like this (all the values are default and come from android support/material library) :
drawable-v21/bg_btn_outlined.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/abc_btn_colored_borderless_text_material"/>
<corners android:radius="#dimen/abc_control_corner_material"/>
<padding
android:bottom="#dimen/abc_button_padding_vertical_material"
android:left="#dimen/abc_button_padding_horizontal_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"/>
</shape>
</item>
<item android:id="#android:id/mask">
<shape>
<stroke
android:width="2dp"
android:color="#color/abc_btn_colored_borderless_text_material"/>
<solid android:color="#android:color/white"/>
<corners android:radius="#dimen/abc_control_corner_material"/>
</shape>
</item>
</ripple>
</inset>
styles.xml
<style name="Button.Outlined" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:background">#drawable/bg_btn_outlined</item>
</style>
At this point, we should have an outlined button that responds to touches, respects the enabled="false" state and of the same height as the default Widget.AppCompat.Button:
Now, from here you can start customizing colors by providing your own implementation of the #color/abc_btn_colored_borderless_text_material color selector.
In your XML use this,
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Its a button"
android:textColor="#android:color/white"
android:textSize="#dimen/_12ssp"
app:backgroundTint="#android:color/transparent"
app:strokeColor="#android:color/white"
app:strokeWidth="#dimen/_1sdp" />
where
app:backgroundTint is used for background color
app:strokeColor is border color
app:strokeWidth is border width
This is how I do buttons only with a border and the ripple effect on Lollipop and above. Just like the AppCompat buttons, these have a fallback pressed effect on lower APIs (if you need ripples on lower APIs, you need to use an external library). I use FrameLayout because it's cheap. The color of the text and the border is black, but you can change it with a custom one:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/background_button_ghost">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:gravity="center"
android:padding="14dp"
android:textSize="16sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#android:color/black"
android:text="Text"/>
</FrameLayout>
drawable/background_button_ghost.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="#android:color/black"/>
<solid android:color="#color/transparent"/>
</shape>
If I have missed something, please leave a comment and I will update the answer.
You can do this with Jetpack Compose too. To do it, just create a composable function with a OutlinedButton and pass the as parameter the border you want:
#Composable
fun OutlineButton() {
OutlinedButton(
onClick = { //TODO - implement click here },
border = BorderStroke(1.dp, Color.Blue), // <-- border property
shape = RoundedCornerShape(corner = CornerSize(20.dp)),
colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Blue)
){
Text(text = "CONFIRM")
}
}
Just use MaterialButton with an outline style.
<com.google.android.material.button.MaterialButton
...
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="Delete Mentor Manager"
app:strokeColor="#color/...."
app:strokeWidth="1dp" />
Simply you can use this code. Its looks soo Good.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#F48025"
android:text="login"
android:textColor="#color/colorWhite" />
</LinearLayout>
Here border color is:
android:background="#ffffff"
And background color is:
android:backgroundTint="#F48025"
<Button
android:id="#+id/btn_add_discussion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:padding="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:backgroundTint="#80ffffff"
android:textColor="#color/colorPrimaryDark"
style="#style/btn_start_new_discussion"
android:text="Start new discussion" />
Put below code in Styles.xml file :
<style name="btn_start_new_discussion">
<item name="android:layout_marginTop">15dp</item>
<item name="strokeWidth">2dp</item>
<item name="strokeColor">#color/colorPrimary</item>
<item name="cornerRadius">10dp</item>
</style>
I want to create custom button and I need it to be circle. How can I create a circle button?
I do not think that be possible with draw9patch.
Also I do not know how to make custom button!
Do you have any suggestion?
Use xml drawable like this:
Save the following contents as round_button.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#fa09ad"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#c20586"/>
</shape>
</item>
</selector>
Android Material Effect: Although FloatingActionButton is a better option, If you want to do it using xml selector, create a folder drawable-v21 in res and save another round_button.xml there with following xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#c20586">
<item>
<shape android:shape="oval">
<solid android:color="#fa09ad"/>
</shape>
</item>
</ripple>
And set it as background of Button in xml like this:
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="hello"
android:textColor="#fff" />
Important:
If you want it to show all these states (enabled, disabled, highlighted etc), you will use selector as described here.
You've to keep both files in order to make the drawable backward-compatible. Otherwise, you'll face weird exceptions in previous android version.
Markushi wrote a circle button widget with amazing effects. Click here!
With the official Material Components library you can use the MaterialButton applying a Widget.MaterialComponents.Button.Icon style.
Something like:
<com.google.android.material.button.MaterialButton
android:layout_width="48dp"
android:layout_height="48dp"
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/ic_add"
app:iconSize="24dp"
app:iconPadding="0dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
/>
Currently the app:iconPadding="0dp",android:insetLeft,android:insetTop,android:insetRight,android:insetBottom attributes are needed to center the icon on the button avoiding extra padding space.
Use the app:shapeAppearanceOverlay attribute to get rounded corners. In this case you will have a circle.
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
The final result:
With jetpack compose you can use:
Button(
onClick = { /* Do something! */ },
modifier = Modifier.width(48.dp).height(48.dp),
shape = CircleShape
) {
Icon(Icons.Filled.Add, "")
}
AngryTool for custom android button
You can make any kind of custom android button with this tool site...
i make circle and square button with round corner with this toolsite..
Visit it may be i will help you
For a FAB looking button this style on a MaterialButton:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
app:cornerRadius="28dp"
android:layout_width="56dp"
android:layout_height="56dp"
android:text="1" />
Result:
If you change the size be careful to use half of the button size as app:cornerRadius.
You can use MaterialButton from AndroidX material library
<com.google.android.material.button.MaterialButton
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="10dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:cornerRadius="50dp"
app:icon="#drawable/ic_camera"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="35dp" />
and it will be like this
if you want use VectorDrawable and ConstraintLayout
<FrameLayout
android:id="#+id/ok_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:background="#drawable/circle_button">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/icon_of_button"
android:layout_width="32dp"
android:layout_height="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="#drawable/ic_thumbs_up"/>
<TextView
android:id="#+id/text_of_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/icon_of_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="5dp"
android:textColor="#android:color/white"
android:text="ok"
/>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
circle background: circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#41ba7a" />
<stroke
android:width="2dip"
android:color="#03ae3c" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
Unfortunately using an XML drawable and overriding the background means you have to explicitly set the colour instead of being able to use the app style colours.
Rather than hardcode the button colours for every behaviour I opted to hardcode the corner radius, which feels marginally less hacky and retains all the default button behaviour (changing colour when it's pressed and other visual effects) and uses the app style colours by default:
Set android:layout_height and android:layout_width to the same value
Set app:cornerRadius to half of the height/width
(It actually appears that anything greater than or equal to half of the height/width works, so to avoid having to change the radius every time you update the height/width, you could instead set it to a very high value such as 1000dp, the risk being it could break if this behaviour ever changes.)
Set android:insetBottom and android:insetTop to 0dp to get a perfect circle
For example:
<Button
android:insetBottom="0dp"
android:insetTop="0dp"
android:layout_height="150dp"
android:layout_width="150dp"
app:cornerRadius="75dp"
/>
here is how you can perform simply, make a drawable resource file in drawable.xml. Say round_button.xml and then paste the following code.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid
android:color="#color/button_start_gradient_color"/>
</shape>
</item>
<item
android:drawable="#drawable/microphone"/>
</layer-list>
Note:- use your own color and drawable resource as i have used #drawable/microphone
Following is the result
[1]: https://i.stack.imgur.com/QyhdJ.png
If you want to do with ImageButton, use the following. It will create round ImageButton with material ripples.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_settings_6"
android:background="?selectableItemBackgroundBorderless"
android:padding="10dp"
/>
Create a new vector asset in the drawable folder.
You can import your PNG image as well, and convert the file to SVG online at https://image.online-convert.com/convert-to-svg. The higher the resolution, the better the conversion will be.
Next, create a new vector asset from that SVG file.
This is a sample vector circle image you can use. Copy the code to an xml file in the drawables folder.
ic_check.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="256"
android:viewportWidth="256">
<path
android:fillColor="#2962FF"
android:pathData="M111,1.7c-7.2,1.1 -22.2,4.8 -27.9,7 -33.2,12.5 -61.3,40.3 -74.1,73.3 -8.7,22.6 -10.5,55.3 -4.4,78 10.9,40 39.7,72.4 77.4,87 22.6,8.7 55.3,10.5 78,4.4 45.3,-12.3 79.1,-46.1 91.4,-91.4 2.9,-10.7 3.9,-21.9 3.3,-37.4 -0.7,-21.2 -4.6,-35.9 -14,-54.1 -18.2,-35 -54,-60.5 -93.4,-66.4 -6.7,-1 -30.7,-1.3 -36.3,-0.4zM145,23.1c21.8,3.3 46.5,16.5 61.1,32.8 20.4,22.6 30.1,51.2 27.7,81.1 -3.5,44.4 -35.9,82.7 -79.6,94 -21.6,5.6 -46.6,3.7 -67.8,-5.1 -10.4,-4.3 -24.7,-14.1 -33.4,-22.9 -41.6,-41.5 -41.6,-108.4 0,-150 24.3,-24.3 57.6,-35.1 92,-29.9z"
android:strokeColor="#00000000" />
<path
android:fillColor="#2962FF"
android:pathData="M148.4,113c-24.6,26 -43.3,44.9 -44,44.6 -0.7,-0.3 -8.5,-6.1 -17.3,-13 -8.9,-6.9 -16.5,-12.6 -17,-12.6 -1.4,-0 -25.6,19 -25.8,20.3 -0.3,1.4 62.7,50.2 64.8,50.2 1.7,-0 108.4,-112.3 108.4,-114.1 0,-1.3 -23.8,-20.4 -25.4,-20.4 -0.6,-0 -20.2,20.3 -43.7,45z"
android:strokeColor="#00000000" />
</vector>
Use this image in your button:
<ImageButton
android:id="#+id/btn_level1"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/ic_check"
/>
Your button will be a circle button.