Professional Documents
Culture Documents
MAD Exp 9
MAD Exp 9
Practical No 9:
Q. Write a Program to create 2 toggle buttons and on the click of the button it should display which
button is pressed.
CODE:
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"
android:padding="10dp"
tools:context=".MainActivity">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/submitBtn"
android:layout_marginRight="90dp"
android:layout_marginLeft="60dp"
android:id="@+id/toggleButton1"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/toggleButton1"
android:layout_above="@id/submitBtn"
android:id="@+id/toggleButton2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:layout_marginTop="10dp"
android:id="@+id/submitBtn"
android:layout_centerInParent="true"
Oaish Qazi 2|Page
/>
</RelativeLayout>
JAVA:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
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 submitBtn = findViewById(R.id.submitBtn);
ToggleButton toggleButton1 = findViewById(R.id.toggleButton1);
ToggleButton toggleButton2 = findViewById(R.id.toggleButton2);
submitBtn.setOnClickListener(view -> {
String result = "";
result += "TB 1 State: " + toggleButton1.getText().toString();
result += "| TB 2 State: " + toggleButton2.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
});
}
}
OUTPUT:
Q. Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.
CODE:
Oaish Qazi 3|Page
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:textSize="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="100dp"
android:id="@+id/toggleButton"/>
</LinearLayout>
JAVA:
package com.example.myapplication;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
OUTPUT:
Oaish Qazi 4|Page
Q. Write a Program to create a 1 rounded corner image button and 1 normal image button and on the
click of the button it should display which button is pressed.
CODE:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sq_btn"
android:onClick="onImageButtonClicked"
android:id="@+id/sqBtn"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rdBtn"
android:onClick="onImageButtonClicked"
android:background="@drawable/image_rounded_corner"
android:backgroundTint="#A11FD8"
android:src="@drawable/rd_btn"/>
</LinearLayout>
Oaish Qazi 5|Page
JAVA:
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onImageButtonClicked(View v) {
if (v.getId() == R.id.sqBtn) {
Toast.makeText(getApplicationContext(), "Normal image button pressed", Toast.LENGTH_SHORT).show();
} else if (v.getId() == R.id.rdBtn) {
Toast.makeText(getApplicationContext(), "Rounded image button pressed", Toast.LENGTH_SHORT).show();
}
}
}
OUTPUT: