I'm a beginner in Android Development. So I'm searching online for tutorials to learn about.I got this code that I was trying on my computer, when I run the code and view the App, it says that the webpage is not available,so I added the manifest file, but it still says the same thing
The code is given below:
package com.paresh.webviewclientdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewClientDemoActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://www.google.com");
}
public 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;
}
}
}
layout:
<?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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Here's the manifest file:
<users-permission android:name="permission.INTERNET" />
But even after this,the Android web view webpage does not load, I cannot find the error. Help me find it. Do I need to add something else, or is there an error in the existing files that I have given
try these codes instead.
class:
public class WebViewClientDemoActivity extends Activity {
private WebView webView;
private String url = "https://www.google.com";
private ProgressBar progressBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl(url);
}
public 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;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressBar.setVisibility(View.GONE);
}
}, 10000);
}
}
main.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/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="false"/>
</RelativeLayout>
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'm trying to open WebView on button click. I want to show the progress bar and set a default view in WebView. What changes should I make?
Also if web view can take progress bar's space?
Below is my code.
xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.dell.ca1.Birthdays">
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/background_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:color/background_dark"
android:orientation="horizontal">
<Button
android:id="#+id/archies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="Archies >>"
android:textSize="10sp" />
<Button
android:id="#+id/fern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="FernsNPetals >>"
android:textSize="10sp" />
<Button
android:id="#+id/meeta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="Gifts By Meeta >>"
android:textSize="10sp" />
</LinearLayout>
</HorizontalScrollView>
<ProgressBar
android:id="#+id/pb"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#id/horizontalScrollView" />
<WebView
android:id="#+id/webview_bday"
android:layout_width="match_parent"
android:layout_height="525dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/horizontalScrollView" />
</LinearLayout>
Also if I can retain the data even after the back is pressed and start from beginning URL only when the app restarts.eg when I click "archies" button, view a product and then click "fernNpetals", then I click "archies" button, then I want to view the product page instead of the main page.
java file
package com.example.dell.ca1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
public class Birthdays extends AppCompatActivity {
Button btn_fnp,btn_archies,btn_meeta;
WebView webView;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birthdays);
webView=(WebView)findViewById(R.id.webview_bday);
btn_archies=(Button)findViewById(R.id.archies);
btn_fnp=(Button)findViewById(R.id.fern);
// btn_igb=(Button)findViewById(R.id.igb);
btn_meeta=(Button)findViewById(R.id.meeta);
//progressBar=(ProgressBar)findViewById(R.id.pb);
OnClickButton();
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setJavaScriptEnabled(true);
}
private class MyBrowser extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
void OnClickButton()
{
btn_archies.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
webView.loadUrl("https://www.archiesonline.com/");
}
});
btn_meeta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
webView.loadUrl("https://www.giftsbymeeta.com/");
}
});
btn_fnp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
webView.loadUrl("https://www.fnp.com/");
}
});
}
}
Thanks!
You can use separate class for webViewClient
For Example,
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressLoading.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
progressLoading.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
progressLoading.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
progressLoading.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
}
}
Then call to webview,
myWebView.setWebViewClient(new MyWebViewClient());
Please modify as you like!!
Happy java!!
Please Try like this...
btn_archies.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
wv1.setWebViewClient(new MyBrowser());
wv1.loadUrl(url);
}
});
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog_webview = new ProgressDialog(Ppt_viewclass.this, R.style.MyTheme);
progressDialog_webview.setMessage("Please wait ...");
progressDialog_webview.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (progressDialog_webview != null) {
progressDialog_webview.dismiss();
}
}
}
I hope this will help you.....,All the best
I am trying to add a loading spinner to show while the webpage loads.
Here is my code:
package com.wEgyptianpost;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.TextView;
import java.util.Random;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.wEgyptianpost.R;
public class MainActivity extends Activity {
WebView view;
SwipeRefreshLayout mySwipeRefreshLayout;
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) findViewById(R.id.admob_id);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mWebView.reload();
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
mWebView.stopLoading();
swipeRefreshLayout.setRefreshing(false);
}
},10000);
}
});
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
// Use remote resource
mWebView.loadUrl("http:google.com");
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
// Use local resource
//mWebView.loadUrl("file:android_asset/web/google.html");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my activity xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipelayout"
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=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout >
<com.google.android.gms.ads.AdView android:id="#+id/admob_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-1851250777225639/8300259410"
android:layout_gravity="center_horizontal|bottom" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:layout_gravity="center" />
</FrameLayout>
I added progress bar to the activity xml but I dont know how to link it to the main activity code.
You can add it with setting a webclient
for
SET
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
} );
To avoid Ajax calls
Call this in activity
MyClient client=new MyClient();
mWebView.setWebViewClient(client);
client.setFirsttime();
and create a class in activity like this
public void MyClient extends WebViewClient(){
private boolean oneTime=false;
public void setFirsttime(){
oneTime=true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(oneTime){
progressBar.setVisibility(View.VISIBLE);
onetime=false;
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(final WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
}
On Ajax call onPageStarted called multiple times hence progressBar will not get hidden.
Note: you have to call client.setFirsttime(); every time when you are using mWebView.loadUrl();
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar2.setProgress(newProgress);
//newProgress maybe is 1~100
super.onProgressChanged(view, newProgress);
}
you can try this,
public class MainActivity extends Activity {
WebView view;
SwipeRefreshLayout mySwipeRefreshLayout;
private ProgressBar progressBar;
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) findViewById(R.id.admob_id);
progressBar= (ProgressBar)findViewById(R.id.progressBar2);
progressBar.setVisibility(View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mWebView.reload();
swipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
#Override
public void run() {
mWebView.stopLoading();
swipeRefreshLayout.setRefreshing(false);
}
},10000);
}
});
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
// Use remote resource
mWebView.loadUrl("http:google.com");
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
// Use local resource
//mWebView.loadUrl("file:android_asset/web/google.html");
progressBar.setVisibility(View.GONE);
}
I've got an app, that has 2 activity, the first one launch the second to load a url into a webview.
It works, but while the url is loading , the webview appear empty... then i want to make a splash screen or something like this, to show it while the url is loading, I did that in a new activity, but i don't know what can i do to close the third activity when the url is loaded... Please can anybody help me?
This is my code...Thank you!
public class Visor extends Activity {
WebView mWebView;
int Result;
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.visor);
Bundle extras=getIntent().getExtras();
String s= extras.getString("url");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl(s);
mWebView.setWebViewClient(new VisorClient());
mWebView.getSettings().setBuiltInZoomControls(true);
}
private class VisorClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
lanzarIntro();
}
#Override
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl(url);
}
}
public void lanzarIntro(){
Intent i=new Intent (this, Intro.class);
startActivity(i);
}
}
I do it by initially showing an ImageView and then once the WebView has loaded, swapping their visibility like this
WebView wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
...
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView1).setVisibility(View.VISIBLE);
}
});
wv.loadUrl("http://yoururlhere.com");
And my xml layout looks like this
<ImageView android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/vert_loading"
/>
<WebView android:id="#+id/webView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
I have one activity. 1 xml file and 1 java class. Inside xml file i have:
WebView
ImageView, logo of my application,
ProgressBar and
TextView, app version .
Code of main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:background="#aaaaaa"
a:orientation="vertical" >
<WebView
a:id="#+id/webView1"
a:layout_width="fill_parent"
a:layout_height="fill_parent" />
<ImageView
a:id="#+id/imageView1"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentTop="true"
a:layout_centerHorizontal="true"
a:layout_marginTop="46dp"
a:src="#drawable/logo" />
<ProgressBar
a:id="#+id/progressBar1"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_below="#+id/imageView1"
a:layout_centerHorizontal="true" />
<TextView
a:id="#+id/textView1"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentBottom="true"
a:layout_alignParentRight="true"
a:layout_marginBottom="13dp"
a:layout_marginRight="13dp"
a:text="version 1.0"
a:textAppearance="?android:attr/textAppearanceSmall"
a:textColor="#444444" />
</RelativeLayout>
Code of NovcanikActivity.java:
package zm.Nocanik;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class NovcanikActivity 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.webView1);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setSaveFormData(false);
websettings.setSavePassword(false);
webview.loadUrl("http://m.novcanik.net/?appvers=1.0");
webview.setHorizontalScrollBarEnabled(false);
webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setBackgroundColor(128);
webview.setWebViewClient(new NovcanikWebViewClient());
webview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
public void visible(){
WebView webview = (WebView) findViewById(R.id.webView1);
ImageView logo = (ImageView) findViewById(R.id.imageView1);
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
TextView version = (TextView) findViewById(R.id.textView1);
webview.setVisibility(10);
logo.setVisibility(0);
bar.setVisibility(0);
version.setVisibility(0);
}
public void unvisible(){
WebView webview = (WebView) findViewById(R.id.webView1);
ImageView logo = (ImageView) findViewById(R.id.imageView1);
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
TextView version = (TextView) findViewById(R.id.textView1);
webview.setVisibility(0);
logo.setVisibility(10);
bar.setVisibility(10);
version.setVisibility(10);
}
private class NovcanikWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url){
webview.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
view.loadUrl("file:///android_asset/noconnection.html");
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
visible();
}
#Override
public void onPageFinished(WebView view, String url) {
unvisible();
}
}
}
Sorry for no description. If there would be need for description, i will describe in detail the entire code.
Just use mWebView.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); for your mWebView
My problem is that the webpage is not loaded inside the WebView.
mWebview.loadUrl("http://www.google.com"); launches the web browser...
This is the code of my activity:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.loadUrl("http://www.google.com");
setContentView(mWebview);
}
}
I added the required permission in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
Thanks to this post, I finally found the solution. Here is the code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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("http://www.google.com");
setContentView(mWebview );
}
}
try this
webviewlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/help_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
/>
In your Activity:
WebView webView;
setContentView(R.layout.webviewlayout);
webView = (WebView)findViewById(R.id.help_webview);
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
Update
Add webView.setWebViewClient(new WebViewController()); to your Activity.
WebViewController class:
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
webView.setWebViewClient(new WebViewController());
Please use this code:-
Main.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/background">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#drawable/top_heading"
android:id="#+id/rlayout1">
<TextView android:layout_width="wrap_content"
android:layout_centerVertical="true" android:layout_centerHorizontal="true"
android:textColor="#ffffff" android:textSize="22dip"
android:textStyle="bold" android:layout_height="wrap_content"
android:text="More Information" android:id="#+id/txtviewfbdisplaytitle" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="#+id/rlayout1"
android:id="#+id/rlayout2">
<WebView android:id="#+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
</RelativeLayout>
MainActivity.Java
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
Try this code if any query ask me.
It's very simple try integrate these lines of code
first take permission in the Android Manifest file
<uses-permission android:name="android.permission.INTERNET" />
then write some code in you Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/help_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Then write these code in your MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity{
private WebView mWebview ;
String link = "";// global variable
Resources res;// global variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_modernherbal_main);
mWebview = (WebView) findViewById(R.id.help_webview);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.example.com");
}
}
Try this it'll help you to solve your problem
just go into XML file and give id to your webView then in java paste these line:
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_layout_file_name);
mWebview = (WebView)findViewById(R.id.id_you_gave _to_your_wenview_in_xml);
mWebview.loadUrl("http://www.google.com");
}
}
I used this code that was cool. but have an error. " neterr_cleartext_not_permitted"
show when you use this code then you will face this problem..
I got a solution of this.you have to add this in your AndroidManifest.xml near about Application
android:usesCleartextTraffic="true"
<uses-permission android:name="android.permission.INTERNET" /> // ignore if you already added. outside of Application.
You can do like this.
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("Your URL goes here");
try this;
webView.loadData("<iframe src='http://www.google.com' style='border: 0; width: 100%; height: 100%'></iframe>", "text/html; charset=utf-8", "UTF-8");
Add Internet permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
In your Layout:
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
/>
In your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
progressDialog = new ProgressDialog(this);
url_Api = "https://learn.microsoft.com/en-us/learn";
webView = this.findViewById(R.id.webView);
progressDialog.setMessage(getString(R.string.connection_Wait));
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
LoadUrlWebView( url_Api );
}catch (Exception e){
Log.w(TAG, "onCreate", e);
}
}
private void LoadUrlWebView( String url_api ) {
try {
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new MyWebChromeClient( url_api ));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.loadUrl(url_api);
} catch (Exception e) {
Log.w(TAG, "setUpNavigationView", e);
}
}
private class MyWebChromeClient extends WebChromeClient {
private String urlAccount;
public MyWebChromeClient( String urlAccount ) {
this.urlAccount = urlAccount;
}
#Override
public void onProgressChanged(WebView view, int newProgress) {
try {
//Tools.LogCat(context, "INSIDE MyWebChromeClient | onProgressChanged / newProgress1:" + newProgress);
progressDialog.setMessage(newProgress + "% " + getString(R.string.connection_Wait));
if (newProgress < 100 && !progressDialog.isShowing()) {
if (progressDialog != null)
progressDialog.show();
}
if (newProgress == 100) {
if (progressDialog != null)
progressDialog.dismiss();
}
}catch (Exception e){
Log.w( "onProgressChanged", e);
}
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
sharedPreferences = new Shared_Preferences( context );
sharedPreferences.setPageWebView(view.getUrl());
}
}
Add WebView Client
mWebView.setWebViewClient(new WebViewClient());
You need to add WebView client
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
also you can use onPageFinished to do task after webview done loading web page
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">
<WebView
android:id="#+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView .loadUrl("http://www.google.com");
webView.setWebViewClient(new MyWebViewClient());
}
}
AndroidManifest.xml: (add uses-permission and android:usesCleartextTraffic)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Simple Webview activity in kotlin :
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent.getStringExtra(URL_EXTRA)?.let { url ->
WebView(this).apply {
settings.javaScriptEnabled = true // check if your need this
settings.domStorageEnabled = true
settings.setSupportZoom(true)
settings.builtInZoomControls = true
settings.displayZoomControls = false
loadUrl(url)
setContentView(this)
}
}
}
companion object {
const val URL_EXTRA = "url"
}
}
Add below method in your activity class.Here browser is nothing but your webview object.
Now you can view web contain page wise easily.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
browser.goBack();
return true;
}
return false;
}