3/6/11

Android App : Ring Profile Changer

Here is an App which will reduce the work load of the users of Android phones, as they wont have to go to settings to change their ring mode when in office, college or meeting, they can just use this app and change their mode in one tap on the screen.


Create the new application by choosing File➪New Project. Choose Android Project from the list, and then click the Next button. Use Table  for your project settings.


Project Settings


Setting Value
Application Name   Ring Mode Toggle
Project name Ring Mode Toggle
ContentsLeave the default selected (create new project in workspace)
Build target Android 3
Package nameandroid.ringmodetoggle
Create activity MainActivity
Min SDK Version 11


Click the "finish" button.


After you click the finish button, the project folder will get loaded in the left panel in eclipse.


It will have folders like :





As you can see their are two xml files. main.xml is the file where you will define the layout and the widgets that you will be using in the app.


main.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">
    <ImageView android:src="@drawable/index" android:layout_width="wrap_content"  
    android:layout_height="wrap_content" android:layout_gravity="center_horizontal" 
   android:id="@+id/phone_icon"></ImageView>
    <Button android:layout_height="wrap_content" android:id="@+id/toggleButton"  
    android:layout_width="wrap_content" android:layout_gravity="center_horizontal" 
   android:text="Toggle Silent Mode"></Button>
</LinearLayout>


string. xml is the xml where you define your string name and values:


string.xml :
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Mode Toggle</string>
</resources>


The drawable folder contains the icon of the app that will appear on the phone.
1. drawable-ldpi (Low-density screen (ldpi)) show have the image (ie .png) with the size 36 x 36 px
2. drawable-mdpi (Medium-density screen (mdpi)) show have the image (ie .png) with the size 48 x 48 px
3. drawable-hdpi (High-density screen (hdpi)) show have the image (ie .png) with the size 72 x 72 px


Along with the above images, the folder should also contain the images that you will using to show that phone is in silent or in ringing mode. Like I have the below image :


Ringing Mode


Silent Mode


The src file has the file ie the activity file with the name that you provide at first in the activity field. Like i gave MainActivity so I had a MainActivity.java file:


MainActivity.java file should look like this :


package com.android.modetoggle;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.net.NetworkInfo.DetailedState;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.media.*;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    
    private AudioManager maudio;
    private boolean msilent;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        maudio=(AudioManager)getSystemService(AUDIO_SERVICE);
        
        checkWhetherSilent();
        ButtonClickChecker();
        
    }
    
    public void ButtonClickChecker(){
        Button toggleButton=(Button)findViewById(R.id.toggleButton);
        
        
        toggleButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(msilent){
                    maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    msilent=false;
                }else{
                    maudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    msilent=true;
                }
                toggleU1();
            }
        });
    }
    
    private void checkWhetherSilent(){
        int ringer=maudio.getRingerMode();
        if (ringer==AudioManager.RINGER_MODE_SILENT){
            msilent=true;
        }else{
            msilent=false;
        }
    }
    
    private void toggleU1(){
        ImageView img=(ImageView)findViewById(R.id.phone_icon);
        Drawable drw;
        
        if(msilent){
            drw=getResources().getDrawable(R.drawable.index1);
        }else{
            drw=getResources().getDrawable(R.drawable.index);
        }
        img.setImageDrawable(drw);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        checkWhetherSilent();
        toggleU1();
    }
    
}


After all this you have done then you should just run the project as an Android Application and after the emulator opens up the you will be able to see your application. Just click on it and Here it Goes. You just created your Ring Mode Toggle Android App.

SHARE THIS POST: