0% found this document useful (0 votes)
13 views5 pages

Practical No. 9A

The document describes developing a program to implement button, image button, and toggle button controls in Android. It includes XML layout code to create the buttons and Java code to set onclick listeners and change images when buttons are clicked or toggle is changed.

Uploaded by

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

Practical No. 9A

The document describes developing a program to implement button, image button, and toggle button controls in Android. It includes XML layout code to create the buttons and Java code to set onclick listeners and change images when buttons are clicked or toggle is changed.

Uploaded by

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

Practical no.

-09
Title: Develop a program to implement Button,Image Button and Toggle Button.
Roll No.:15 Batch- A Date of Performance: 08/02/2024
<ToggleButton

1.
activity_main.xml android:id="@+id/tbtn"
<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content"
<AbsoluteLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/ android:layout_x="91dp"
apk/res/android" android:layout_y="625dp">
xmlns:app="http://schemas.android.com/apk/ </ToggleButton>
res-auto" </AbsoluteLayout>
xmlns:tools="http://schemas.android.com/to
ols"
android:layout_width="match_parent"
MainActivity.java
package com.example.eventhandling;
android:layout_height="match_parent"
import
tools:context=".MainActivity">
androidx.appcompat.app.AppCompatActivit
<Button
y;
android:id="@+id/btn1"
import android.annotation.SuppressLint;
android:layout_width="106dp"
import android.os.Bundle;
android:layout_height="wrap_content"
import android.view.*;
android:layout_x="6dp"
import android.widget.*;
android:layout_y="16dp"
public class MainActivity extends
android:text="Mickey
AppCompatActivity implements
Mouse"></Button>
View.OnClickListener{
<Button
Button b1,b2,b3;
android:id="@+id/btn2"
ImageButton im1,im2;
android:layout_width="106dp"
ToggleButton t1;
android:layout_height="wrap_content"
@SuppressLint("MissingInflatedId")
android:layout_x="121dp"
@Override
android:layout_y="17dp"
protected void onCreate(Bundle
android:text="Chota
savedInstanceState) {
Bheem"></Button>
super.onCreate(savedInstanceState);
<Button
setContentView(R.layout.activity_main);
android:id="@+id/btn3"
b1=(Button) findViewById(R.id.btn1);
android:layout_width="106dp"
b1.setOnClickListener(this);
android:layout_height="wrap_content"
b2=(Button) findViewById(R.id.btn2);
android:layout_x="236dp"
b2.setOnClickListener(this);
android:layout_y="18dp"
b3=(Button) findViewById(R.id.btn3);
android:text="Tom and
b3.setOnClickListener(this);
Jerry"></Button>
t1=(ToggleButton)
<ImageButton
findViewById(R.id.tbtn);
android:id="@+id/img1"
t1.setOnClickListener(this);
android:layout_width="313dp"
im1=(ImageButton)
android:layout_height="249dp"
findViewById(R.id.img1);
android:layout_x="14dp"
im2=(ImageButton)
findViewById(R.id.img2);
android:layout_y="82dp"></ImageButton>
}
<ImageButton
@Override
android:id="@+id/img2"
public void onClick(View v) {
android:layout_width="268dp"
int checkbtn = v.getId();
android:layout_height="269dp"
if (checkbtn == R.id.btn1)
android:layout_x="19dp"
{
android:layout_y="344dp"></ImageButton>
im1.setImageResource(R.drawable.mickey);
}
else if (checkbtn == R.id.btn2)
{

im1.setImageResource(R.drawable.bheem);
}
else
{

im1.setImageResource(R.drawable.tomjerry)
;
}
if(t1.isChecked())
{

im2.setImageResource(R.drawable.on);
}
else
{

im2.setImageResource(R.drawable.off);
}
}
}

2.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content"
<AbsoluteLayout
xmlns:android="http://schemas.android.co android:layout_height="wrap_content"
m/apk/res/android"
android:layout_gravity="center_horizontal
xmlns:app="http://schemas.android.com/a "
pk/res-auto" android:layout_x="76dp"
android:layout_y="28dp"
xmlns:tools="http://schemas.android.com/ android:text="Calculator"
tools" android:textColor="#F48D3E"
android:layout_width="match_parent" android:textSize="50dp"
android:layout_height="match_parent" android:textStyle="bold|italic" />
tools:context=".MainActivity">
<EditText
<TextView android:id="@+id/txt1"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content" android:layout_marginTop="20dp"
android:layout_marginLeft="20dp" android:layout_marginRight="20dp"
android:layout_marginTop="20dp" android:layout_x="4dp"
android:layout_marginRight="20dp" android:layout_y="402dp"
android:layout_x="1dp" android:backgroundTint="#EFC547"
android:layout_y="124dp" android:text="Subtract"
android:ems="10" android:textSize="20sp" />
android:hint="First Number"
<Button
android:inputType="numberDecimal" android:id="@+id/btndiv"

tools:ignore="TouchTargetSizeCheck,Spe android:layout_width="match_parent"
akableTextPresentCheck" />
android:layout_height="wrap_content"
<EditText android:layout_marginLeft="20dp"
android:id="@+id/txt2" android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent" android:layout_x="-1dp"
android:layout_y="531dp"
android:layout_height="wrap_content" android:backgroundTint="#75DA59"
android:layout_marginLeft="20dp" android:text="Divide"
android:layout_marginTop="20dp" android:textSize="20sp" />
android:layout_marginRight="20dp"
android:layout_x="-2dp" <Button
android:layout_y="185dp" android:id="@+id/btnmult"
android:ems="10"
android:layout_width="match_parent"

android:hint="Second Number" android:layout_height="wrap_content"


android:inputType="numberDecimal" android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
tools:ignore="SpeakableTextPresentCheck android:layout_marginRight="20dp"
,TouchTargetSizeCheck" /> android:layout_x="4dp"
android:layout_y="466dp"
<Button android:backgroundTint="#EF8F16"
android:id="@+id/btnadd" android:text="Multiply"
android:layout_width="match_parent" android:textSize="20sp" />

android:layout_height="wrap_content" <TextView
android:layout_marginLeft="20dp" android:id="@+id/result"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp" android:layout_width="match_parent"
android:layout_x="1dp" android:layout_height="37dp"
android:layout_y="342dp" android:layout_marginLeft="20dp"
android:backgroundTint="#EA4444" android:layout_marginTop="25dp"
android:text="Add" android:layout_marginRight="20dp"
android:textSize="20sp" /> android:layout_x="1dp"
android:layout_y="261dp"
<Button android:hint="Result="
android:id="@+id/btnsubs" android:textSize="20sp"/>
</AbsoluteLayout>
android:layout_width="match_parent"
MainActivity.java
package com.example.calci; Toast.makeText(MainActivity.this,
"Please Enter Number",
import Toast.LENGTH_SHORT).show();
androidx.appcompat.app.AppCompatActi }
vity; else {
float a, b, c;
import android.os.Bundle; a=
import android.view.View; Float.parseFloat(txt1.getText().toString());
import android.widget.Button; b=
import android.widget.EditText; Float.parseFloat(txt2.getText().toString());
import android.widget.TextView; c = a + b;
import android.widget.Toast; result.setText("The Addition
Result Is " + c);
import
androidx.appcompat.app.AppCompatActi }
vity;
}
public class MainActivity extends });
AppCompatActivity {
Button btnadd,btnsubs,btnmult,btndiv; btnsubs.setOnClickListener(new
EditText txt1,txt2; View.OnClickListener() {
TextView result; @Override
@Override public void onClick(View view) {
protected void onCreate(Bundle if
savedInstanceState) { (txt1.getText().toString().equals("")) {
super.onCreate(savedInstanceState);
Toast.makeText(MainActivity.this,
setContentView(R.layout.activity_main); "Please Enter Number",
Toast.LENGTH_SHORT).show();
btnadd=findViewById(R.id.btnadd); } else if
(txt2.getText().toString().equals("")) {
btnsubs=findViewById(R.id.btnsubs);
btndiv=findViewById(R.id.btndiv); Toast.makeText(MainActivity.this,
"Please Enter Number",
btnmult=findViewById(R.id.btnmult); Toast.LENGTH_SHORT).show();
}
txt1=findViewById(R.id.txt1);
txt2=findViewById(R.id.txt2); else {
float a, b, c;
result=findViewById(R.id.result); a=
Float.parseFloat(txt1.getText().toString());
btnadd.setOnClickListener(new b=
View.OnClickListener() { Float.parseFloat(txt2.getText().toString());
@Override c = a - b;
public void onClick(View view) { result.setText("The
if Subtraction Result Is " + c);
(txt1.getText().toString().equals("")) {
}
Toast.makeText(MainActivity.this, }
"Please Enter Number", });
Toast.LENGTH_SHORT).show(); btnmult.setOnClickListener(new
} else if View.OnClickListener() {
(txt2.getText().toString().equals("")) { @Override
public void onClick(View view) {
if }
(txt1.getText().toString().equals("")) { });
}
Toast.makeText(MainActivity.this, }
"Please Enter Number",
Toast.LENGTH_SHORT).show();
} else if
(txt2.getText().toString().equals("")) {

Toast.makeText(MainActivity.this,
"Please Enter Number",
Toast.LENGTH_SHORT).show();
}

else {
float a, b, c;
a=
Float.parseFloat(txt1.getText().toString());
b=
Float.parseFloat(txt2.getText().toString());
c = a*b;
result.setText("The
Multiplication Result Is " + c);

}
}
});
btndiv.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
if
(txt1.getText().toString().equals("")) {

Toast.makeText(MainActivity.this,
"Please Enter Number",
Toast.LENGTH_SHORT).show();
} else if
(txt2.getText().toString().equals("")) {

Toast.makeText(MainActivity.this,
"Please Enter Number",
Toast.LENGTH_SHORT).show();
}
else {
float a, b, c;
a=
Float.parseFloat(txt1.getText().toString());
b=
Float.parseFloat(txt2.getText().toString());
c = a/b;
result.setText("The Division
Result Is " + c);

You might also like