mhrzn mhrzn - 2 months ago 4x
Android Question

Android 5.1 -How do i update the UI change when i change configuration from notification tray?

I have a simple app that change that toggle the silent mode to normal or vice versa. But the problem is that i can just drag the notification tray can change the mode from silent to normal and i want the Volume icon inside the also change to normal icon.

In which state the application goes in when i pull down the Tray Notification ?

enter image description here

Now When i tap the mute to normal mode from tray, which method should i put my update method to reflect the changes.

The uiupdate

I tried callinf updateUI method onResume but doesn;t seems to work as the application doesn't seem to be pause.

package com.example.manis.silentmodetoggle;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity {

private AudioManager audioManager;

protected void onCreate(Bundle savedInstanceState) {

Log.v("myTest","called onCreate");

//reference to the audio manager
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
FrameLayout contentView = (FrameLayout) findViewById(;


contentView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

private void updateUi(){
ImageView imageView = (ImageView) findViewById(;

//Set The PhoneImage
int phoneImage =RingerHelper.isPhoneSilent(audioManager) ? R.drawable.noaudio_96: R.drawable.highvolume_96 ;


protected void onStart() {
Log.v("myTest","Called onStart");


protected void onResume() {
Log.v("myTest","Called onResume");


protected void onPostResume() {
Log.v("myTest","Called onPostResume");

protected void onPause() {
Log.v("myTest","Called onPause");

package com.example.manis.silentmodetoggle;


public class RingerHelper {

private RingerHelper(){};

public static void performToggle(AudioManager audioManager)
//if the phone is currently silent then set ot normal
//if normal then set to silent
isPhoneSilent(audioManager)? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT

public static boolean isPhoneSilent(AudioManager audioManager){
return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT;


ps:This is sample app from Android App development from dummies book.


There aren't any direct callbacks when the notification tray is dragged down or pulled up. It's meant to be decoupled from the applications. That being said, there are a couple things you might be able to try.

First, you could write a service that will poll for the ringer status periodically. Not the ideal solution, but this will definitely work.

Second, you could try implementing onWindowFocusChanged(), like so:

public void onWindowFocusChanged(boolean hasFocus) {

    if(hasFocus) {
        // check ringer status


And if you want to be able to programmatically expand the status bar from your application, you can include this permission:

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />