How to hide / show a button in Android home screen widgets

I am starting Android development. I'm currently working on a small home screen widget that changes a mobile phone’s wallpaper to a button. The installation wallpaper works fine, but I want to make an interactive picture (ImageView) to allow the user to show and hide this settings button.

I configure it for the service and use PendingIntent to attach my onClick event to the same service, but I cannot determine the property of the button to show or hide.

So are there any suggestions and solutions to get ImageView to show or hide the button in the home screen widgets?

Thanks in advance.

+4
source share
5 answers

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

You can also check the visibility status of buttons in a boolean variable using mButton.isShown ().

Edited by:

Example

In onReceive() of AppWidgetProvider ,

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

So, to hide your button

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

Edit - 2: According to Kartik's comment,

Code example:

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

Call setVisibility (View.Invisible); using the button object that you created after the user clicks the button.

+1
source
 <pre><code> // 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); </code></pre> 
0
source

You should not do this in the onReceive (Context, Intent) method, as indicated in the official documentation

This is called for each translation and before each of the above callback methods. Usually you do not need to implement this method, because by default the AppWidgetProvider implementation filters all broadcast applications and calls the above methods.

You must do this in onAppWidgetOptionsChanged (). See white papers.

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

All Articles