Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:35:49+00:00 2026-06-17T06:35:49+00:00

After putting off and being lazy for a long time, I’ve finally decided to

  • 0

After putting off and being lazy for a long time, I’ve finally decided to learn Android programming. So yeah I’m pretty much a basic n00b in it.

I created an app which toggles the phone’s mode to silent and back to ringer. Now the problem that I’m facing is that if the app goes to background and when it is resumed, the state of the toggle button needs to be according to the state the phone is in.

Example:
If the phone is already in Silent mode and I launch the app, the toggle button should be pressed/activated and showing the appropriate text according to it.

Now, from what I’ve figured, this state setting should be done in OnCreate/OnResume, when it is checking if the Phone is silent.

Here’s the code from the MainActivity.java

package com.SMT.silentmodetoggle;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.ImageView;
import android.util.Log;

public class MainActivity extends Activity {
    private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;
    private static final String TAG = "SilentModeApp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

        checkIfPhoneIsSilent();
        setBUTTONCLICKLISTENER();

        Log.d("SilentModeApp", "This Is A Test");
    }

    private void setBUTTONCLICKLISTENER() {
        ToggleButton toggleButton1 = (ToggleButton)findViewById(R.id.toggleButton1);
        toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonview, boolean isChecked){
                if (mPhoneIsSilent) {
                    // Change back to normal mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    mPhoneIsSilent = false;
                    } 
                else {
                    // Change to silent mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    mPhoneIsSilent = true;
                    }
                    // Now toggle the UI again
                    toggleUi();
                }
         });
    }

    private void checkIfPhoneIsSilent() {
        int ringerMode = mAudioManager.getRingerMode();
        if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
            mPhoneIsSilent = true;

        } 
        else {
            mPhoneIsSilent = false;
        }
    }
        /**
        * Toggles the UI images from silent to normal and vice versa.
        */
    private void toggleUi() {
        ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
        Drawable newPhoneImage;
        //ToggleButton checkTB = null;
        if (mPhoneIsSilent) {
            newPhoneImage = getResources().getDrawable(R.drawable.phone_off);
            //checkTB.setChecked(true);
        }
        else {
            newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
            //checkTB.setChecked(false);
        }

        imageView.setImageDrawable(newPhoneImage);
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkIfPhoneIsSilent();
        toggleUi();
    }

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

}

Here’s the info on the toggle button:

 <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/phone_icon"
        android:layout_centerHorizontal="true"
        android:text="ToggleButton1" 
        android:textOff="Toggle Silent Mode On"
        android:textOn="Toggle Silent Mode Off"
        />

I’m running/debugging the app on Samsung Galaxy SII I9100 running CM10.1 4.2.1

Eclipse is already set to target 4.2

All help and feedback is always welcome and valuable 🙂
Thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:35:50+00:00Added an answer on June 17, 2026 at 6:35 am

    Just add the following after checkIfPhoneIsSilent(); in onCreate() method. Also add it in the onResume() to check the state change outside the activity.

     ToggleButton toggleButton1 = (ToggleButton)findViewById(R.id.toggleButton1);
     toggleButton1.setChecked(mPhoneIsSilent);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After searching around for months on and off I finally decided to post this
I putting wmd on my website. After putting code in and giving it a
what does putting () after a word do? sometimes it doesn't work
After some time researching and trying different things I still cannot get my @ExceptionHandler
After a long search I'm still confused about it although I found some related
Good Sunday morning! After fiddling around with some JavaScript this weekend, I finally wrote
does anyone know why i'm getting a null date value after putting this thru
I've purchased shared hosting with microhosting.in After putting together site for my start-up the
I am in the process of putting together a little Android app. I have
Guys.. I want my html div not to resize after putting some text.. as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.