Good morning,
I created an application with the code below, it allows to display my site with webview
Everything works very well except for a small problem, the message Loading appears for a while and disappears, I want that remains until the total loading of the whole page (images ...).
Thanks for your help
mainactivity.java
package com.example.monsport;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private WebView WebView;
ProgressBar progressBar;
ProgressDialog progressDialog;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progress) ;
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading wait....");
WebView = (WebView) findViewById(R.id.WebView);
WebView.getSettings().setJavaScriptEnabled(true);
WebView.setWebViewClient(new WebViewClient());
WebView.loadUrl("https://www.mywebsite.com/apps/");
WebView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(android.webkit.WebView view, int newProgress) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
progressDialog.show();
if (newProgress == 100){
progressBar.setVisibility(View.GONE);
progressDialog.dismiss();
}
super.onProgressChanged(view, newProgress);
}
});
}
//This method require to use back button if want to go previous web page
#Override public void onBackPressed() {
if (WebView.canGoBack()) {
WebView.goBack();
} else {
super.onBackPressed();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_gravity="center"
android:gravity="center">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_marginTop="-3dp"
android:progress="20"
android:visibility="gone">
</ProgressBar>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/WebView">
</WebView>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried the loading code but it appears constant for all pages and disappears even if the page has not been completely loaded !
Thank you
Related
I want to display webview below progressbar. How to do it ?
My code is as below :
<FrameLayout
android:id="#+id/flWeb"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
<WebView
android:id="#+id/wvWebsite"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
I used this and solved my problem...
<FrameLayout
android:id="#+id/flWebpre"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/wvWebsite"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center_horizontal" />
</FrameLayout>
This is the code i have used to show the progress:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
WebView mWvUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Activity activity=this;
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
activity.setProgressBarIndeterminateVisibility(false);
mWvUrl = (WebView) findViewById(R.id.wv_url);
mWvUrl.getSettings().setJavaScriptEnabled(true);
Button btnLoad = (Button) findViewById(R.id.btn_load);
btnLoad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText etUrl = (EditText) findViewById(R.id.et_url);
mWvUrl.loadUrl(etUrl.getText().toString());
activity.setProgressBarIndeterminateVisibility(true);
}
});
mWvUrl.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/** This prevents the loading of pages in system browser */
return false;
}
/** Callback method, executed when the page is completely loaded */
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getBaseContext(),
"Page loaded Completely",
Toast.LENGTH_SHORT).show();
/** Hiding Indeterminate Progress Bar in the title bar*/
activity.setProgressBarIndeterminateVisibility(false);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You need another Layout. The FrameLayout is not the better way to do alignement between components. If you are using a FrameLayout, components will be superimposed. I suggest you to use a LinearLayout vertical or a RelativeLayout.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
<WebView
android:id="#+id/wvWebsite"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I want to hide the URL bar from the browser permanently, and it should not appear even if the person gets redirected from one page to another.
Here is my .java code:
package com.example.com.android.royalcastleapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.support.v4.app.NavUtils;
public class Bookinghome extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookinghome);
//Get a reference of WebView holder
WebView webview = (WebView) this.findViewById(R.id.webview);
//Get the settings
WebSettings websettings = webview.getSettings();
//Enable Javascript
websettings.setJavaScriptEnabled(true);
//Make the zoom controls visible
websettings.setBuiltInZoomControls(true);
//Load the default url.
webview.loadUrl("http//okRoom.aspx");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_bookinghome, menu);
return true;
}
}
And here is my layout:
<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" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".Bookinghome"
android:id= "#+id/webview"/>
</RelativeLayout>
If anyone could suggest any changes I could make, that would be great!
Your application starts default browser.
You need to use WebViewClient.shouldOverrideUrlLoading like described here: https://stackoverflow.com/a/2379054/2183804
I have created a WebViewActivity extending Activity and showing a WebView inside of my app instead of redirecting to the browser.
So I tried to use AsyncTask to show my ProgressBar while the view is loading. But the progress bar doesn't display at all. Here is the code I'm using:
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity;
public class WebViewActivity extends Activity {
WebView webView;
TextView textView;
ProgressBar bar;
private class Async extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
String url = new String(getIntent().getStringExtra("url"));
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(url);
textView.setText(getIntent().getStringExtra("title"));
bar.setVisibility(ProgressBar.INVISIBLE);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_web_view);
vueText = (TextView) findViewById(R.id.barTitle);
vueWeb = (WebView) findViewById(R.id.WebView);
bar = (ProgressBar) findViewById(R.id.progressBar);
bar.setVisibility(ProgressBar.VISIBLE);
new Async().execute();
}
}
And here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/grayBackground"
android:orientation="vertical" >
<TextView
android:id="#+id/barTitle" />
<ScrollView
android:id="#+id/sc1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/barTitle"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="#+id/WebView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>
You should perform the background action (loading the URL) of the AsyncTask in the doInBackground() method.
The onPostExecute() is invoked on the UI thread so it will freeze the UI while running.
See the documentation of AsyncTask for more details.
You have to use pubishProgress() inside the doInBackground.
Then override onProgressUpdate
I am opening a simple flash based website in my webview using following code:
Java Code:
package com.sample.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class SampleWebViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webView = (WebView) findViewById(R.id.webview);
final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("loading");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progress.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.dismiss();
}
});
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("http://www.fusioncharts.com/demos/features/#linkedcharts-for-easy-drill-down");
}
});
}
}
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Load URL" />
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp" />
</LinearLayout>
Now when the page is loaded completely and i am trying to scroll down to view rest of the content, the webview is coming on top of "Load URL" button. Check the screenshot:
while it should look like:
Please suggest, what should i do so that it works correctly.
Edit: I am using a Galaxy Note(OSv2.3.5) with the latest version of both Flash Player and Adobe Air installed.
I have also tried this things but I think the only solution to do this is by using only a single webview in your xml, so, you don't have other native controls.
This is really broken on Android. If you try to use the Build in topbar it will also go behind the flash. I'd really recommend you go away (far away) from flash. There is some discussions about it on google groups and some on stackoverflow but all ends without any good results. The only good suggestion I found was using flash to cover other flash components =/
How can I add a button on a WebView?. I have a WebView and I want to show a popup. For that I need to add a button on the bottom-left corner of WebVew. How can I do this?
I would use a RelativeLayout. I like using it a lot. Its a great way to easily place and organize views,buttons,layouts,etc...
Some Example code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFD0">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/My_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="My Button!" />
</RelativeLayout>
I think the views and buttons will be drawn in order from top to bottom in the XML, but it may be the other way around.use margins like :
android:layout_marginLeft="15dip" and android:layout_marginBottom="10dip"
to help adjust the position.
What kind of content is in the WebView? Is it some HTML that you can control / modify?
If yes, just add a <button> tag and position it accordingly using CSS.
If no, use Wayner's solution.
package com.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class webview extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
Button btnTag = new Button(this);
btnTag.setText("Button");
btnTag.setId(1);
webview.addView(btnTag);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
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://www.google.com/");
}
}