Activity Circle while WebView loads any page? - android

I have an WebView app that uses multiple pages, I want to have a Activity Circle that shows that a new page is loading. As of right now when I click a link/button in the app it stays on the same page until the next one is fully loaded, this leaving users confused when they have bad connection.
I have Googled and looked here on Stack Overflow but only found questions asking how to set a loading indicator upon first launch.
I'm guessing I don't need to provide my code here.

As far as I can understand your question, you want to show the progress bar if the page is loading in the webview and when page is fully loaded then display the content of the page. Right ?
Here is what you can do.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
// Here you can check if the progress is = 100 or not
if(progress == 100){
// hide the progress bar
// show the webview
} else {
// show the progress bar
// hide the webview
}
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://developer.android.com/");
Code reference from Webviews - Android developers

Related

Unable to program detection behavior on Webview and xml Textview

I am trying to change the visibility of my textview inside onCreate method, but I cant seem to get a handle for it. Or, perhaps something else is wrong. Also, the getUrl() from the WebView class is not behaving as it should. It seems that it detects the url i am looking for after i leave the page with the url i am trying to detect. After reading the documentation, it made it somewhat clear based on the definition of the method getUrl(). Here is what the documentation says: "Gets the URL for the current page. This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed."
Here's what I mean:
I click on "link within the webpage that has url am trying to detect call it url5", then pressed back button, then click on "some other link within webpage call it url6" . Now, when I go back out of link with url6, the link for url5 is detected and I should be able to setVisibility of my textview as VISIBLE, which i tried but nothing happens. Well, here's my code. Please advise of anything that I might be doing wrong.
"xml format"
<RelativeLayout>
specifications for the relative layout...
<TextView>
android:visibility="invisible"
other specifications such as match_parent for both height and width
/>
<LinearLayout>
specifications of linear layout...
<LinearLayout/>
</RelativeLayout>
"inside onCreate"
relative_layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.content_main, null);
text_view = (TextView)relative_layout.findViewById(R.id.text);
WebView w = (WebView) findViewById(R.id.webview);
w.setWebViewClient(new MyWebViewClient());
//outside of onCreate inside private class MyWebViewClient
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
if(W.getUrl().equals("url I want to detect") ||
W.getUrl().equals("another url I want to detect")){
text_view.setVisibility(View.VISIBLE); //HOWEVER NOTHING SHOWS AT ALL
relative_layout.setVisibility(View.INVISIBLE);
}
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && w.canGoBack()) {
if(text_view.isShown()){
w.goBack();
text_view.setVisibility(View.INVISIBLE);
relative_layout.setVisibility(View.VISIBLE);
}else w.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

how to apply zoom in/out functionality for Image view on button click?

I am new to Android and I have to create one android application that shows the images from the server. I want to apply zoom effects on button click. I have two buttons one for zoom in and another is form zoom out. I want to apply zoom in or out on click of related button click. I searched a lot but did not find any thing related to my specific case. I have so far implemented imageview.onTouchlisterner() for zoom effects but I don't want this I want to do zoom on button click so please help me. Thank you in advance.
a very simple technique that i used for such purpose, make a webview and load you image using its url into that webview on image clicked like this:
WebView web=(WebView)findViewById(R.id.web);
mProgress = ProgressDialog.show(this, "Please wait!", "Loading image and zoom controlls...");
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setBuiltInZoomControls(true);
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
String url=getIntent().getExtras().getString("imageURL");
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
web.loadUrl(url);
}

Start animation on page load, stop on page loaded

I have a MenuItem in my ActionBar that is a "reload" icon. My Activity has a WebView and I'd like the icon to start animating when the WebView starts loading a webpage and stop when it's finished. This includes when clicking on links in the site that is loaded. What I have so far, works the first time I bring up a webpage, but if I leave the Activity and load another webpage, the "reload" icon seems to double up, or I will get NullReference exception thrown on refreshItem.setActionView(ivRefresh);
Here is my code:
public class Browser extends SherlockFragmentActivity {
private MenuItem refreshItem;
private WebView mWebView;
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.browser);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new WebBrowserClient());
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//if (!isFinishing() && progress == 100 && refreshItem != null && refreshItem.getActionView() != null)
//{
//refreshItem.getActionView().clearAnimation();
//refreshItem.setActionView(null);
//}
}
});
}
private class WebBrowserClient extends WebViewClient {
#Override
public void onLoadResource(WebView view, String url) {
//StartAnimation();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
StartAnimation();
}
#Override
public void onPageFinished(WebView view, String url) {
if (refreshItem != null && refreshItem.getActionView() != null)
{
refreshItem.getActionView().clearAnimation();
refreshItem.setActionView(null);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private void StartAnimation() {
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);
final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh);
ivRefresh.startAnimation(rotation);
refreshItem.setActionView(ivRefresh);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
refreshItem = menu.findItem(R.id.refresh);
return super.onCreateOptionsMenu(menu);
}
}
The commented out code is different ways I tried to get it to work.
UPDATE:
After debugging this more, I put a breakpoint in the StartAnimation function and sometimes it's hit up to 7 times in a row and other times it's not. This makes no sense, as, to me, this should be working. Perplexing...
SOLUTION (SORT OF):
Updating the StartAnimation() function to this, seems to fix this issue, but seems more of a duct tape solution:
private void StartAnimation() {
if (refreshItem != null && refreshItem.getActionView() == null)
{
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);
final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh);
ivRefresh.startAnimation(rotation);
refreshItem.setActionView(ivRefresh);
}
}
To create a smooth ui-experience, your 'loading wheel' animation should probably start when the user clicks the link - rather than when the new page starts loading - and end when the new page has loaded. This makes the app seem more responsive to the user, and is the behaviour implemented in Dolphin Browser, as an example.
To implement this, you need to listen for the user's link clicks in the WebView. How to do this has already been covered in this answer here on SO:
Detect click on HTML button through javascript in Android WebView
You have to validate that the click is in fact on a link to a new page. You can find the clicked HTML element via WebView's HitTestResult class. This answer on SO gives you the details on how to validate the HitTestResult value:
Get the click event from webpage in my android application
You start your animation from your onClick() method and end it from your WebViewClient's onPageFinished() method and, in case the new page doesn't load, also in onReceivedError().
Make the Animation variable the member of your Browser class and make sure you stop the animation before leaving the activity.
You can use the concept of threads.
Till the URL in Web View loads, you show the loading animation
and when URL is loaded stop the animation.
You need to use two threads, one for progress indicator (i.e URL Loading) and the other UI thread for your animation.
Search for it!

How to disable text selection in a webview in Android?

My application playing HTML promotions in WebView, Html promotions having text so if user press long click android standard dialog appear Copy/Share/Find/Web Search, so is there any option to disable text selection dialog for webview ?
Help me on the same.
You need to do this way:
WebView webView = (WebView) findViewById(R.id.webView);
webView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
webView.setLongClickable(false);
// Below line prevent vibration on Long click
webView.setHapticFeedbackEnabled(false);
Hope this will help you.

How to load a part of an webpage in android webview?

I would like to make an app, that shows a part of the site.
Lets say that i have this site:
I only want to show this:
https://mail.google.com/mail/?ui=2&ik=d5146163e8&view=att&th=132496ec47863fea&attid=0.1&disp=inline&realattid=f_gsbtyog80&zw
Is there anyway to do this?
I hope some of you could help me.
Gaauwe
You could load the url to a WebView and then set the WebView's scroll position and zoom level to display only a specific part of the web page. If you don't want to change zoom level you could initially scroll the WebView to the required location and then override the WebView's onTouch method so that it will only scroll upto the required cordinates.
public class MyWebViewActivity extends Activity implements PictureListener{
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setPictureListener(this);
// disable scroll on touch
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webView.loadUrl(url);
}
#Override
public void onNewPicture(WebView view, Picture picture) {
// TODO Auto-generated method stub
webView.scrollTo(x, y);
}
}
One of the way is to use PHP on your own server and CURL the webpage into it and strip out all the unnecessary things .
After that you only load it into your webview.
*This is one of the method, there are others method better then this. *
Another way is coldfusion cfhttp .. not sure,about this http://www.quackit.com/coldfusion/tutorial/coldfusion_http.cfm

Categories

Resources