Warm tip: This article is reproduced from serverfault.com, please click

Custom background resource not working in android

发布于 2020-11-29 03:52:00

I know it is a small thing but I don't know why it is not working.

when I am putting custom drawable image it is not working. I want to have a transparent background button with corners (stroke).

below is the image.

enter image description here

<RelativeLayout 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="@drawable/welcome"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/app_logo"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:src="@drawable/applogo"/>

    <TextView
        android:id="@+id/app_slogan"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="80dp"
        android:layout_marginTop="140dp"
        android:text="Find, Discover, Choose and buy anything online"
        android:textAlignment="center"
        android:textSize="22sp"
        android:textStyle="bold|italic"
        android:textColor="@color/design_default_color_primary"

        />

        <Button
            android:id="@+id/main_login_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="10dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:background="@drawable/buttons"
            android:padding="20dp"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:text="Already have a account ? Login"
            android:textColor="@android:color/white"

            />

    <Button
        android:id="@+id/main_join_now_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_above="@id/main_login_btn"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:background="@drawable/input_design"
        android:padding="20dp"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:text="Already have a account ? Login"
        android:textColor="@android:color/white"
        />


</RelativeLayout>

// Drawable resource file.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:color="#fff"
        android:width="3dp"
        />

    <corners
        android:radius="15dp"
        />
    

login button should have a background of input_design.xml but I don't know why it is not working. help me out thank you in advance.

Questioner
Dhananjay pathak
Viewed
0
David Kroukamp 2020-11-29 16:03:02

To achieve a transparent background on your button perhaps use Widget.MaterialComponents.Button.OutlinedButton.

You will need this line in your build.gradle (if not there already):

implementation 'com.google.android.material:material:1.2.1'

Your button would then simply look like this:

<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="@color/teal_700"
    tools:context=".FirstFragment">

    <Button
        android:id="@+id/button"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:text="I am a Button!"
        android:textColor="@color/white"
        app:cornerRadius="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rippleColor="@color/white"
        app:strokeColor="@color/white"
        app:strokeWidth="3dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Notice:

  • style="@style/Widget.MaterialComponents.Button.OutlinedButton"
  • android:textColor="@color/white"
  • app:cornerRadius="15dp"
  • app:rippleColor="@color/white"
  • app:strokeColor="@color/white"
  • app:strokeWidth="3dp"

and that will produce:

enter image description here