0

I need help with a Widget for Android App,

I want to animate a Refresh Button with two ImageViews. On Click the first Image should hide and the refresh button Image which is 180° degrees turned should appear. Than after small delay the 180° turned ImageView should hide again and the first ImageView should appear.

At The end I want to update the Widget.

enter image description here

I would be happy about better ideas to animate a refresh Button in an Android Widget. All StackoverFlow results are talking about the normal App...

CJ Kev
  • 31
  • 5
  • Welcome to SO, please read https://stackoverflow.com/help/how-to-ask – MadMax Aug 25 '21 at 09:36
  • Does this answer your question? [Button click event for android widget](https://stackoverflow.com/questions/14798073/button-click-event-for-android-widget) – Henry Twist Aug 25 '21 at 10:21
  • No it doesn't because I want a delay after the ImageView was clicked. Your highlighted Ticket just talk about "onClickListener for ImageViews in Widgets". – CJ Kev Aug 25 '21 at 11:49

3 Answers3

0

While using server call we can make progress in AsynCall as far now we can fix it like this

Img1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        //Hide the First Image
        //Show the 2nd Img after 3 Sec 

          DelayMethod();

    }
});

public void DelayMethod(){
 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            //Show the 1st Img after 3 Sec

        }
    },3000);
}
William Willi
  • 194
  • 1
  • 2
  • 19
  • but I want to use it for a widget. a remote view don't have a onClickListener. Where should I implement this method and what is Img1. can you give me the code for Img1 pls ? – CJ Kev Aug 25 '21 at 09:53
  • @CJKev img1 is the ImageView and for that you have to use DataBinding https://developer.android.com/topic/libraries/data-binding – William Willi Aug 25 '21 at 10:17
0

Maybe put both images(maybe add more images with different angles to make the animation smoother) in a view flipper and then set it to android:autoStart="true" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out" android:animateFirstView="true" Then set the flipping interval as you like using android:flipInterval="10000". Then you can stop the flipping once the widget is ready.

refrence for flipping view: https://stackoverflow.com/a/9253770/15552614

Mihir Shah
  • 51
  • 3
  • it works but I don't know how to stop the view flipper programmatically. Can you help? – CJ Kev Aug 25 '21 at 11:38
  • @CJKev You can put a listener on the flipper's animation and then put a check inside onAnimationEnd() that if the widget is ready or not. And if the widget is ready then simply call `viewFilpper.stopFlipping()` – Mihir Shah Aug 25 '21 at 11:58
  • @CJKev this https://stackoverflow.com/questions/12610106/how-to-stop-android-viewflipper-from-looping and https://stackoverflow.com/questions/3813108/listener-for-viewflipper-widget-flipping-events/3813179#3813179 might help. – Mihir Shah Aug 25 '21 at 12:06
0

Here is my solution for my own Problem:

I created an PendingIntent to listen to the button.

remoteviews.setOnClickPendingIntent(R.id.refresh_btn,
            getPendingSelfIntent(context, BTN_REFRESH));

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

I use global toggleState and call a method:

private void refresh(RemoteViews remoteviews, Context context) {
    if (toggleState) {//if Button gets clicked onReceive will update all widgets
        remoteviews.setViewVisibility(R.id.refresh_btn, View.GONE);
        remoteviews.setViewVisibility(R.id.refresh_btn_rev, View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toggleState = false;
                updateAllWidgets(context);
            }
        }, 500);
    } else {
        remoteviews.setViewVisibility(R.id.refresh_btn, View.VISIBLE);
        remoteviews.setViewVisibility(R.id.refresh_btn_rev, View.GONE);
    }
}

toggleState is set to true if Button gets clicked.

CJ Kev
  • 31
  • 5