This question already has answers here:
Android button background is taking the primary color
(6 answers)
Closed 10 months ago.
I have set it up to where my button's style is borderless and I'm using a drawable for the button background android:background="#drawable/circle"
This is the code for the #drawable/circle file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape = "rectangle">
<corners android:radius="1000dp"/>
<solid android:color="#263238"/>
</shape>
</item>
</selector>
and that color (#26338) does not appear on the button instead the button looks like this
what it looks like in design mode
In case you're wondering heres the XML code for the button
<Button
android:id="#+id/clearBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:backgroundTint="#color/red"
android:fontFamily="#font/oswald_medium"
android:text="#string/clear"
android:textColor="#FFFFFF"
android:textSize="24sp" />
When in reality it should be more like this color
this is the color I'm looking for
Some more additional info is that my button is in a TableRow inside of a tableLayout. Also when I try applying a backgroundTint it also does not work. Am using the latest version of android studio and I'm pretty sure everything else is up to date.
How can I fix this? Thanks.
That is Button bug.
change Button
to androidx.appcompat.widget.AppCompatButton
use bellow xml for create button background.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#263238"/>
</shape>
this code for use drawable with button.#drawable/circle is your background drawable name.
<Button
android:id="#+id/clearBTN"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:fontFamily="#font/oswald_medium"
android:text="#string/clear"
android:textColor="#FFFFFF"
android:textSize="24sp" />
I had the same problem with Buttons.
use AppCompatButton instead using Button.
package : androidx.appcompat.widget.AppCompatButton
Related
<Button
android:id="#+id/sort_time"
android:layout_width="0dp"
android:layout_height="52dp"
android:layout_marginTop="54dp"
android:background="#drawable/roundless_button"
android:backgroundTint="#2A56C6"
android:text="#string/order_by_time"
android:textSize="14sp"
app:layout_constraintEnd_toStartOf="#+id/sort_status"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/IndBlue"
>
<shape android:shape="rectangle">
</shape>
<solid android:color="#color/IndBlue" />
</item>
</selector>
I tried to edit the buttons colors, but for some reason it won't change. Solid android:color / android:drawable doesn't work even though I placed it everywhere.
when you use selector as button background it inherits the color from theme. you can try changing the theme primary color and that will affect the button color.
This button is displaying a purple color and its XML code is
android:id="#+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="141dp"
android:layout_marginEnd="142dp"
android:layout_marginBottom="48dp"
android:background="#drawable/btn_login"
android:fontFamily="#font/poppins"
android:text="Login"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/tvCreateAccount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
"#drawable/btn_login" is a shape I created -
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#71D775">
</solid>
<corners android:radius="66dp">
</corners>
</shape>
And this is how it looks like,
This is because the default MaterialButton style overrides backgroundTint.
So if we are using a custom android:background, we have to make sure to null out backgroundTint. We can achieve it by any of the following way:
Add any of these line to your button XML.
app:backgroundTint="#null"
OR
app:backgroundTint="#empty"
For some reason, you need to set the app:backgroundTint to null.
Like so:
app:backgroundTint="#null"
layout_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
main_activity.xml
<ImageButton
android:background="#drawable/layout_bg"
android:src="#drawable/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#android:color/black"
android:minWidth="80.0dp"
android:minHeight="80.0dp"
android:padding="10px"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:id="#+id/btnFoo"
android:layout_marginLeft="91.0dp"
/>
I am setting the background of my ImageButton to a layout so I can have dynamic rounded corners as you can see in this image:
However, I need to change the background color of just that button to another solid color such as red.
If I set the background color with btnFoo.SetBackgroundColor(Color.Red), then the rounded corners are gone.
I assume because it overwrote the background attribute to be a solid color value rather than the layout_bg.xml file.
How can I set the background color of this specific ImageButton and not all the ImageButtons in my main_activity that use layout_bg as the background?
You need to make another drawable with the color Red then use it :
btnFoo.setBackgroundResource(R.drawable.red_drawable);
use material button all functionality is built in already
<com.google.android.material.button.MaterialButton
android:layout_width="size"
android:layout_height="size"
android:backgroundTint="#color/colorPrimary"
android:text="Add Images"
android:textAllCaps="false"
android:textColor="#color/white"
app:cornerRadius="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"/>
You can use something like:
btnFoo.background.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
color, BlendModeCompat.SRC_ATOP)
Note: it requires androidx.core:core:1.2.0
As alternative you can use the MaterialButton :
<com.google.android.material.button.MaterialButton
android:layout_width="80dp"
android:layout_height="80dp"
android:insetTop="0dp"
android:insetBottom="0dp"
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="48dp"
android:backgroundTint="#color/..."
android:textColor="#FFFFFF"
app:cornerRadius="10dp"/>
In this case just use:
button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)
I am trying to give my button a gradient background and stronger stroke. I have read/seen many tutorials but nothing works for me.
This is my addon .xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:shape="rectangle">
<gradient android:startColor="#25AAFF"
android:endColor="#004F81">
</gradient>
<stroke
android:width="5dp"
android:color="#000000">
</stroke>
<corners android:radius="5dp"></corners>
NOTE: The corners work fine
and Button file
<Button
android:background="#drawable/login_button"
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/login_password_input"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/login_button_text"
android:textColor="#FFFFFF"
android:textStyle="bold">
</Button>
Using the Material Components Library there is no difference between <Button /> and <com.google.android.material.button.MaterialButton />. Check this post for more info.
MaterialButton ignored android:background until release 1.2.0-alpha06.
With this release you can have a gradient background using something like:
<Button
android:background="#drawable/bg_button_gradient"
app:backgroundTint="#null"
... />
Also you can apply the stroke using the app:cornerRadius, app:strokeWidth and app:strokeColor attributes:
<Button
app:cornerRadius="4dp"
app:strokeWidth="1dp"
app:strokeColor="#color/primaryLightColor"
..>
If you are using an earlier versions of the material components library you can use the AppCompatButton:
<androidx.appcompat.widget.AppCompatButton
android:background="#drawable/bg_button_gradient"
..>
I'm developing Android app for 4.0 version, and all checkboxes have got a black background for tick, but I need white. If I use "background" xml attribute as "white" then all checkbox (with text) is white; I need only white place for tick.
The most simple way - emulate CheckBox using ImageView with 3 background states (stateSelected=true, stateSelected=false, statePressed = true). Just create appropriate xml file with 3 backgrounds and set it to ImageView background. Then in code, when click on ImageView just switch ImageView.setSelected = !ImageView.isSelected. Hope its help
I dont know if i am extremely late or what, but here is the solution
Code snippet:
say this selector is name "checkbox_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true">
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true">
</item>
in order to set it just for the checkbox and not the entire text also, you could have it as:
<CheckBox
android:layout_width="45dip"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:button="#drawable/checkbox_selector"
android:text="#string/text_here" />
Maybe my solution is not very elegant, but it works for me:
I simplily create another layout with the dimensions of the checkbox rectangle (15x15) and with white background. Then I add this layout to a RelativeLayout, where I also put the checkbox. With a margin of 9dp, I put the white layout-rectangle below the checkbox, so it seems that the checkbox bacground color is white.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/login_large_margin"
android:layout_marginTop="#dimen/login_medium_margin" >
<RelativeLayout
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="9dp"
android:background="#color/white" >
</RelativeLayout>
<CheckBox
android:id="#+id/login_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="#string/login_check"
android:textColor="#color/white" >
</CheckBox>
</RelativeLayout>