Fon Fon - 4 months ago 15x
Android Question

How to Hide/Show button in android home screen widget

I am a beginner of android development. Current, I am working on creating a small home screen widget that is changing wallpaper of the mobile on click the button. The setting wallpaper is working fine but I want to make a clickable small picture (ImageView) to allow user to show and hide this setting button.

I setup it on service and use PendingIntent in order to attach my onClick event to the same service, but I cannot detect the property of button whether showing or hiding.

Therefore,is there any suggestion and solution to make my ImageView to show or hide the button in home screen widget?

Thanks in advance..


You can use mButton.setVisibility(View.GONE) to hide the button.

You can also check state of button's visibility in a boolean variable using mButton.isShown().


For Example

In onReceive() of AppWidgetProvider,

     remoteViews = new RemoteViews( context.getPackageName(), R.layout.yourwidgetlayout );

     remoteViews.setViewVisibility(viewId, visibility);

So for hiding your Button


Edit - 2: According to Kartik's comment,

Sample Code:

    public class ButtonHideShowWidget extends AppWidgetProvider {

     private static boolean status = false;

     public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      if (intent.getAction()==null) {

             Bundle extras = intent.getExtras();
             if(extras!=null) {

                 remoteViews = new RemoteViews( context.getPackageName(), R.layout.your_widget_layout );
                  status = false;
                  status = true;

                 watchWidget = new ComponentName( context, ButtonHideShowWidget.class );

                 (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                 //Toast.makeText(context, "Clicked "+status, 2000).show();