'Android: Class representing custom Button not inflating Layout properly?

I have a custom class CustomButton that extends Button, which upon initialization, inflates a layout custom_button.xml. In MainActivity.java, a CustomButton is created and added to the layout to be displayed. However, when I run my application, I get the following:

enter image description here

Which does not match the style that I defined for custom_button.xml, which is supposed to render the following:

enter image description here

Below is my code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#ffffff"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tile_list"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

custom_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    android:padding="15dp">

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:gravity="left|center_vertical"
        android:background="#ff0000"
        android:text="Button" />

</LinearLayout>

CustomButton.java:

package com.example.stackoverflowquestion;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;

import androidx.appcompat.widget.AppCompatButton;

public class CustomButton extends AppCompatButton {
    public CustomButton(Context context) {
        super(context);
        initUI();
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI();
    }

    private void initUI() {
        inflate(getContext(), R.layout.custom_button, (ViewGroup) this.getParent());
    }
}

MainActivity.java:

package com.example.stackoverflowquestion;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomButton tile1 = new CustomButton(this);
        LinearLayout tileListLayout = findViewById(R.id.tile_list);
        tileListLayout.addView(tile1);
    }
}

build.gradle (app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 31
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.stackoverflowquestion"
        minSdkVersion 26
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source