0% found this document useful (0 votes)
19 views6 pages

Practical No 09

The document discusses programs to implement button, image button, and toggle button in Android. It includes the XML layout and Java code to create each type of button and handle button clicks. For buttons, it shows how to get input, perform calculations, and display output. For image buttons and toggle buttons, it demonstrates how to set on click listeners to display a toast message on click.

Uploaded by

Asad Moinuddin
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)
19 views6 pages

Practical No 09

The document discusses programs to implement button, image button, and toggle button in Android. It includes the XML layout and Java code to create each type of button and handle button clicks. For buttons, it shows how to get input, perform calculations, and display output. For image buttons and toggle buttons, it demonstrates how to set on click listeners to display a toast message on click.

Uploaded by

Asad Moinuddin
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/ 6

Practical No 09

AIM:-Develop a program to implement Button, Image Button and


Toggle Button

1. Program for Button implementation :-


CODE:-

.xml file
<?xml version="1.0" encoding="utf-8"?> android:layout_marginRight="20dp"
<LinearLayout android:text="Addition"
xmlns:android="http://schemas.android.com/apk/re android:background="#FA980B"
s/android" android:textColor="#fff" />
xmlns:app="http://schemas.android.com/apk/res- <Button
auto" android:id="@+id/btnsubs"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="match_parent" android:layout_marginLeft="20dp"
android:orientation="vertical" android:layout_marginTop="20dp"
tools:context=".MainActivity"> android:layout_marginRight="20dp"
android:text="Substraction"
<ImageView android:background="#FA980B"
android:id="@+id/imageView" android:textColor="#fff" />
android:layout_width="match_parent" <Button
android:layout_height="111dp" android:id="@+id/btndiv"
app:srcCompat="@mipmap/ic_launcher" /> android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText android:layout_marginLeft="20dp"
android:id="@+id/txt1" android:layout_marginTop="20dp"
android:layout_width="match_parent" android:layout_marginRight="20dp"
android:layout_height="wrap_content" android:text="Division"
android:layout_marginLeft="20dp" android:background="#FA980B"
android:layout_marginTop="20dp" android:textColor="#fff" />
android:layout_marginRight="20dp" <Button
android:hint="Enter first number" android:id="@+id/btnmult"
android:inputType="numberDecimal" android:layout_width="match_parent"
android:textColor="@color/black" /> android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
<EditText android:layout_marginTop="20dp"
android:id="@+id/txt2" android:layout_marginRight="20dp"
android:layout_width="match_parent" android:text="Multiplication"
android:layout_height="wrap_content" android:background="#FA980B"
android:layout_marginLeft="20dp" android:textColor="#fff" />
android:layout_marginTop="20dp"
android:layout_marginRight="20dp" <TextView
android:hint="Enter second number" android:id="@+id/result"
android:inputType="numberDecimal" android:layout_width="match_parent"
android:textColor="@color/black" /> android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
<Button android:layout_marginTop="20dp"
android:id="@+id/btnadd" android:layout_marginRight="20dp"
android:layout_width="match_parent" android:text="Result" />
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" </LinearLayout>
android:layout_marginTop="20dp"
.java file
package com.example.practical_09a;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button btnadd,btnsubs,btnmult,btndiv;
EditText txt1,txt2;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnadd=findViewById(R.id.btnadd);
btnsubs=findViewById(R.id.btnsubs);
btnmult=findViewById(R.id.btnmult);
btndiv=findViewById(R.id.btndiv);
txt1=findViewById(R.id.txt1);
txt2=findViewById(R.id.txt2);
result=findViewById(R.id.result);

btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float a,b,c;
a=Float.parseFloat(txt1.getText().toString());
b=Float.parseFloat(txt2.getText().toString());
c=a+b;
result.setText("The Addition result is"+c);

}
});

btnsubs.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
float a,b,c;
a=Float.parseFloat(txt1.getText().toString());
b=Float.parseFloat(txt2.getText().toString());
c=a-b;
result.setText("The Substraction result is"+c);

}
});
btndiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
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);

}
});
btnmult.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
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);

}
});

}
}

OUTPUT:-
2. Program for ImageButton implementation :-
CODE:-
.xml file
<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"
android:layout_marginTop="200dp"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/simpleImageButtonHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@color/black"
android:padding="20dp"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>

.java file
package com.example.practical_09b;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton simpleImageButtonHome = (ImageButton)findViewById(R.id.simpleImageButtonHome);
simpleImageButtonHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Play Button",Toast.LENGTH_LONG).show(); }
});
}}
OUTPUT:-
3. Program for ToggleButton implementation :-
CODE:-

.xml file
<ToggleButton
android:id="@+id/simpleToggleButton2"
<LinearLayout
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/re
android:layout_height="wrap_content"
s/android"
android:layout_gravity="center_horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginLeft="50dp"
android:layout_width="match_parent"
android:checked="true"
android:layout_height="match_parent"
android:orientation="vertical"
android:drawableLeft="@mipmap/ic_launcher"
tools:context=".MainActivity">
android:drawablePadding="20dp"
android:textColor="#000" />
<LinearLayout
</LinearLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:layout_gravity="center_horizontal"
android:id="@+id/submitButton"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<ToggleButton
android:layout_gravity="center"
android:id="@+id/simpleToggleButton1"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:background="#0f0"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:text="Submit"
android:checked="false"
android:textColor="#fff"
android:drawablePadding="20dp"
android:textSize="20sp"
android:drawableRight="@mipmap/ic_launcher"
android:textStyle="bold" />
android:textColor="#000" />
</LinearLayout>

.java file
package com.example.practical_09c;

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

public class MainActivity extends AppCompatActivity {

ToggleButton simpleToggleButton1, simpleToggleButton2;


Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate toggle button's
simpleToggleButton1 = (ToggleButton) findViewById(R.id.simpleToggleButton1);
simpleToggleButton2 = (ToggleButton) findViewById(R.id.simpleToggleButton2);
submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String status = "ToggleButton1 : " + simpleToggleButton1.getText() + "\n" + "ToggleButton2 : " +
simpleToggleButton2.getText();
Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show(); // display the
current state of toggle button's
}
});
}
}

OUTPUT:-

You might also like