Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 262
Fon Avatar asked Sep 22 '11 05:09

Fon


1 Answers

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().

Edited:

For Example

In onReceive() of AppWidgetProvider,

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

     remoteViews.setViewVisibility(viewId, visibility);

So for hiding your Button

     remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);

Edit - 2: According to Kartik's comment,

Sample Code:

    public class ButtonHideShowWidget extends AppWidgetProvider {

     private static boolean status = false;

     @Override
     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 );
                 if(status){
                   remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);
                  status = false;
                 }else{
                   remoteViews.setViewVisibility(R.id.buttonId,View.VISIBLE);
                  status = true;
              }

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

                 (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                 //Toast.makeText(context, "Clicked "+status, 2000).show();
             }
         }
    }
}
like image 196
Hiral Vadodaria Avatar answered Nov 02 '22 23:11

Hiral Vadodaria