I am new with Android developement and I am trying to hide a ProgressBar after page not but it is not hiding.
Here is my code for main_activity.xml and MainActivity.java
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="395dp"
android:layout_height="715dp"
android:layout_centerHorizontal="true"
android:id="#+id/webView"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
</WebView>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
/>
</RelativeLayout >
MainActivity.java
package test.com.webview;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressBar progressBar;
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView) findViewById(R.id.webView);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("www.google.com");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.INVISIBLE); // to hide
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE); //to show
progressBar.setVisibility(View.INVISIBLE); // to hide
}
}
}
I am using onPageStarted and onPageFinished method for progressbar and tried to get some solution from google but nothing seems working or basically it might be because of my experience with android i am not able to make it work so please help me with my code. Thanks
You have to change this line:
webview.setWebViewClient(new WebViewClient());
to the next line:
webView.setWebViewClient(new myWebClient())
Because you are using default client and not your own that you have created.
Related
I was try to create a android webview with the refresh option.
Whenever I pull down for refresh it always goes to the home page.
I have try using the swiperefeshlayout. My code are as follows
activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/destiny"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/progressBar" />
</RelativeLayout>
MainActivity.java file
package com.my.project;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
SwipeRefreshLayout swipe;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
WebAction();
}
});
WebAction();
}
public void WebAction(){
myWebView = (WebView)findViewById(R.id.destiny);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
myWebView.loadUrl("http://myurl");
swipe.setRefreshing(true);
myWebView.setWebViewClient(new WebViewClient());
AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this); //pass the activity in constructure
appUpdateChecker.checkForUpdate(false); //mannual check false here
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
public void onReceivedError(WebView view, int errorCode, String description, String fallingUrl) {
myWebView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
progressBar.setVisibility(View.VISIBLE);
setTitle("Loading.....");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url){
progressBar.setVisibility(View.GONE);
setTitle(view.getTitle());
super.onPageFinished(view, url);
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
With this code the refresh always goes to the home page
but I want to refresh the webview on the same page itself.
You are setting all variables and actions again onRefresh() you only need to load current url of WebView again. So you need to change your OnRefreshListener like below;
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myWebView.loadUrl(myWebView.getUrl());
}
});
I am trying to load a simple PTI new page. But getting error. Below is my code:
WebView newsContent = (WebView) findViewById(R.id.news_content);
newsContent.getSettings().setJavaScriptEnabled(true);
newsContent.getSettings().setLoadWithOverviewMode(true);
newsContent.getSettings().setUseWideViewPort(true);
newsContent.loadUrl("http://www.ptinews.com/news/9168439_Aadhaar-linkage-with-bank-accounts-mandatory--says-RBI.html");
And the error is :
EDIT:
I have used the following:
<uses-permission android:name="android.permission.INTERNET" />
Which device you used for checking WebView
I have implement webview as follow and its work for me
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dhruv.demoapiaitutorial.WebVIewActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
WebViewActivity.java
package com.example.dhruv.demoapiaitutorial;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebVIewActivity extends AppCompatActivity {
private WebView mWebview;
private ProgressDialog dialog; //creating object of progress dialog
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait, your web is being open...");
dialog.setCancelable(false);
mWebview = (WebView) findViewById(R.id.webview);
mWebview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
/*Showing progress dialog*/
dialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/*You can also access url in this overridden method*/
/*closing progress dialog*/
dialog.dismiss();
}
});
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.getSettings().setLoadWithOverviewMode(true);
mWebview.getSettings().setUseWideViewPort(true);
mWebview.loadUrl("http://www.ptinews.com/news/9168439_Aadhaar-linkage-with-bank-accounts-mandatory--says-RBI.html");
}
}
I can't load this specific url (http://sia.bogota.unal.edu.co/academia/) using WebView, but I can load other URLs. (See images below)
This is my code
Activity Class:
package co.luisfer.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
Activity activity;
private ProgressDialog progDailog;
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading", "Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
settings.setDomStorageEnabled(true);
webView.loadUrl("http://sia.bogota.unal.edu.co/academia");
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
I don't have any clue what's going on.
Can you help me?
Thanks in advance
PS: I have already set up the internet permission:
<uses-permission android:name="android.permission.INTERNET" />
Ok so I've been working on an app in Android Studio for school. I have a webview inside of a fragment. But in the app, when ever I open the fragment on my phone, the app crashes and my phone says "Unfortunately, LARKer App has stopped."
Here's the Fragment class:
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Andrew on 12/29/2014.
*/
public class menu4_Fragment extends Fragment {
private WebView mWebView;
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu4_layout,container,false);
mWebView = (WebView) getView().findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("foo");
mWebView.setWebViewClient(new MyWebViewClient());
return rootview;
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().contentEquals("foo")) {
// This is my website, so do not override; ler my WebView load the page
return false;
}
//Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
And here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LARK: Login"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:textSize="50dp" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_below="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here's an image of what I got on my phone:
Change this code
mWebView = (WebView) getView().findViewById(R.id.webView);
Like this, it will work
mWebView = (WebView) rootview.findViewById(R.id.webView);
You have to get the view's from your View Group.otherwise it will return error.
change this on your oncreateview() methed of your fragmanet
mWebView = (WebView) rootview.findViewById(R.id.webView);
Try this code,
WebView webViewInfo = (WebView) findViewById(R.id.webview);
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("LOAD_YOUR_URL", "text/html", "UTF-8");
And WebViewClient class implement on webview method.
private class MYWEBCLIENT extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On keyboard back handle,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewInfo.canGoBack()) {
webViewInfo.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I hope this code help for you...!!
I have created an edittext and a go button and a webview in single layout. When a person enters a url and clicks on go button, the webview should load the website content, Instead it is going to default browser to load website how should i slove this issue ?
This is my code
xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="#+id/etenterurl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:inputType="textUri" />
<Button
android:id="#+id/buttongo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3.0"
android:text="Go" />
</LinearLayout>
</FrameLayout>
java code :
import java.util.ArrayList;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.annotation.SuppressLint;
import android.app.Activity;
public class MainActivity extends Activity {
EditText etenter;
Button bgo;
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etenter= (EditText)findViewById(R.id.etenterurl);
bgo =(Button)findViewById(R.id.buttongo);
webview = (WebView)findViewById(R.id.webview);
bgo.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onClick(View v) {
String url=etenter.getText().toString();
if(url.trim().length()>0){
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://"+url);
}
}
});
}
}
You have to use WebViewClient
wbView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.v("uuuurl",url);
view.loadUrl(url);
return true;
}
}
See here for example
you should use WebViewClient
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("TAG", " ==> "+url);
view.loadUrl(url);
return true;
}
});