Search code examples
androidandroid-widgetimagebuttontogglebutton

how to creat a toggle button for widget app


this is the closet post I found to my question How to make a toggle button for a widget android, however, I am looking for a more straight forward solution to this. Does any one have any straight forward solution to make a toggle button using image button to create a simple widget app? I would greatly appreciate any advice on that.

I only need a simple toggle button which I can listen for the state change using a service class. Is this possible?

Many thanks in advance.


Solution

  • As per your comments, I am posting sample relevant code below which can be used to change the image used in your widget:

                    case WifiManager.WIFI_STATE_ENABLED:
                        remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_on);
                        break;
                    case WifiManager.WIFI_STATE_ENABLING:
                        remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_transition);
                        break;
                    case WifiManager.WIFI_STATE_DISABLED:
                        remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_off);
                        break;
                    case WifiManager.WIFI_STATE_DISABLING:
                        remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_transition);
                        break;
                    case WifiManager.WIFI_STATE_UNKNOWN:
                        remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_error);
                        break;
    

    For a more thorough tutorial about creating homescreen widgets, you can visit: http://www.vogella.com/articles/AndroidWidgets/article.html

    It explains the whole process including how to update the widget with new information