android:background attribute not being applied to buttons - android

This is my very first question on StackOverflow, I expect great help from the community :)
I've looked around for this problem's solution for hours but I'm kinda puzzled..
I'm trying to create a custom outlined rounded button for my android app but when I'm applying it through the android:background attribute in XML file, It doesn't quite work. The default background color (purple) is not removed. The stroke is not applied too. However, corners are rounded.
Some say android:background is not working on MaterialButtons. Some say create a style in themes.xml file and reapply it thru android:backgroundTint attribute (It doesn't work either).
Some say try to change it programmatically(not working too!).
Please provide an efficient solution.
Here's my code and what I'm achieving with it.
activity_main
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#drawable/round_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
round_button
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/black" />
<stroke
android:width="3dp"
android:color="#color/teal_200" />
<corners android:radius="25dp" />
</shape>
Here's the result that I get
This is what I want to achieve.
I'm really looking forward to your answers! Thanks.

I faced this before and all I did was change the main theme in the file theme to Appcompat instead of the Material Theme

Hello #Shafique Ahmed,
First of all Welcome to Stack Overflow, and now to answer your question try changing your drawable file as follows and let me know if it works:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape>
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#android:color/transparent" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape></item>
</selector>
Edit: You can also change one of your activity's theme from Material Components to App Compact by the following steps:
In the styles.xml(Sometimes also named as themes.xml) make another theme below your main one(New theme: App Compact while keeping the Main theme as Material Components) and give it any name
2.Go the the activity file of the layout you want to change the theme of, and add then call the setTheme(R.style.NameOfTheme); function.
Note: Call the set theme() function before setting the Content View (which is executed by setContentView)
Edit 2: Try refering to
this question to answer your question:

use app:backgroundTint to change your button color

Related

android: background is not working in the material design button component

I want to make a gradient button in an Android app that I'm developing in Kotlin. I have made the drawable file for the gradient background but when I do give the button the customized background it doesn't work, nothing changes. This is the code for the gradient background and below that is the XML code for the button.
<item>
<shape android:shape="rectangle">
<gradient android:startColor="#color/start_color"
android:endColor="#color/end_color"/>
<corners android:radius="10dp"/>
</shape>
</item>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo"
android:layout_marginHorizontal="26dp"
tools:layout_editor_absoluteX="16dp"
app:cornerRadius="10dp"
android:textSize="20sp"
android:text="Create an account"
android:background="#drawable/create_acc_btn"
android:fontFamily="#font/montserrat" />
You just have to fix your gradient drawable file.
remove the item tag from it and put android xml namespace declaration like this :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#color/start_color"
android:endColor="#color/end_color"/>
<corners android:radius="10dp"/>
</shape>
And add this line to your button xml code :
app:backgroundTint="#null"
without this your gradient background won't show up
Note : MaterialButton manages its own background to control elevation, shape, color and states. Consider using backgroundTint, shapeAppearance and other attributes where available. A custom background will ignore these attributes and you should consider handling interaction states such as pressed, focused and disabled
You are also going to lose ripple effect when using custom background.

Applying a top and bottom border to TextView using background doesn't work, but foreground does

I've been trying to set a top and bottom border to a TextView, which rests inside a ConstraintLayout, which is sitting in a CardView:
<androidx.cardview.widget.CardView
android:id="#+id/ham_frame"
android:layout_width="244dp"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="?attr/background"
app:cardCornerRadius="4dp"
app:cardElevation="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/ham_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:background="#drawable/border_top_bottom"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
The layer-list:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-2dp" android:left="-2dp" android:right="-2dp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="?attr/colorSeperator" />
<solid android:color="#00000000" />
</shape>
</item>
<item android:bottom="-2dp" android:left="-2dp" android:right="-2dp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="?attr/colorSeperator" />
<solid android:color="#00000000" />
</shape>
</item>
</layer-list>
Now, I have gone through the questions asked about how to do that earlier, and followed multiple variations of the layer-list drawable to set as the background of the TextView, but that is simply not working for me (no borders are rendered in both a device and the editor).
Instead of that, setting the drawable as foreground just works, but the issue with that is it seems to be unsupported below API 23 (which is not acceptable).
Multiple answers here:
Is there an easy way to add a border to the top and bottom of an Android View?
seem to instruct to set the drawable as background, and there doesn't seem to be anyone facing the same issue - what could I be doing wrong?
EDIT: ?attr/colorSeperator is correctly set to #764AFF1A
Double-checked, neither the editor (API 26-29) nor my LG device (G7+ Pie) render the borders.
On device, with drawable set as background:
With drawable set as foreground:
Please note the shadow-esque appearance at the top and left edges are of the container, not the TextView (which is supposed to display a slight purple-ish border).
Exploring as in the discussion in the question comments with Zain, the theme (under styles) I was using had set the backgroundTint to the background color of the activity (and other components in which the TextView was lying).
So, even though the background was being set as the border drawable, it was being tinted to the color (by default) of the background of its parent, so the background was indistinguishable.
Note however that setting an explicit backgroundTint on the TextView (while the one in styles existed), did not make the background visible, so the backgroundTint from default styling had precedence over the one set individually.
This behavior seems to contradict Style hierarchyIf I misinterpreted, kindly correct me
TL;DR: If you're having a similar issue, check the backgroundTint property in styles.xml among the other fixes (the drawable itself).

Background drawable not applying to button

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
.....
/>

how to customize the android spinner?

Hi guys i have looked at various sites on how to customize the spinner but they have been poor and have not helped me to achieve the look i want. So i have tried to use the rules of customizing a button to help. Here is my code
spinner
<Spinner
android:id="#+id/spinner1"
android:layout_height="30dp"
android:layout_width="300dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="0dp"
android:layout_marginTop="210dp"
android:background="#drawable/scannershape"
android:spinnerMode="dropdown"
/>
background of spinner
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#fafafa"
android:centerColor="#ffffff"
android:endColor="#fafafa"
android:angle="360"/>
<padding android:left="6dp"
android:top="6dp"
android:right="6dp"
android:bottom="6dp" />
<corners android:radius="0dp" />
<stroke android:width="2px" android:color="#cfcfcf" />
</shape>
The look is almost perfect but there are a few things missing.
1,First of all with the normal spinner when it is clicked there is a brief moment when the scanner turns bright orange to show you have clicked it. How do i do this in my customization as it no longer does this anymore in my customized version? and if possible but not important change this color to orange.
2, Secondly the normal spinner has a arrow that points downward to indicate a dropdown list. This is also lost in my customization. How do i customize this arrow into my spinner?
finally were is this file located so i can see if the answers to my question might be there?
android:background="#android:drawable/btn_dropdown"
You may use selector. There's a question for buttons, it's the same for spinner, I think

Android button background color

I am trying to set the background color of a button in my app and I am unable to achieve the result that I want...
The color that I am trying to set is holo_green_light(#ff99cc00). In order to do it, I am using setColorFilter(0xff99cc00, PorterDuff.Mode.MULTIPLY);
The color that I get is not the holo_green_light but a mix of lightgrey and holo_green_light.
I have tried using the LightingColorFilter without much success.
Is there a way to do it programatically, so that the button appears like a button and not a flat rectangle with the color that I need.
If you want to keep the general styling (rounded corners etc.) and just change the background color then I use the backgroundTint property
android:backgroundTint="#android:color/holo_green_light"
This is my way to do custom Button with a different color:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="3dp"
android:color="#80FFFFFF" />
<corners android:radius="25dp" />
<gradient android:angle="270"
android:centerColor="#90150517"
android:endColor="#90150517"
android:startColor="#90150517" />
</shape>
This way you set as background:
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginBottom="25dp"
android:layout_centerInParent="true"
android:background="#drawable/button" />
If you don't mind hardcoding it you can do this ~> android:background="#eeeeee" and drop any hex color # you wish.
Looks like this....
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:text="#string/ClickMe"
android:background="#fff"/>
Create /res/drawable/button.xml with the following content :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#90EE90"/>
<corners
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
</shape>
And then you can use the following :
<Button
android:id="#+id/button_save_prefs"
android:text="#string/save"
android:background="#drawable/button"/>
Just use a MaterialButton and the app:backgroundTint attribute:
<MaterialButton
app:backgroundTint="#color/my_color_selector"
Why not just use setBackgroundColor(getResources().getColor(R.color.holo_light_green))?
Edit: If you want to have something which looks more like an Android button you are going to want to create a gradient and set it as the background. For an example of this, you can check out this question.
No need to be that hardcore.
Try this :
YourButtonObject.setBackground(0xff99cc00);
Try this
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="34dp"
android:text="Check Out"
android:textAllCaps="true"
android:background="#54c2bc"
android:textColor="#FFFFFF"
android:textSize="9sp"/>
In order to keep the style, use:
int color = Color.parseColor("#99cc00");
button.getBackground().mutate().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC));
In addition to Mark Proctor's answer:
If you want to keep the default styling, but have a conditional coloring on the button, just set the backgroundTint property like so:
android:backgroundTint="#drawable/styles_mybutton"
Create the associated file /res/drawable/styles_mybutton.xml, then use the following template and change the colors as per your tastes:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Disabled state-->
<item android:state_enabled="false"
android:color="#android:color/white">
</item>
<!-- Default state-->
<item
android:color="#cfc">
</item>
</selector>
try this
app:backgroundTint="#color/colorGreen"
With version 1.2.0-alpha06 of material design library, now we can use android:background="..." on MaterialButton components:
<com.google.android.material.button.MaterialButton
android:background="#fff"
...
/>

Categories

Resources