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

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.

Related

Unable to apply background style on button in Android

I am trying to apply background style on button for that I created a drawable resource file but it's unable to take its effect. It's only showing white button only.
Below is my code:
log_out.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/accent"/>
<corners
android:radius="10dp"/>
</shape>
activity_main.xml
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="LOGOUT"
android:background="#drawable/log_out"/>
How can I achieve the desired layout?
It is due to the application theme. This structure will not work if you are using a Material Design theme.
If you change the main theme with parent = "Theme.AppCompat.DayNight" as an example, it will work.

Button Doesn't Show Correct Color - Android Studio

In my Android Studio application, I have a button for which I set a drawable as the background. The drawable rounds the corners and changes the color of the button.
However, when I look at the resulting design of the activity, the color I want doesn't show up. Instead, the button assumes colorPrimary (orange, #ED3616). The same occurs when I run the application.
Note: the other attributes of the drawable file translate perfectly to the button (i.e. the rounded corners). The problem is only with the color.
I have tried using a MaterialButton, and setting the color with android:backgroundTint. Then, the color does show up as I want to, but the design looks odd (there is a different colored rectangle inside of the button(Picture of MaterialButton if you'd like to see it)
How can I make the regular button have the color I desire?
Button:
<Button
android:background="#drawable/round2"
android:id="#+id/signIn"
android:layout_width="256dp"
android:layout_height="66dp"
android:text="#string/logIn"
android:textColor="#FFFFFF"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.878" />
Drawable: round2.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#B30C44"
android:paddingLeft="6dp"
android:paddingTop="6dp" />
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
</shape>
What shows up:
What the Button looks like
It happens because the default style of the MaterialButton (the Button is replaced with a MaterialButton automatically if your are using a MaterialComponents theme) tints the background with the colorPrimary.
You have to use:
<Button
android:background="#drawable/round2"
app:backgroundTint="#null"
... />

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

Android shape button stripped

I want to build a button with 3 different color in the background like the attached image, and I have no idea how to do that with shapes
Thank you for you help.
Use ImageButton and use above image as background of that button:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/YOUR_BTN_IMG"/>
1st make a xml file in drawable folder with bg1 name and put this code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="#4CAF50"
android:endColor="#2E7D32"
android:startColor="#b5e7b7"
android:type="linear" />
</shape>
than where ever you want it use like
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bg1"/>
here u change angle,start end and center color as per your requirement

Categories

Resources