0% found this document useful (0 votes)
31 views2 pages

Practical 9

The document describes a program that implements three types of buttons - a regular button, an image button, and a toggle button. It defines the layout using XML with each button type and its properties. It then implements the Java code to set onclick listeners for each button so that a toast message is displayed when each button is clicked with the button type and state.

Uploaded by

swapnil kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Practical 9

The document describes a program that implements three types of buttons - a regular button, an image button, and a toggle button. It defines the layout using XML with each button type and its properties. It then implements the Java code to set onclick listeners for each button so that a toast message is displayed when each button is clicked with the button type and state.

Uploaded by

swapnil kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PRACTICAL 9

PROGRAM TO IMPLEMENT BUTTON, IMAGE BUTTON AND TOGGLE


BUTTON

The layout of the MainActivity is defined as follows. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- Button -->


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>

<!-- Image button -->


<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>

<!-- Toggle button -->


<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton"
android:textOn="ON"
android:textOff="OFF"
android:checked="false"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

The MainActivity.java is defined below.

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

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

// Button
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();
}
});

// Image button
ImageButton imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Image button clicked!",
Toast.LENGTH_SHORT).show();
}
});

// Toggle button
ToggleButton toggleButton = findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = ((ToggleButton) v).isChecked();
if (checked) {
Toast.makeText(getApplicationContext(), "Toggle button ON",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Toggle button OFF",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like