I would like to add a border to a button.
I have tried the usual stuff i.e creating a drawable file and referencing that in the xml file. But this does not seem to be working. Anyone know why?
Here is my XML code for the button:
<Button
android:id="#+id/logoutButton"
android:layout_width="357dp"
android:layout_height="51dp"
android:background="#drawable/round_btn"
android:fontFamily="#font/work_sans"
android:text="Log Out"
android:textAllCaps="false"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.992"
/>
Here is my code for the drawable file 'round_btn':
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#6200EE"/>
<stroke
android:width="20px"
android:color="#000000"
/>
<corners
android:radius="50dp"
/>
</shape>
No border is added but the corners are modified (curved).
Adding this line:
app:backgroundTint="#null"
solves the problem.
So, your final code (for the button) becomes:
<Button
android:id="#+id/logoutButton"
android:layout_width="357dp"
android:layout_height="51dp"
android:background="#drawable/round_btn"
android:fontFamily="#font/work_sans"
android:text="Log Out"
android:textAllCaps="false"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.992"
app:backgroundTint="#null"
/>
You may look into this question: Android Background Drawable Not Working in Button Since Android Studio 4.1 for more information
Related
This question already has answers here:
Android button background is taking the primary color
(6 answers)
Closed 3 months ago.
i want to add button color but it didnt show up
here is the drawable
rounded_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#47D476"/>
<corners android:radius="15dp"/>
</shape>
and this the layout
<Button
android:id="#+id/buttonOKE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/rounded_button"
android:text="SUBMIT"
/>
how to set the colors it didnt change to green
color from the rounded_button.xml
the result
thank you.
You should set #null at backgroundTint .
app:backgroundTint="#null"
Final Button xml code:
<Button
android:id="#+id/buttonOKE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="SUBMIT"
app:backgroundTint="#null"
android:background="#drawable/rounded_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
You can try adding the following code to the button.
app:backgroundTint="#null"
You can use textview instead of button like this
<TextView
android:id="#+id/tvDiscount"
style="#style/txt_white_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start
android:background="#drawable/discount_horizontal"
android:gravity="center"
android:paddingLeft="#dimen/_5sdp"
android:paddingRight="#dimen/_5sdp"
android:text="" />
You can apply color and shape both things at once
i have a strange issue. I created a simple Button which looks like that :
<com.google.android.material.button.MaterialButton
android:id="#+id/continue_button"
android:background="#drawable/green_button_selector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/landing_margin"
android:layout_marginEnd="#dimen/landing_margin"
android:layout_marginBottom="#dimen/landing_margin"
android:text="#string/login_button_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
I want to make Button green with rounded corners, so I created a xml file called green_button_selector and set is as button background. This file code's is posted below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#color/colorGreenButton" />
<corners android:radius="10dp" />
</shape>
But instead of my button getting green it has colorAccent of my app, any ideas what am I doing wrong?
If you are using material button you don't need to create custom drawable.
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
app:backgroundTint="#android:color/holo_green_dark"
app:cornerRadius="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="BUTTON" />
More: https://material.io/develop/android/components/material-button/
<com.google.android.material.button.MaterialButton
android:id="#+id/material_text_button"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/outlined_button_label_enabled"/>
You can below property for same.
app:cornerRadius
app:backgroundTint
Read more from
Material Button official document
This post has plenty of answers that explain how to create a rounded button in Android. But almost all of the answers end up expanding the size of the button.
For instance, in this layout, notice how the top button (the one with the rounded corners) has significantly more padding then the rest of the buttons.
Here's the code for a button without rounded corners;
<Button
android:id="#+id/useKeyboardButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/use_keyboard_msg"
app:layout_constraintBottom_toTopOf="#+id/privacyPolicyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enableKeyboardButton"
style="#style/Widget.AppCompat.Button.Colored"
/>
And here's the code for a button with rounded corners:
<Button
android:id="#+id/enableKeyboardButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/enable_keyboard_msg"
app:layout_constraintBottom_toTopOf="#+id/useKeyboardButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="#style/Widget.AppCompat.Button.Colored"
android:background="#drawable/rounded_corners"
/>
where the background is this drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorAccent"/>
<corners android:radius="10dp"/>
</shape>
Try removing the style attribute and only setting the background drawable.
This could be done with newer support library (v28) MaterialButton.
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MATERIAL BUTTON"
android:textSize="18sp"
app:icon="#drawable/ic_android_white_24dp" />
Take a look at this great article
Use MaterialButton
and add app:cornerRadius="8dp"for rounded corner
I created a simple circular button to create - and + buttons for a custom dialog picker.
As seen in the screenshot the - and the + are shifted a little bit down and not centered in the middle of the custom background.
My custom background in the drawable/circular_button.xml looks like this.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#color/colorPrimaryDark" android:width="1dp" />
<solid android:color="#color/colorPrimary" />
<size android:width="30sp" android:height="30sp"/>
</shape>
The button is configured like this inside the LinearLayout
<Button
android:id="#+id/decrease_one"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_marginRight="20sp"
android:background="#drawable/rounded_button"
android:text="-"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
In one of the posts I read about the tag minHeight but it didn't solve my problem.Any ideas ?
Batter to use FloatingActionButton mini
<android.support.design.widget.FloatingActionButton
android:id="#+id/activity_my_digital_executor_fabDigitalex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:fabSize="mini"
card_view:srcCompat="#drawable/ic_new_plus" />
Add 'com.android.support:design:26.0.+' in your App dependencies
This is happen because of your Button's android:textSize="30sp" if you set 50sp its goes down the line more,
Just convert your Buttons to Textviews and set android:gravity="center" also keep your textsize 30sp
<TextView
android:id="#+id/decrease_2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounded_button"
android:text="+"
android:gravity="center"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
If you are use a Imageview instead of Textview than your problem is solved.
Here I am writing the code of XML That I am using to change the background of button.
roundbutton2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#eea0e5ee"/>
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topRightRadius="10dp"
android:topLeftRadius="10dp"/>
</shape>
Buttons snippet
<Button
android:layout_width="125dp"
android:layout_height="55dp"
android:text="Clear"
android:id="#+id/bt_clear"
android:layout_marginLeft="55dp"
android:layout_marginRight="30dp"
android:textColor="#FFFFFF"
android:textSize="25dp"
android:textAllCaps="false"
android:background = "#drawable/roundbutton2"/>
<Button
android:layout_width="125dp"
android:layout_height="55dp"
android:text="Proceed"
android:textColor="#FFFFFF"
android:textSize="25dp"
android:id="#+id/bt_proceed"
android:textAllCaps="false"
android:background = "#drawable/roundbutton2"/>
For first i want the #EE494D4E color and for second button i want #eea0e5ee.How can do this.
I want the different color for both button using same XML file.
Please Tell me about this.Thanx
In your roundbutton2.xml you are directly setting the color of the shape value to #eea0e5ee which causes your buttons that set it as a background to have that color. What I did was remove the <solid android:color="#eea0e5ee"/> and set the backgroundTint via the xml to your preferred color, like so:
<Button
android:id="#+id/bt_clear"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_marginLeft="55dp"
android:layout_marginRight="30dp"
android:background="#drawable/roundbutton2"
android:backgroundTint="#EE494D4E"
android:text="Clear"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="25dp" />
<Button
android:id="#+id/bt_proceed"
android:layout_width="125dp"
android:layout_height="55dp"
android:background="#drawable/roundbutton2"
android:backgroundTint="#eea0e5ee"
android:text="Proceed"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="25dp" />
NOTE: This is just a workaround. I'm not entirely sure if this should be done, or is against best practice. This answer here is my reference. If you still want the default color value of your button to stay, the only way you could achieve changing the color of other buttons is programmatically.
Cheers! :D