Shafique Ahmed
Shafique Ahmed

Reputation: 39

android:background attribute not being applied to buttons

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
Here's the result that I get

This is what I want to achieve.
This is what I want to achieve.

I'm really looking forward to your answers! Thanks.

Upvotes: 0

Views: 1410

Answers (6)

Ixtiyor Yarashov
Ixtiyor Yarashov

Reputation: 1

Just use this code, it worked for me:

app:backgroundTint="@null"
android:background="@drawable/bg_your_button"

Upvotes: 0

Devesh Kumawat
Devesh Kumawat

Reputation: 218

Use <com.google.android.material.button.MaterialButton  button for rounded button with border. and also use

app:strokeColor="@color/color_E0E6F9"
app:strokeWidth="0.5dp"
app:cornerRadius="8dp"


these properties for round corner and border of button.

Upvotes: 0

ayman omara
ayman omara

Reputation: 344

in your style.xml you could use

    <style name="RoundedBtn"  parent="@style/Widget.MaterialComponents.Button">
        <item name="android:textColor">@color/gray</item>
        <item name="strokeColor">@color/light_purple</item>
        <item name="strokeWidth">3dp</item>
        <item name="cornerRadius">20dp</item>
        <item name="android:backgroundTint">@android:color/transparent</item>
        
    </style>

here is how to use

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        style="@style/RoundedBtn"
        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" />

Upvotes: 0

Shaurya Goyal
Shaurya Goyal

Reputation: 181

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:

  1. 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:

Upvotes: 0

sajjadio
sajjadio

Reputation: 29

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

Upvotes: 2

Dep1111
Dep1111

Reputation: 101

use app:backgroundTint to change your button color

Upvotes: -2

Related Questions