<?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="match_parent"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/simpleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Toggle ON"
android:textOff="Toggle OFF" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"
android:contentDescription="Camera Button"
android:background="?android:attr/selectableItemBackgroundBorderless"/>
</LinearLayout>
Java file
package com.example.buttondemo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button simpleButton;
ToggleButton toggleButton;
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleButton = findViewById(R.id.simpleButton);
toggleButton = findViewById(R.id.toggleButton);
imageButton = findViewById(R.id.imageButton);
simpleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Simple Button Clicked",
Toast.LENGTH_SHORT).show();
});
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
Toast.makeText(MainActivity.this, "Toggle is ON", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Toggle is OFF", Toast.LENGTH_SHORT).show();
});
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Image Button Clicked",
Toast.LENGTH_SHORT).show();
}
});