How to add a progress bar for webview - android

I'm new to app building... I tried to add a progress bar or a loading screen to a practice app but i wasn't successful.
Could you please help and show me how to do it and where to add it in the code.
Tab2.java:
package com.dan.test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class Tab3Fragment extends Fragment {
private WebView webView;
private Bundle webViewBundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.tab2,
container, false);
webView = (WebView) ll.findViewById(R.id.tab2);
webView.setWebViewClient(new WebViewClient());
*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%
activity.setProgress(progress * 1000);
}*
if (webViewBundle == null) {
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.youtube.com/");
} else {
webView.restoreState(webViewBundle);
}
return ll;
}
#Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
tab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
*<ProgressBar android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:id="#+id/progressBar1"/>*
<WebView
android:id="#+id/webView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Thanks in advance,
Dan.

This is how I have been using it in the past.
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (!mProgressDialog.isShowing()) {
mProgressDialog.show();
}
mProgressDialog.setProgress(progress)
if (progress == 100) {
mProgressDialog.dismiss();
}
}
});
Where mProgressDialog is a Dialog where I am showing the progress.

Related

How to get the content from webview and display it in a Textview

In my app, I need to display the content of a webview to the user via textView so that they can make changes for further use.
Can this be possible by reading the screen of the phone and displaying in a textView?
Or is there any function in webview which only retrieves the text shown in the URL to the user?
Also take help from here: https://developer.android.com
http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29
Get text content from a webview
Here is the code
strong text
package test.android.webview;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class WebviewTest2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webView);
TextView contentView = (TextView) findViewById(R.id.contentView);
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
private TextView contentView;
public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}
#SuppressWarnings("unused")
public void processContent(String aContent)
{
final String content = aContent;
contentView.post(new Runnable()
{
public void run()
{
contentView.setText(content);
}
});
}
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
}
});
webView.loadUrl("http://MIH.blogspot.com");
}
}
file.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" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
<TextView
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5" />
</LinearLayout>

Webview progressBar not hiding after page load

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.

adjusting dimensions of alert in android

I am making a basic app which has got an alert which would show up at the time the app is started. This alert has got a WebView in it and it covers almost the whole screen. Following are the questions I have in mind.
Now is it possible to adjust the dimensions of this alert ?
The url in the WebView in alert is set to 'http://www.google.com' let's say if some users are using my app and now I want them(users) to see a different webpage in the alert so how do I change the url without having them(users) update the app, how can I do it ?
MainActivity.java
package com.example.android.two;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import com.example.android.two.R;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Notification");
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
}
}
Yes, you can set dimensions for this alert dialog, by using custom layout for the Alert Dialog.
Please elaborate your second point.
You can use Custom Dialog Layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:layout_width="60dp"
android:layout_height="match_parent"
android:text="GO"
android:id="#+id/go"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/urlText"/>
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/web">
</WebView>
</ScrollView>
</LinearLayout>
We will use WindowManager.LayoutParams to adjust the dimension or we can set the height and width for the linearLayout above.
The Activity Code will be:
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
WebView wv;
EditText url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert_dialog);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.gravity = Gravity.CENTER;
url=(EditText) dialog.findViewById(R.id.urlText);
wv=(WebView) dialog.findViewById(R.id.web);
wv.setWebViewClient(new MyBrowser());
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://www.google.com");
Button go=(Button)dialog.findViewById(R.id.go);
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
wv.loadUrl("http://"+url.getText().toString());
}
});
dialog.getWindow().setAttributes(lp);
dialog.show();
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Hope it Helps.
Cheers!

How to open two webviews in one screen using fragments

could any one help me in figuring out how to open two webviews in the same screen using fragments Android,each webview must display a certain web page for example :1-google,2,yahoo.
i've tried too many tutorials and samples .nothing works fine for me... :(
The main issue for me that conflicting my thoughts, is what to write in the fragment class to open a webView and what to write in the main activity that runs the whole app .
Thanks in advance for any help.. :)
here is my code that runs fine for the portrait mode with one screen and crashes on landscape mode:
package com.example.androidwebviewfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment1 extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_webfragment,container,false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
and here is the second fragment :
package com.example.androidwebviewfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Fragment2 extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://android-er.blogspot.com";
String myUrl;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate
(R.layout.layout_webfragment2,container,false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
And here is the main activity:
package com.example.androidwebviewfragment;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the .xml files are :
1-Fragment 1 :
<WebView
android:id="#+id/mywebview"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
2-fragment 2 :
</LinearLayout>
3-main xml layout:
<fragment
android:name="com.example.androidwebviewfragment.Fragment1"
android:id="#+id/myweb_fragment1"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:name="com.example.androidwebviewfragment.Fragment2"
android:id="#+id/myweb_fragment2"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
And this is the main.xml in the layout-land folder:
<?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="horizontal" >
<fragment
android:name="com.example.androidwebviewfragment.Fragment1"
android:id="#+id/myweb_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:name="com.example.androidwebviewfragment.Fragment2"
android:id="#+id/myweb_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
The error on land scape mode from log:
java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.androidwebviewfragment
/com.example.androidwebviewfragment.MainActivity}
:android.view.InflateException: Binary XML file line #12: Error inflating class fragment
In the fragment class you create view and returns it to main activity and in the mainactivity you create a fragmentadapter to bind fragments. See (http://developer.android.com/reference/android/support/v4/view/ViewPager.html) link for more details and see (http://www.vogella.com/articles/AndroidFragments/article.html) for examples.

Android app in eclipse

i've searched for days but cant find an answer, perhaps you guys can help.
I'm creating an android app in eclipse, it all works just one thing is bugging me.
this is my main.java:
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
View firstButton = findViewById(R.id.btn_rassen);
firstButton.setOnClickListener(this);
View secondButton = findViewById(R.id.button2);
secondButton.setOnClickListener(this);
}
// Process the button click events
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_rassen:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.test.Webscreen.URL,
"http://www.google.com/");
startActivity(j);
break;
case R.id.button2:
Intent k = new Intent(this, Webscreen.class);
k.putExtra(com.test.Webscreen.URL,
"http://notworkingurltotest.com");
startActivity(k);
break;
}
}
}
now when it calls the webview.java the page called shows up but not the buttons i created in the layout xml page. does anybody have any idea why this is?
your help is much appreciated!
ohw this is my webscreen.java
package com.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Webscreen extends Activity {
public static final String URL = "";
private static final String TAG = "WebscreenClass";
private WebView webview;
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webscreen);
this.getIntent().getExtras();
this.webview = (WebView) findViewById(R.string.webview);
String turl = getIntent().getStringExtra(URL);
Log.i(TAG, " URL = "+turl);
WebView webview = new WebView(this);
setContentView(webview);
final Activity activity = this;
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Bezig met laden...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Intent myIntent = new Intent();
myIntent.setClassName("com.test", "com.test.Main");
startActivity(myIntent);
Toast.makeText(activity, "Laden van onderdeel mislukt, probeer het later nog eens! ", Toast.LENGTH_LONG).show();
progressDialog.show();
}
});
webview.loadUrl(turl);
}
}
webscreen.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">
<!-- <1> -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="#+id/url" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:lines="1"
android:layout_weight="1.0" android:hint="http://"
android:visibility="visible" />
<Button android:id="#+id/go_button" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="go_button" />
</LinearLayout>
<!-- <2> -->
<WebView
android:id="#string/webview"
android:layout_width="fill_parent"
android:layout_height="0dip"
/>
</LinearLayout>
Looks like you're creating a second webview and then setting that as the content view, so your R.layout.webscreen is replaced.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webscreen);
this.getIntent().getExtras();
this.webview = (WebView) findViewById(R.string.webview);
String turl = getIntent().getStringExtra(URL);
Log.i(TAG, " URL = "+turl);
**WebView webview = new WebView(this);
setContentView(webview);**
.....
Edit:
I've just noticed something in your code, should the line that reads:
this.webview = (WebView) findViewById(R.string.webview);
Acutally be:
this.webview = (WebView) findViewById(R.**id**.webview);
Edit 2:
Just noticed another thing while I was making the project. The following in webscreen.xml:
android:id="#string/webview"
Should be:
android:id="#+id/webview"
Edit 3:
And another thing noticed in webscreen.xml:
android:layout_height="0dip"
Sure you want a 0 height?? ;-)

Categories

Resources