Introduction

In this article, you will learn how to develop or set volume mode, vibrate mode, and silent mode to ON/OFF in Android applications, using Android Studio.
Requirements
If you want to create volume, vibrate, and silent mode in switch control, follow the below steps.
Step 1
Open Android Studio and choose File >> New >> NewProject.
Android
Step 2
Here, enter your application name and select the location where your project should be stored. Then, click the NEXT button.
Android
Now, select the minimum version of Android for which you want to target the Android Devices.
Android
Step 3
On the next screen, add the activity of your choice, Empty in our case, and click the Next button.
Android
Now, we write the activity name and click Finish.
Android
Step 4
Now, open your project and go to activity_main.xml. Afterward, build the design with the help of drag and drop method, like dragging the Switch and Button tools .
Android
Now, we can see the Graphical User Interface design.
Android
Step 5
Now, go to the source panel and write the XML code.
Activity_mai.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.switchonoffapp.MainActivity">
  3. <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Volume" android:id="@+id/vlm" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />
  4. <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Silent" android:id="@+id/slt" android:layout_below="@+id/vlm" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />
  5. <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Vibrate" android:id="@+id/vbt" android:layout_below="@+id/slt" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />
  6. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" android:id="@+id/button" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
  7. </RelativeLayout>
Step 6
Now, go to MainActivity.java page and build the Java code.
First, you need to declare an extension file.
Android
Below, we can see the MainActivity.java code.
  1. package xyz.rvconstructions.www.switchonoffapp;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Switch;
  9. import android.widget.Toast;
  10. public class MainActivity extends AppCompatActivity {
  11. Switch Switch1, Switch2, Switch3;
  12. Button submit;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. Switch1 = (Switch) findViewById(R.id.vlm);
  18. Switch2 = (Switch) findViewById(R.id.slt);
  19. Switch3 = (Switch) findViewById(R.id.vbt);
  20. submit = (Button) findViewById(R.id.button);
  21. submit.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View view) {
  24. String statusSwitch1, statusSwitch2, statusSwitch3;
  25. if (Switch1.isChecked())
  26. statusSwitch1 = Switch1.getTextOn().toString();
  27. else
  28. statusSwitch1 = Switch1.getTextOff().toString();
  29. if (Switch2.isChecked())
  30. statusSwitch2 = Switch2.getTextOn().toString();
  31. else
  32. statusSwitch2 = Switch2.getTextOff().toString();
  33. if (Switch3.isChecked())
  34. statusSwitch3 = Switch3.getTextOn().toString();
  35. else
  36. statusSwitch3 = Switch2.getTextOff().toString();
  37. Toast.makeText(getApplicationContext(), "Volume :" + statusSwitch1 + "\n" + "Silent :" + statusSwitch2 + "\n" + "Vibrate :" + statusSwitch3, Toast.LENGTH_LONG).show(); // display the current state for switch's
  38. }
  39. });
  40. }
  41. @Override
  42. public boolean onOptionsItemSelected(MenuItem item) {
  43. int id = item.getItemId();
  44. if (id == R.id.action_bar) {
  45. return true;
  46. }
  47. return super.onOptionsItemSelected(item);
  48. }
  49. }
Step 7
Here, go to Run >> Run 'app' option.
Android
Here, choose the Emulator or the devices, like the Nokia Nokia _X.
Android
Step 8
Finally, you can see the output, as shown below.
Android
Now, set the device on the Volume mode and click submit. The Volume mode is ON.
Android
Similarly, activate the Silent mode.
Android
And, the Vibrate mode.
Android