AppCompatButton

Buttons in Android are used to communicate our actions with the application.
Button is a subclass of TextView class and compound button is the subclass of Button class. On a button we can perform different actions or events like click event, pressed event, touch event etc.

Class hierarchy
public class AppCompatButton extends Button implements TintableBackgroundView, TintableCompoundDrawablesView
java.lang.Object
↳ android.view.View
  ↳ android.widget.TextView
    ↳ android.widget.Button
      ↳ androidx.appcompat.widget.AppCompatButton

We Recommend to use new UI components i.e. androidx components


Create AppCompatButton in layout xml
<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btnDemo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Show Toast" />


Access into Activity
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

public class ButtonDemoActivity  extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.button_demo_activity);

        AppCompatButton btnShowToast = findViewById(R.id.btnDemo);
        btnShowToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ButtonDemoActivity.this, "You just clicked on button.", Toast.LENGTH_SHORT).show();
            }
        });
    }
}


Create AppCompatButton in programatically
AppCompatButton btnShowToast = new AppCompatButton(this);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnShowToast.setLayoutParams(params);

btnShowToast.setText("Show Toast");
btnShowToast.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(ButtonDemoActivity.this, "You just clicked on button.", Toast.LENGTH_SHORT).show();
    }
});