0% found this document useful (0 votes)
19 views

Practical 9 - Develop A Program To

The document describes how to implement a button, image button, and toggle button in Android. It includes the XML layout code to add the buttons to an activity and the Java code to handle clicks on each button type and check the state of the toggle button.

Uploaded by

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

Practical 9 - Develop A Program To

The document describes how to implement a button, image button, and toggle button in Android. It includes the XML layout code to add the buttons to an activity and the Java code to handle clicks on each button type and check the state of the toggle button.

Uploaded by

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

Practical 9 - Develop a program to implement button, image button and toggle button

___________________________________________________________________________________
______

Step 1 - activity_main.xml

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


<RelativeLayout 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"
tools:context=".MainActivity">

<!-- Button -->


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClick"/>

<!-- ImageButton -->


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

<!-- ToggleButton -->


<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle Button"
android:layout_below="@id/imageButton"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"
android:onClick="onToggleButtonClick"/>

</RelativeLayout>

___________________________________________________________________________________
___

Step 2 - MainActicity.java

package com.prathamesh.practical9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
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);
}
// Method to handle Button click
public void onButtonClick(View view) {
Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
}

// Method to handle ImageButton click


public void onImageButtonClick(View view) {
Toast.makeText(this, "ImageButton Clicked", Toast.LENGTH_SHORT).show();
}

// Method to handle ToggleButton click


public void onToggleButtonClick(View view) {
boolean checked = ((ToggleButton) view).isChecked();
if (checked) {
Toast.makeText(this, "ToggleButton is ON",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "ToggleButton is OFF",
Toast.LENGTH_SHORT).show();
}

}
}

You might also like