Custom Xml file's functionality is not implementing in activity_main.xml

Viewed 106

I am new in android development and I have created an xml file of name of "round_btn_bg.xml" in which I am just set the styling of my button but when I use this xml file in activity_main.xml the functionality of button is not loading in app...

here it is the code of "round_btn_bg.xml"

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

    <solid android:color="@color/colorAccent"/>
    <corners android:radius="60dp"/>
</shape> 

Below it is the code of "actvity_main.xml"

<?xml version="1.0" encoding="utf-8"?>
<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="@color/colorBlack"
    android:id="@+id/mainActivity_RL"
    tools:context=".MainActivity">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainActivity_BackgroundIV"
    android:src="@drawable/backgroundconnecteach"
    android:scaleType="centerCrop"
    android:alpha=".6"/>

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/mainActivity_logoIV"
        android:src="@drawable/logo"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="160dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mainActivity_TagLineTV"
        android:text="@string/get_started_tag_line"
        android:textColor="@color/colorWhite"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:layout_below="@id/mainActivity_logoIV"/>
    
    <Button
        style="@drawable/round_btn_bg"
        android:id="@+id/mainActivity_MoveBtn"
        android:layout_width="168dp"
        android:layout_height="40dp"
        android:layout_below="@id/mainActivity_TagLineTV"
        android:layout_centerInParent="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/round_btn_bg"
        android:text="@string/get_started_btn" />


</RelativeLayout>
3 Answers

In the Button that you have declared, you cannot set the style to a drawable resource. It has to point to a style defined in styles.xml. You are correct in setting it as the background.

Remove the following line from your Button in XML

style="@drawable/round_btn_bg"

use background property for this.. 1st create your design in drawable file like this

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="#800013"/>
        <corners android:radius="5dp"/>
    </shape>
</item>

<item>
    <shape android:shape="oval">
        <solid android:color="#AA3B18"/>
        <corners android:radius="5dp"/>
    </shape>
</item>
then add this as a background of any widget like button
background="@drawable/round_btn_bg"
<Button
        android:id="@+id/mainActivity_MoveBtn"
        android:layout_width="168dp"
        android:layout_height="40dp"
        android:layout_below="@id/mainActivity_TagLineTV"
        android:layout_centerInParent="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/round_btn_bg"
        android:text="@string/get_started_btn" />

Replace the Button code with this. I think It will work

Related