0

I would like to show a retry button when my application cannot load a page. As far as I understand, whenever this happens, the onReceiveError() method on WebViewClient is called.

I have a piece of code that does an animation. It makes a button visible and animates the y property of it until it reaches the middle of the screen. However, I would like to be able to fire this piece of code in the onReceiveError() method, and animate the button down and make it invisible when onPageFinished() happens.

My problem is not with the animation bit of the code. My problem is in where to put it. I can't put the animation code inside the WebViewClient since as I said above, I have to reach through the WebView to its parent activity, and animate a Button defined on that activity. This violates every object-oriented best practice I'm aware of, so I have a bad feeling about it. However, I don't know where else I can put it. I'm looking for a best practice defined by the community on code like this, where a dependency of a view (i.e. WebViewClient) needs to change something on an activity.

I'm new to Android development, so please be gentle.

Parham Doustdar
  • 2,019
  • 3
  • 22
  • 41
  • This would be the same as any other view animation in Android, where the view you want to animate is positioned above the WebView on the z axis when visible. Have you searched for anything on the topic of view animation yet? – Doug Stevenson Feb 14 '16 at 00:08
  • Yes. My issue is not the bit about animation; I know how to animate. I don't know "where" to animate. That is, it doesn't feel right to put the animation code inside `WebViewClient` since I'm reaching through another object (i.e. `WebView`) to its activity and animating one of its views (i.e. the `Button`). Where should I put the animation code? I'm guessing I should put it in the `MainActivity` and have it listen to the `onReceiveError` event, but I'm not sure how to do it, or if it is even possible, or whether it is idiomatic. – Parham Doustdar Feb 14 '16 at 06:30
  • I edited my question to hopefully make it clearer. Thanks a lot for your help! – Parham Doustdar Feb 14 '16 at 06:37
  • You can animate a view in the same view hierarchy as the WebView, not inside the WebView. Just be sure to place it on top of the WebView so it can be seen. – Doug Stevenson Feb 14 '16 at 07:01
  • Wouldn't I have to use `view.getParent().findViewById(R.id.btn_retry).animate().y(...)`? If I have to do this, I'm reaching through the `Webview` to its parent, which is the `MainActivity`. – Parham Doustdar Feb 14 '16 at 11:04
  • Just find the view by its ID and work with it from there. Nothing unusual about that. – Doug Stevenson Feb 14 '16 at 19:40
  • Sorry, I don't understand what you mean. Can you please create an answer and give me a code example? The code I'm looking for is the code inside a concrete `WebViewClient` implementation that overrides `onReceiveError`. – Parham Doustdar Feb 28 '16 at 06:07

0 Answers0