Exemples de code Android

Le composant Switch

Comme la case à cocher (CheckBox) et le bouton bascule (ToggleSwitch), le composant Switch peut être vu comme un simple bouton, mais avec une indication visuelle de son état.

L'exemple suivant montre qu'un switch est par défaut inactif (équivalent de android:checked="false") et que les chaînes de caractères associées aux deux états possibles sont par défaut "ON" et "OFF".

MainActivity.java

package net.codeandroid.exempleswitch;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Switch sw1 = findViewById(R.id.switch1);
        Switch sw2 = findViewById(R.id.switch2);

        Button btnEtat = findViewById(R.id.bouton_etat);
        btnEtat.setOnClickListener(new View.OnClickListener() {
            // affiche des informations sur les switchs
            @Override
            public void onClick(View v) {
                String str1;
                String str2;

                if (sw1.isChecked()) {
                    str1 = sw1.getTextOn().toString();
                } else {
                    str1 = sw1.getTextOff().toString();
                }
                if (sw2.isChecked()) {
                    str2 = sw2.getTextOn().toString();
                } else {
                    str2 = sw2.getTextOff().toString();
                }
                
                Toast.makeText(getApplicationContext(), "Son = " + str1
                        + "\n\n" + "Image = " + str2, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

activity_main.xml

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

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50px"
        android:switchMinWidth="75dp"
        android:textSize="25sp"
        android:text="Son :"
        android:checked="true"
        android:textOff="Éteint"
        android:textOn="Allumé"/>

    <Switch
        android:id="@+id/switch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50px"
        android:switchMinWidth="75dp"
        android:textSize="25sp"
        android:text="Image :"/>

    <Button
        android:id="@+id/bouton_etat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50px"
        android:textSize="15sp"
        android:text="Afficher état" />

</LinearLayout>