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

Toggle Button Interaction Example

The document discusses creating toggle buttons and image buttons in Android Studio and displaying which button is pressed when clicked. It includes XML code to design the user interface and Java code for the button interactions and toast messages.

Uploaded by

Oaish Qazi
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)
139 views5 pages

Toggle Button Interaction Example

The document discusses creating toggle buttons and image buttons in Android Studio and displaying which button is pressed when clicked. It includes XML code to design the user interface and Java code for the button interactions and toast messages.

Uploaded by

Oaish Qazi
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

Oaish Qazi 1|Page

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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button submitBtn = findViewById([Link]);
ToggleButton toggleButton1 = findViewById([Link].toggleButton1);
ToggleButton toggleButton2 = findViewById([Link].toggleButton2);
[Link](view -> {
String result = "";
result += "TB 1 State: " + [Link]().toString();
result += "| TB 2 State: " + [Link]().toString();
[Link](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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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 [Link];

import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
ToggleButton toggleButton = (ToggleButton) findViewById([Link]);
[Link](view -> [Link](getApplicationContext(), "Bluetooth turned " +
[Link]().toString().toLowerCase(), Toast.LENGTH_SHORT).show());
}
}

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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
public void onImageButtonClicked(View v) {
if ([Link]() == [Link]) {
[Link](getApplicationContext(), "Normal image button pressed", Toast.LENGTH_SHORT).show();
} else if ([Link]() == [Link]) {
[Link](getApplicationContext(), "Rounded image button pressed", Toast.LENGTH_SHORT).show();
}
}
}

OUTPUT:

You might also like