Set Volume Mode, Vibrate Mode, And Silent Mode To On/Off In Android Studio

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

  • Android Studio version 2.1.3

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 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. Afterwards, 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.   
  7.     <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" />  
  8. </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.   
  9. import android.widget.Switch;  
  10.   
  11. import android.widget.Toast;  
  12. public class MainActivity extends AppCompatActivity {  
  13.     Switch Switch1, Switch2, Switch3;  
  14.     Button submit;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         Switch1 = (Switch) findViewById(R.id.vlm);  
  20.         Switch2 = (Switch) findViewById(R.id.slt);  
  21.         Switch3 = (Switch) findViewById(R.id.vbt);  
  22.         submit = (Button) findViewById(R.id.button);  
  23.         submit.setOnClickListener(new View.OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View view) {  
  26.                 String statusSwitch1, statusSwitch2, statusSwitch3;  
  27.                 if (Switch1.isChecked())  
  28.                     statusSwitch1 = Switch1.getTextOn().toString();  
  29.                 else  
  30.                     statusSwitch1 = Switch1.getTextOff().toString();  
  31.                 if (Switch2.isChecked())  
  32.                     statusSwitch2 = Switch2.getTextOn().toString();  
  33.                 else  
  34.                     statusSwitch2 = Switch2.getTextOff().toString();  
  35.                 if (Switch3.isChecked())  
  36.                     statusSwitch3 = Switch3.getTextOn().toString();  
  37.                 else  
  38.                     statusSwitch3 = Switch2.getTextOff().toString();  
  39.                 Toast.makeText(getApplicationContext(), "Volume :" + statusSwitch1 + "\n" + "Silent :" + statusSwitch2 + "\n" + "Vibrate :" + statusSwitch3, Toast.LENGTH_LONG).show(); // display the current state for switch's   
  40.             }  
  41.         });  
  42.     }  
  43.     @Override  
  44.     public boolean onOptionsItemSelected(MenuItem item) {  
  45.         int id = item.getItemId();  
  46.         if (id == R.id.action_bar) {  
  47.             return true;  
  48.         }  
  49.         return super.onOptionsItemSelected(item);  
  50.     }  
  51. }  
Step 7

Here, go to Run >> Run 'app' option.

Android

Here, choose the Emulator or the devices, like 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

 

Up Next
    Ebook Download
    View all
    Learn
    View all