Как закруглить кнопку в android studio
Перейти к содержимому

Как закруглить кнопку в android studio

  • автор:

Rounded Button in Android Studio

Nidhi Vanjare

There is no default attribution to make a button round in Android Studio. So to do that we have to add a new XML file to make the button round.

Drawable > New > Drawable Resource File (example: rounded_corner.xml)

Color and corner radius for the button can be modified as per your needs. The changes you make can be seen in the design option in the side:

Now , in the main xml file where you want the rounded corners for the button , add this line : android:background=”@drawable/rounded_corner ” .

Android Button Design, Custom Button, Round Button, Color

Android Button Design, Custom Button, Round Button, Color

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

In this tutorial, we’ll be customizing the Buttons in our Android Application. If you aren’t aware of Android Buttons, check out this tutorial before proceeding. We’ll be setting selectors and shapes on our buttons in xml.

Android Button Design

A selector is used to define a different behaviour for different states of the Button. What are drawable states? Each of the following events of a view (Button or any other kind of view) are a type of state:

  • state_pressed
  • state_selected
  • state_focused
  • state_enabled

state_focused is when you hover over the widget. Typically works only on emulators. state_selected is meant to select the view. Doesn’t work with Buttons. Works with RadioButtons. Typically for a button, there are just three important states: normal, pressed and enabled. For each of the states of the selector, we can set a different drawable/color/shape on our button. Let’s get started with the implementation of each of these in a new Android Studio Project.

Android Custom Button Project Structure

We’ll be looking at each of the drawable files one at a time. The colors are defined in the colors.xml file : A selector can be an xml file created inside the drawable folder. Selector for different background colors. The following selector file btn_bg_selector.xml contains the code for setting different background colors on a button for different states.

In the above code, each of the states is represented using item tag. The selector tag behaves like an if — else if statement to an extent. It checks every condition from top to bottom. Whenever the condition matches, it sets the relevant things on the button and stops processing the next item tags. The third item tag is the default one. It’s important that we keep it at the last. Keeping it at the top would not allow the other two item tags to get executed ever. We set the above drawable selector file on our button in the activity_main.xml as:

The selector is set on the background attribute of the button.

Selector for Disabled Button

The following selector btn_bg_selector_disabled.xml is used on a button which is not enabled.

For the above selector to work, we need to specify android:enabled as false.

Selector with Different Drawables

We can set a different drawable image to be displayed based upon the state of the button. The selector code that does so is present in the file btn_drawable_selector.xml

Note: The focused state doesn’t work on smart phones. Now the drawable images when set as the button background can get stretched if it’s width/height is larger than the button’s. So we need to set the Button width/height in accordance with the drawable image. We can do so either by hardcoding in xml or getting the drawable dimensions programmatically and setting it over the button. For the sake of convenience we’ve done the former here:

ImageButton is the ideal View to be used when you need to display a drawable as the button background only since it fits the drawable appropriately. ImageButton comes with the android:scale attribute to resize the drawable image.

Android Button Color

We can change the text color on the button based on the state in the selector. The following code from btn_txt_selector.xml does so. We need to use android:color here in place of android:drawable .

The button in the layout:

Android Button Shapes

We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line . The most used tags inside the shape tag are:

  • <gradient> — Setting start and end colors of the gradient along with the type(radius, linear, sweep)
  • <stroke> — Setting border color and width
  • <solid> — Setting the solid color on the button
  • <corners>— Setting radius

Android Round Corner Button

The xml code for the btn_shape_round.xml file is given below:

Just like selectors, we can set this on the android:background tag on the Button in our xml layout.

Android Button Shape With Gradient

In the following btn_shape_gradient.xml file, we’ve set the gradient as a radial one. We must set the gradient_radius attribute too.

Android Button Shape and Selector Together

The btn_selector_shape.xml holds the selector. Each of the items has a shape specified.

Set this on the Button and the shape would change from rectangle to oval when the button is clicked. A linear gradient should have the angle specified in the multiples of 45, else it’ll crash. Setting the Button shape as capsule btn_shape_capsule.xml is where we set the shape inside the selectors as a capsule.

The code for the MainActivity.java class which hosts all of the above button examples is given below.

Android Custom Button Design App Output

The output of the above application in action is given below. This brings an end to this tutorial on Button selectors and shapes. You can download the final android studio project from the below link.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

How to Android button rounded corners | Radius, ripple, corners, design?

Do you want to make the corners of a button round in Android? In this tutorial, you will learn an easy way to achieve this in Android.

Android button rounded corners

To create an Android button rounded corners have to use a new XML file. This new XML file will be inside a drawable folder. To change the corners of all sides of the button need only one attribute in the drawable XML file.

To change one or two or three corners, you have to use the following attributes:-

Step 7. Now Run the application, in an emulator or on your Android device
Output screenshot Android corner button example:

Download source code of button design from an Activity in Kotlin

Q: What is an android material button and how to set the corners (radius) of it?

Answer: Material Button is a customizable button component with updated visual styles

How to Use

Q: How to create a circle button in android?

Answer: Same as the above example, use XML drawable file:

Save the following contents as round_button.xml in drawable folder.

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 the following XML.

And set it as background of Button in xml like this:

Important:

  1. To show all state: -enabled, disabled, highlighted, etc, you have to use selector as described here.
  2. You’ve to keep both files in order to make the drawable backward-compatible. Otherwise, you’ll face weird exceptions in the previous android version

Source: https://stackoverflow.com/questions/9884202/custom-circle-button

Q: How to create Android button rounded corners programmatically?

Answer: Here code for how to create GradientDrawable shape programmatically.

And change the radius for all corners of the button.

or Change the radius for specific corners of the button.

And finally, use this drawable as a background as of button widget:

Note: A button design topic is UI design. in this tutorial, we used a Kotlin programming language in Main Activity and will work the same with java code.

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=”25″
TargetSdkVersion=”28″
Coding in Kotlin

How to make the corners of a button round?

I want to make the corners of a button round. Is there an easy way to achieve this in Android?

Jonik's user avatar

20 Answers 20

If you want something like this

Button preview

here is the code.

1.Create a xml file in your drawable folder like mybutton.xml and paste the following markup:

2.Now use this drawable for the background of your view. If the view is button then something like this:

Daniel Lerps's user avatar

Md. Monsur Hossain Tonmoy's user avatar

Create a xml file in drawable folder like below

Apply this as background to button you want make corners round.

Or you can use separate radius for every corner like below

Sandip Jadhav's user avatar

Is there an easy way to achieve this in Android?

With Jetpack Compose you can use the shape parameter:

enter image description here

With the Material Components library you can use the MaterialButton with the app:cornerRadius attribute.

enter image description here

It is enough to obtain a Button with rounded corners.

You can use one of Material button styles. For example:

enter image description here

Also starting from the version 1.1.0 you can also change the shape of your button. Just use the shapeAppearanceOverlay attribute in the button style:

You can also apply the shapeAppearanceOverlay in the xml layout:

The shapeAppearance allows also to have different shape and dimension for each corner:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *