S
S
Semyon2020-10-26 07:29:01
Android
Semyon, 2020-10-26 07:29:01

Android studio: Why is background not being added to button?

When adding a button to the layout, it has some kind of default style of its own. I then add the background to the button as a selector with the button states, but the buttons don't change.

layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:fontFamily="@font/maven_pro_bold">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="77dp"
        android:background="@drawable/cbutton"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

cbutton (selector)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
        android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

button_default
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <solid android:color="#000000"/>
    <corners android:radius="16sp"/>
    <stroke android:color="#ffffff" android:width="6dp"/>
</shape>

button_pressed
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <corners android:radius="16sp"/>
</shape>

button_focused
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <solid android:color="#8F8F8F"/>
    <corners android:radius="16sp"/>
    <stroke android:color="#ffffff" android:width="6dp"/>
</shape>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Semyon, 2020-10-28
@Hitreno

The problem is solved by poking. In the files in the res/values/themes folder, you need to change the name parameter from materialcomponents to appcompact in the style tag, after everything worked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question