[embedyt] https://www.youtube.com/watch?v=Nn4-Vn7qk9k[/embedyt]
Downloadsactivity_main.xmlcustom_button.xmlMainActivity.java
Button images:
<?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:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_button" /> <Switch android:id="@+id/switch_enable_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Enabled" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <item android:state_enabled="false" android:drawable="@drawable/button_disabled" /> <item android:drawable="@drawable/button_default" /> </selector>
package com.codinginflow.custombuttonexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button customButton = findViewById(R.id.custom_button); Switch switchEnableButton = findViewById(R.id.switch_enable_button); customButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show(); } }); switchEnableButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { customButton.setEnabled(true); } else { customButton.setEnabled(false); } } }); } }