Question: I have problems in Android Studio, I don't know how to import the switch ID generically, so I can call this function from all the switches.
I tried to put a description of the switch with the number of it so I can get a number like "1".

```
public class MainActivity extends AppCompatActivity {
Switch switch1, switch2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switch1 = findViewById(R.id.switch1);
    switch2 = findViewById(R.id.switch2);
}

public void OnClick(View view) {
    String stateSwitch = "";
    if(switch1.isChecked() == false){
        stateSwitch = "0";
    } else {
        stateSwitch = "1";
    }
    if(switch2.isChecked() == false){
        stateSwitch = "0";
    } else {
        stateSwitch = "1";
    }
    String idSwitch = switch1.getContentDescription().toString();

    // What can I put instead of "switch1" or "switch2" to select the switch generically?

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute("switch", stateSwitch, idSwitch);
}

```

}

XML of the switches

```
<Switch
    android:id="@+id/switch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="2"
    android:onClick="OnClick"
    android:text="switch2" />

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="1"
    android:onClick="OnClick"
    android:text="switch1" />

```

Sorry for my english.

Answer: 
To get an ID of widget in a code simply get `view.getId()` it will return a positive integer used to identify the view, 
 to get id generically you can call

```
String mId = Integer.toString(view.getId());

```

when you click on `switch1` it will return the `id` of `switch1` similarly when you click on `switch2` it will return the `id` of `switch2`.
Use checked changed listener and get the id there. For example:

```
 <Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="152dp"
    android:text="Switch1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Switch
    android:id="@+id/switch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Switch2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/switch1" />

//////////Java Code///////////

public class MainActivity extends AppCompatActivity {
Switch gSwitch,switch1, switch2;
boolean state;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switch1 = findViewById(R.id.switch1);
    switch1 = findViewById(R.id.switch2);
    switch1.setOnCheckedChangeListener(checkedChangeListener);
    switch2.setOnCheckedChangeListener(checkedChangeListener);
}

CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       id = buttonView.getId();
        gSwitch = findViewById(id);// generic switch;
        state = isChecked;

        // call your method here for example:
       // BackgroundWorker backgroundWorker = new BackgroundWorker(this);
       // backgroundWorker.execute("switch", state, id);
    }
};

```

}