Translate To Your Own Langauge

Change the Profile Mode Programmatically (Android Example)

Many of the times when we are dealing with Android app development, we need to change the profile mode of our phone as per user's requirement or if you are working with location based app then you need to change the mode according to the location. In this tutorial, i will show you how to change the profile mode of our phone Programmatically

  Follow these simple steps:
1. In the Activity_main.xml file, drag and drop three buttons. If you want to setup an easy UI, just copy and paste the following code into your XML file.

Activity_main.xml
<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=".MainActivity" >

    <Button
        android:id="@+id/btnSilent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="Silent" />

    <Button
        android:id="@+id/btnVibe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnSilent"
        android:layout_below="@+id/btnSilent"
        android:layout_marginTop="61dp"
        android:text="Viberate" />

    <Button
        android:id="@+id/btnRinger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnVibe"
        android:layout_below="@+id/btnVibe"
        android:layout_marginTop="82dp"
        android:text="Ringer" />

</RelativeLayout>



2. Now you are done with UI, Lets work on the programming part. To do so, copy and paste the following code into mainActivity.java file.

mainActivity.java
package com.example.modeswitcher;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
       Button btnSilent, btnVibe, btnRinger;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              final AudioManager mode=(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
              // Initialization of buttons
              btnSilent = (Button) findViewById(R.id.btnSilent);
              btnVibe = (Button) findViewById(R.id.btnVibe);
              btnRinger = (Button) findViewById(R.id.btnRinger);
              // functionallity of btnSilent
              btnSilent.setOnClickListener(new View.OnClickListener() {

                     @Override
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                           mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                           Toast.makeText(getApplicationContext(), "silent mode set", Toast.LENGTH_LONG).show();
                     }
              });

              // functionallity of btnVibe
              btnVibe.setOnClickListener(new View.OnClickListener() {

                     @Override
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                           mode.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                           Toast.makeText(getApplicationContext(), "Viberate mode set", Toast.LENGTH_LONG).show();
                     }
              });

              // functionallity of btnRinger
              btnRinger.setOnClickListener(new View.OnClickListener() {

                     @Override
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                           mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                           Toast.makeText(getApplicationContext(), "Ringer mode set", Toast.LENGTH_LONG).show();
                     }
              });

       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}

12 Responses to “Change the Profile Mode Programmatically (Android Example)”

CryptoWarrior101 said...
1 October 2023 at 00:38

Eskişehir
Adana
Sivas
Kayseri
Samsun
5DOİPG


Alpike3344 said...
4 October 2023 at 17:56

bitlis
urfa
mardin
tokat
çorum
V8YCGE


BinarySpecter007 said...
6 October 2023 at 23:10

van
elazığ
zonguldak
uşak
sakarya
VRY7U


Keramet4 said...
8 October 2023 at 22:19

whatsapp görüntülü show
ücretli.show
SA8C7


https://welcome-web.blogspot.com/2010/11/currency-converter.html said...
19 October 2023 at 12:58

ankara parça eşya taşıma
takipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
XKCİNH


ElectricWandererYOLG said...
21 October 2023 at 16:31

Çorlu Lojistik
Manisa Lojistik
Eskişehir Lojistik
Afyon Lojistik
Konya Lojistik
KB3


35640Maeve13251 said...
9 November 2023 at 17:50

E17E8
winstrol stanozolol for sale
buy deca durabolin
buy dianabol methandienone
fat burner for sale
order deca durabolin
buy sustanon
order testosterone propionat
buy trenbolone enanthate
buy turinabol


E61CCJennifer719F8 said...
11 November 2023 at 18:14

F7900
Karapürçek Fayans Ustası
Çanakkale Şehirler Arası Nakliyat
Bartın Lojistik
Poloniex Güvenilir mi
Etimesgut Fayans Ustası
Samsun Lojistik
Iğdır Şehir İçi Nakliyat
Ardahan Lojistik
Antalya Şehir İçi Nakliyat


F4098Timothy44780 said...
11 November 2023 at 21:09

EF70F
Giresun Şehir İçi Nakliyat
Hatay Lojistik
Bartın Şehir İçi Nakliyat
Edirne Lojistik
Ardahan Şehirler Arası Nakliyat
Ünye Kurtarıcı
Maraş Evden Eve Nakliyat
Bitget Güvenilir mi
Karabük Lojistik


E1EE7Alessia3C572 said...
5 January 2024 at 22:22

BCA0E
Görüntülü Sohbet
Mexc Borsası Kimin
Görüntülü Sohbet Parasız
Binance Referans Kodu
Hexa Coin Hangi Borsada
Binance Hesap Açma
Binance Madenciliği Nedir
Mexc Borsası Güvenilir mi
Clubhouse Takipçi Hilesi


5C472Lance92513 said...
8 January 2024 at 19:35

84EFF
Binance Referans Kodu
Parasız Görüntülü Sohbet
Likee App Beğeni Hilesi
Okex Borsası Güvenilir mi
Satoshi Coin Hangi Borsada
Paribu Borsası Güvenilir mi
Bitcoin Madenciliği Siteleri
Bitcoin Mining Nasıl Yapılır
Facebook Grup Üyesi Hilesi


D68CEJorgeC738B said...
18 January 2024 at 12:56

A1B31
Binance Referans Kodu
Clysterum Coin Hangi Borsada
Bitcoin Nasıl Alınır
MEME Coin Hangi Borsada
Youtube İzlenme Hilesi
Görüntülü Sohbet Parasız
Kripto Para Madenciliği Nasıl Yapılır
Linkedin Takipçi Hilesi
Lovely Coin Hangi Borsada


Post a Comment

Comment your feedback for the post. You can also ask your queries regarding android.

Blogger Widgets
All Rights Reserved Android Tutorials | Designed by: Deepak Anand