i created a webview inside my Android app with the following acitivity:
public class WebPageOpener extends Activity {
private WebView webView;
#SuppressLint({ "SetJavaScriptEnabled", "DefaultLocale" })
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle extras = getIntent().getExtras();
String url = extras.getString("url");
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
// settings
WebSettings webSettings = webView.getSettings();
webSettings.setSaveFormData(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
}
private class MyBrowser extends WebViewClient {
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.ProgressBar);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d("TAG", url);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("TAG", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
}
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
}
}
Here is the related 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" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="#+id/ProgressBar"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:visibility="gone"/>
</RelativeLayout>
I would avoid that users can tap on any buttons or image inside the webpage untill the webpage finish to load. In other words users can interact with webpage only when progress bar disappears. Any way?
I solved my problem. The trick is casting the progress bar to RelativeLayout, instead of ProgressBar, and then set its click listener to null in onPageStarted() method of WebViewClient class.
Here is my working solution:
private class MyBrowser extends WebViewClient {
final RelativeLayout progressBar = (RelativeLayout) findViewById(R.id.progressBarLayout);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d("TAG", url);
progressBar.setOnClickListener(null);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("TAG", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
}
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
}
Here is the related 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" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="#+id/progressBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</RelativeLayout>
You can see my changes comparing the code i posted in my question. Anyway it is just two rows...
Related
As soon as the Activity started from intent, need to display a ProgressBar. Once WebView is displayed, then ProgressBar need to be hidden.But not able to show ProgressBar in screen. Below is my 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">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_centerInParent="true"/>
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:visibility="invisible"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
below is my Activity,
public class WebViewActivity extends Activity {
String TAG= WebViewActivity.class.getSimpleName();
String url=null;
WebView webView;
ProgressBar progressBar=null;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
//getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_webview);
progressBar=findViewById(R.id.progress_bar);
Intent intent=getIntent();
this.url=intent.getStringExtra("url");
webView =(WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
AndroidLogger.log(5,TAG," error response"+error);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
AndroidLogger.log(5,TAG,"started"+url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
After starting Intent for WebViewActivity, instead of showing ProgressBar getting a blank white screen, until WebView opens up. So how to solve this. Anybody help with this.
Your xml file seems correct BUT your java file having unnecessary code stuff. just adding progress bar in xml will not work. you need to hide/show inside your java file.
correct it step by step and follow this.
public class WebViewActivity extends Activity implements Listener {
String TAG= PaymentGatewayActivity.class.getSimpleName();
String url=null;
String deviceType="ANDROID", name, password, code, pId=null;
String cur=null;
WebView webView;
ProgressBar progressBar=null;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_webview);
progressBar=(ProgressBar) findViewById(R.id.payment_progress_bar);
progressBar.setVisibility(View.VISIBLE);
AndroidLogger.log(5,TAG,"progress bar made visible");
Intent intent=getIntent();
this.pId=intent.getStringExtra("pId");
// HERE CALLING RETROFIT
CommunicationManager communicationManager = new CommunicationManager(WebViewActivity.this, this,pId );
communicationManager.getLink("get url");
}
//THIS METHOD WILL INVOKED WHEN GETTING SUCCESS RESPONSE
#Override
public void getUrlSuccessResponse(String url, String pId, String cur) {
this.url = url;
this.pId = pId;
this.cur = cur;
SharedPreferences sharedpreferences = this.getSharedPreferences(this.getResources().getString(R.string.app_preferences), Context.MODE_PRIVATE);
this.name = sharedpreferences.getString(this.getResources().getString(R.string.name), "");
this.password = sharedpreferences.getString(this.getResources().getString(R.string.password), "");
this.code = sharedpreferences.getString(this.getResources().getString(R.string.code), "");
webView = (WebView) findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webView.setBackgroundColor(Color.TRANSPARENT) //set this one to remove unnecessary background
//setup custom webview client here
webView.setWebViewClient(new PayWeb());
//load url inside webview
try {
String postData = "type=" + URLEncoder.encode("ANDROID", "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8") + "&code=" + URLEncoder.encode(code, "UTF-8") + "&cur=" + URLEncoder.encode(cur, "UTF-8") + "&name=" + URLEncoder.encode(subscriberId, "UTF-8") + "&pId=" + URLEncoder.encode(pId, "UTF-8");
webView.postUrl(url, postData.getBytes());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public class PayWeb extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.INVISIBLE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//show progressbar
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//hide progressbar
progressBar.setVisibility(View.INVISIBLE);
}
}
#Override
public void getUrlFailureResponse(int statusCode, String status) {
//manage webpage faliour case
}
#Override
public void getUrlNetworkFailureException(int statusCode, String status) {
//manage network faliour case
}
}
I used WebView in my app and it sees the scrolling is not working properly, when I trying to scroll down the page does not go down and the reverse is true, but when I try to touch the screen and get off in reverse "touch with scroll up" It's working!!, more clarification in this images gif1 , gif2
code
public class VisitSiteFragment extends android.app.Fragment {
private View view;
private ProgressBar webViewProgressBar;
private WebView webView;
String url;
public VisitSiteFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_visit_site, container, false);
if (getArguments() != null) {
url = this.getArguments().getString("url");
Log.e("URL", url);
}
webViewProgressBar = (ProgressBar) view.findViewById(R.id.webViewProgressBar);
webView = (WebView) view.findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
webViewProgressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webViewProgressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});
webView.loadUrl(url);
return view;
}
}
the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".VisitSiteFragment"
android:orientation="vertical"
android:background="#color/white"
>
<ProgressBar
android:id="#+id/webViewProgressBar"
android:layout_width="match_parent"
android:layout_height="8dp"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:theme="#style/progressBarBlue"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:progress="20"
/>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
I saw someone asking about this problem here, but the answer in that question did not help
After many attempts of WebView settings, I solved the issue by disabling scrolling that embedded with WebView as explained in this answer, and used ScrollView instead of it
The code after edited
webView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webView.setScrollContainer(false);
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
webViewProgressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webViewProgressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});
I'm using ProgressBar (with counting) to load a web site.
I'm using android.support.v4.widget.SwipeRefreshLayout to connect to internet when internet disconnected.
and
I'm using Custom error.html page to instead of default No internet connection page.
Before I added swipe refresh layout, progressbar was fine. After adding swipe refresh layout, progressbar doesn't work.
I want connect above 3 items.(progress bar,swipe refresh and custom error page)
So I connected all codes.but not working properly.Please can you help me ?
thank to you.
web_view.java
public class web_view extends AppCompatActivity {
SwipeRefreshLayout swipe;
ProgressBar progressBar;
WebView webView;
String url="http://blog.google.com";
TextView textView;
//to hide progressbar after loading part 1
LinearLayout liProgressContainer;
private String currentUrl;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_train);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb()
{
webView = (WebView) findViewById(R.id.webView);
WebSettings browserSetting = webView.getSettings();
browserSetting.setJavaScriptEnabled(true);
webView.loadUrl(url);
swipe.setRefreshing(true);
webView.setWebViewClient(new MyWebViewClient()
{
public void onReceivedError(WebView view,int errorCode,String description ,String failingUrl )
{
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view,String url)
{
swipe.setRefreshing(true);
}
});
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + " %");
}
});
}
//back button function
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
//hide header part
}
}
}
activity_webview.xml
<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=".web_view">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/liProgressContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_alignParentTop="true">
<ProgressBar
android:id="#+id/progressBar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:indeterminate="true"/>
<TextView
android:id="#+id/tvLoadingPercentage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
</LinearLayout>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/liProgressContainer"/>
</android.support.v4.widget.SwipeRefreshLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
help me
try this one code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/liProgressContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="#+id/progressBar"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:indeterminate="true" />
<TextView
android:id="#+id/tvLoadingPercentage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/liProgressContainer">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />
</RelativeLayout>
Replace your web_view activity code with this code
public class web_view extends AppCompatActivity {
SwipeRefreshLayout swipe;
ProgressBar progressBar;
WebView webView;
String url = "http://blog.google.com";
TextView textView;
//to hide progressbar after loading part 1
LinearLayout liProgressContainer;
private String currentUrl;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webView = findViewById(R.id.webView);
progressBar = findViewById(R.id.progressBar);
textView = findViewById(R.id.tvLoadingPercentage);
//to hide progressbar after loading part 2
liProgressContainer = findViewById(R.id.liProgressContainer);
swipe = findViewById(R.id.swipe);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
textView.setText(progress + " %");
}
});
WebSettings browserSetting = webView.getSettings();
browserSetting.setJavaScriptEnabled(true);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb() {
webView.loadUrl(url);
swipe.setRefreshing(true);
}
//back button function
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
liProgressContainer.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
liProgressContainer.setVisibility(View.GONE);
swipe.setRefreshing(false);
}
}
}
I'm trying to open a google form in a WebView.
I write the form in English but when I embed it in my app the page displays in Arabic(?) and the text goes from the right to the left. Why?
my xml code:
<?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"
tools:context="com.example.user.webview.feedback">
<WebView
android:id="#+id/link_webview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Java
public class Feedback extends AppCompatActivity {
private WebView feedbackformlink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback);
setTitle("Your Feedback");
feedbackformlink = (WebView)findViewById(R.id.feedback_webview);
WebSettings webSettings = feedbackformlink.getSettings();
feedbackformlink.setInitialScale(200);
feedbackformlink.getSettings().setSupportZoom(true);
feedbackformlink.getSettings().setLoadWithOverviewMode(true);
feedbackformlink.getSettings().setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
feedbackformlink.loadUrl("https://google form .. share link");
feedbackformlink.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (feedbackformlink.canGoBack()){
feedbackformlink.goBack();
} else
super.onBackPressed();
}
}
Apply this below code inside OnCreate Method of Activity! No need to use Any Tag of WebView in XML.
Hope it works.
..........................
WebView mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
mWebview .loadUrl("https://docs.google.com/forms/ ... link");
setContentView(mWebview );
My webview does not work for the urls that redirect to another page. It opens browsers from the app but does not load the webview. Any idea?
The code for webview is:
webView = (WebView) findViewById(R.id.simpleWebView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
if(getIntent().getStringExtra("url")!=null){
loadLink(getIntent().getStringExtra("url"));
}else{
Toast.makeText(WebViewActivity.this, "Please try again later!",
Toast.LENGTH_SHORT).show();
finish();
}
Main Class
public class Main extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.webview = (WebView)findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
}
}
Your main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="#string/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
I think you should write what does your loadLink method contain.
Are you sure, that you've implemented it properly - webView.loadUrl(...)?
webView=(WebView)view.findViewById(R.id.webview);
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("https://www.google.co.in/");
return view;
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
pDialog.dismiss();
}
}