0

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.

Omar Shawky
  • 1,242
  • 1
  • 12
  • 25

3 Answers3

2

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

sajjadio
  • 29
  • 1
  • 5
  • Yes I found this solution too. But does it have any affects? I think if MaterialComponents is by default added in our app then it may have more and better features than AppCompat.. This is what's keeping me from trying out that solution.. – Shafique Ahmed Sep 19 '21 at 22:59
  • You can change it only for the specific activity and not for the entire app – Shaurya Goyal Sep 20 '21 at 01:18
  • but will that affect some specific activity if I change it to old AppCompat instead of MaterialComponenets. – Shafique Ahmed Sep 20 '21 at 15:56
  • It looks bad, you can use MaterialComponenets and its better features and you can also use btnImag which accepts changing the background but this is not a perfect solution. This is all I have. I hope I helped you even a little – sajjadio Sep 21 '21 at 17:34
0

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:

Shaurya Goyal
  • 169
  • 1
  • 11
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/237328/discussion-on-answer-by-shaurya-goyal-androidbackground-attribute-not-being-app). – Brad Larson Sep 21 '21 at 16:18
-1

use app:backgroundTint to change your button color

Dep1111
  • 93
  • 4