Translate To Your Own Langauge

Showing posts with label change profile mode programmetically. Show all posts
Showing posts with label change profile mode programmetically. Show all posts

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;
       }

}

by Unknown · 14

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