开发者

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

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();
             }
         }
    }
}


Call setVisibility(View.Invisible); with the help of button object created by you after the user clicks the button.


// To remove button
Button button = (Button) findViewById(R.id.button);
button.setVisibility(View.GONE);

// To transparent button
Button button = (Button) findViewById(R.id.button);
button.setVisibility(View.INVISIBLE);


You should not be doing this in onReceive(Context, Intent) method as mentioned in official documentation

This is called for every broadcast and before each of the above callback methods. You normally don't need to implement this method because the default AppWidgetProvider implementation filters all App Widget broadcasts and calls the above methods as appropriate.

You should do this in onAppWidgetOptionsChanged(). See the official docs.


public class Showing 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();
             }
          }
       }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜