- Android — border for button
- 11 Answers 11
- • Android Official Solution
- 1. When you use androidx :
- 2. When you use appcompat :
- Visual Result
- Material design button with border
- 8 Answers 8
- Set background color and border color of Button using Shape Drawable in Android 8
- 1 Answer 1
- How to change border color of radio button in android? [duplicate]
- 3 Answers 3
- Not the answer you’re looking for? Browse other questions tagged android android-ui or ask your own question.
- Linked
- Related
- Hot Network Questions
- How to make the corners of a button round?
- 17 Answers 17
Android — border for button
How do I add a border to a button? Is it possible to do this without resorting to use of images?
11 Answers 11
Step 1 : Create file named : my_button_bg.xml
Step 2 : Place this file in res/drawables.xml
Step 3 : Insert below code
Step 4: Use code «android:background=»@drawable/my_button_bg» where needed eg below:
• Android Official Solution
Since Android Design Support v28 was introduced, it’s easy to create a bordered button using MaterialButton . This class supplies updated Material styles for the button in the constructor. Using app:strokeColor and app:strokeWidth you can create a custom border as following:
1. When you use androidx :
build.gradle
• Bordered Button:
• Unfilled Bordered Button:
2. When you use appcompat :
build.gradle
style.xml
Ensure your application theme inherits from Theme.MaterialComponents instead of Theme.AppCompat .
• Bordered Button:
• Unfilled Bordered Button:
Visual Result
Create a button_border.xml file in your drawable folder.
res/drawable/button_border.xml
And add button to your XML activity layout and set background android:background=»@drawable/button_border» .
and point it out as @drawable/button_green :
Once you have done this, in the XML for your button set android:background=»@drawable/your_button_border»
If your button does not require a transparent background, then you can create an illusion of a border using a Frame Layout. Just adjust the FrameLayout’s «padding» attribute to change the thickness of the border.
I’m not sure if the shape xml files have dynamically-editable border colors. But I do know that with this solution, you can dynamically change the color of the border by setting the FrameLayout background.
Источник
Material design button with border
I know how to make Material Design button with color fill:
And no-bordered transparent button:
However, is there a way to make Material design bordered (transparent inside) button? Something like below?
8 Answers 8
Add the dependency to your build.gradle :
In this case you can use the MaterialButton in your layout file:
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)
The official doc is here and all the android specs here.
With jetpack compose 1.0.x you can use the OutlinedButton and the border attribute:
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:
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
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
2 — Create ripple drawable drawable-v21/bg_btn_outline.xml
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
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
4 — Apply style on button
That’s pretty much it. Here’s a sample of how the buttons looks now.
Источник
Set background color and border color of Button using Shape Drawable in Android 8
I am trying to set background color and border color of Button using Shape Drawable (create button_start.xml in drawable) in Android 8, but it does not seem to work.
button_start.xml file:
activity_main.xml file:
result:
1 Answer 1
Short answer:.
You don’t need to define a background shape, just use a MaterialButton with the shapeAppearanceOverlay attribute:
Long answer:
If you want to use a background shape you have to add app:backgroundTint=»@null» .
Something like:
Using a Material Components Theme the Button is replaced at runtime by a MaterialButton which use a own MaterialShapeDrawable as background. You can define a custom background but to avoid that the custom background doesn’t get tinted you have to add app:backgroundTint=»@null» .
The 2 solutions are not equivalent.
Using a custom android:background the default MaterialShapeDrawable is not used and some features like stroke, shapeappearance, ripple are not set (since they are related to the MaterialShapeDrawable ) . You have to provide them with your custom background.
Источник
How to change border color of radio button in android? [duplicate]
How to change border color of radio button in android?
I want to change circle color of android radio button.
Thanks in advance. Any help is greatly appreciated.
I have try different soluation and concern if any property is there other that drawable image
3 Answers 3
Yes, this is the property you are looking for: buttonTint, but only works on api level 21 or above
in your values/colors.xml put your color in this case a reddish one:
The easiest way to customize(change color) your views.
- Select the color you want for Radio Button
- Download Output Resources (Download.Zip)
- Extract zip contents
- Copy files from extracted zip to your project drawables, styles and values
- Change the parent of your app theme to the one you downloaded
- And you have got what you wanted
Try using AppCompatRadioButton
Not the answer you’re looking for? Browse other questions tagged android android-ui or ask your own question.
Linked
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
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?
17 Answers 17
If you want something like this
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:
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
Is there an easy way to achieve this in Android?
Yes, today there is, and it is very simple.
Just use the MaterialButton in the Material Components library with the app:cornerRadius attribute.
It is enough to obtain a Button with rounded corners.
You can use one of Material button styles. For example:
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:
With Jetpack Compose 1.0.x you can use the shape parameter:
Источник