0% found this document useful (0 votes)
15 views3 pages

Cuevas ACT4

The document contains XML and Java code for an Android activity that uses a toggle button. The activity layout contains a toggle button and a button to check the toggle state. The Java code finds the toggle button and check button in the activity, sets an onClick listener for the check button, and displays a Toast message indicating whether the toggle is enabled or disabled when clicked.
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)
15 views3 pages

Cuevas ACT4

The document contains XML and Java code for an Android activity that uses a toggle button. The activity layout contains a toggle button and a button to check the toggle state. The Java code finds the toggle button and check button in the activity, sets an onClick listener for the check button, and displays a Toast message indicating whether the toggle is enabled or disabled when clicked.
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/ 3

Cuevas, Gerland Andrei S.

07/17/2023

CPE408 – CPE43S2 Activity 4

Toggled:

Toggle is clicked again:


XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButtonEvent"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="16dp" />

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:textOff="Click me"
android:textOn="Click me" />

<Button
android:id="@+id/btnCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check"
android:layout_marginTop="16dp" /> <!-- Add some top margin to the
button -->

</LinearLayout>

JAVA CODE:
package com.example.act4;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ToggleButton toggleButton;


private Button btnCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toggleButton = findViewById(R.id.toggleButton);
btnCheck = findViewById(R.id.btnCheck);

btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkToggleState();
}
});
}

private void checkToggleState() {


if (toggleButton.isChecked()) {
showMessageDialog("The Toggle is enabled");
} else {
showMessageDialog("The Toggle is disabled");
}
}

private void showMessageDialog(String message) {


Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy