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 );
Related
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...
I have a webview. My question is, when user clicks to a link in webview, how to continue with my app?
When user clicks to a link, this dialog box appearing:
How can I avoid this dialogbox and continue with my app?
WebView Code:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(url);
Edit (MYActivity İn Test project)
package com.mycompany.myapp5;
import android.app.*;
import android.os.*;
import android.webkit.*;
public class MainActivity extends Activity
{
WebView view;
String url="http://google.com";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
}
This SO answer is exactly what you need.
The idea is to set a WebViewClient to your WebView and override shouldOverrideUrlLoading method like below:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
I hope this helps you!
Here is the complete code from a test project that is running.
The xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.test.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
Code in the Activity:
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
String url="http://www.google.com";
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
This works fine in a 4.4 device. Please try this and let me know if you still have the issue.
I have two activities in my android project. Both contents WebView and load same html(which include some javascript code also) file from assets folder.
When I load WebView in first activity it works fine. But after navigating to second activity, same html file doesn't load in WebView.
Activity 1:
public class Activity_1 extends AppCompatActivity {
WebView webView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__1);
webView = (WebView) findViewById(R.id.webview1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity_1.this, Activity_2.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.loadUrl("file:///android_asset/sample.html");
}
#Override
protected void onPause() {
super.onPause();
}
}
Activity 2:
public class Activity_2 extends AppCompatActivity {
WebView webView;
ProgressDialog progressDialog ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
webView = (WebView) findViewById(R.id.webview2);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
}
#Override
protected void onResume() {
super.onResume();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
if (Build.VERSION.SDK_INT >= 19)
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
else
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressDialog.setProgress(newProgress);
}
});
webView.loadUrl("file:///android_asset/sample.html");
progressDialog.show();
}
}
activity_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next"
android:textSize="22sp"/>
</LinearLayout>
activity_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
When I open second activity, I get stuck as,
I don't understand that same html is loading properly in first Activity, but not in the second.
I also tried with clear WebView cache in first activity and destroying WebView in onDestroy of activity 1.
To load html, I tried with following methods:
webView.loadUrl("file:///android_res/raw/sample.html");
OR
Access html from storing as string resource
webView.loadDataWithBaseURL(null, getResources().getString(R.string.html), "text/html", "UTF-8", null);
Edit:
I tried with uninstalling webview updates and its working fine.
Is there any problem with webview updates in android 5.0?
Please let me know what I am doing wrong?
I've done a similar project to try your code. It's working for me :S
Maybe you can try to place you file in src/main/assets and use this line
webView.loadUrl("file:///android_asset/sample.html");
But in my case is working with two options. ¿Can you provide more info about html file please?
This is my first question here. I know that this question has been asked before, but I didn't find an answer/solution that really explains the answer for a totally newbie like me.
I am creating an app with a linear layout that has a lot of buttons, each button should drive the user to a different web page. The buttons works well and every buttons goes to its specific web page, but in the default browser, not within the app.
This is my webview.xml file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
This is the WebViewActivity.java file:
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(
"http://egy-tech-droid.blogspot.com.eg/search/label/%D8%AA%D8%B7%D8%A8%D9%8A%D9%82%D8%A7%D8%AA%20%D8%AD%D8%B5%D8%B1%D9%8A%D8%A9");
}
I added the internet permission in the Manifest file:
<uses-permission android:name="android.permission.INTERNET" />
This opens the web page but in the default browser of the device and I want it to open inside my app. Any help? (please give me a detailed answer/explanation)
Add this to your code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
You need to set up a WebViewClient in order to override that behavior (opening links using the web browser).
Use this;
webview.setWebViewClient(new WebViewClient());
Android documentation says:
public void setWebViewClient (WebViewClient client)
Sets the WebViewClient that will receive various notifications and
requests. This will replace the current handler.
Enjoy full code :
Oncreate () :
webView = (WebView) findViewById(R.id.webView1);
if(Constants.isNetworkAvailable(mContext)){
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() );
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setInitialScale(30);
webView.loadUrl(url);
}else{
Toast.makeText(mContext, Constants.msgNoInternet, Toast.LENGTH_LONG).show();
}
MyWebViewClient :
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pDialog.isShowing()) {
pDialog.show();
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//view.loadUrl(url);
System.out.println("on finish");
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
Kotlin version of Sunny's answer
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
}
You can't talk about the Web nowadays without considering Javascript. By default, its use in a WebView is not active. To enable Javascript just insert these lines of code:
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
I hope this will help you.
In your web view layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#c7bbac">
<ImageView
android:id="#+id/txtmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/topbar50" />
<ImageView
android:id="#+id/backbutn"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:paddingTop="2dp"
android:src="#drawable/backbtn" />
</RelativeLayout>
<WebView
android:id="#+id/webView1"
android:layout_below="#+id/relay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Webview Button Onclick:
webbutton = (ImageView) findViewById(R.id.web);
webbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
startActivity(intent);
}
});
Webview Activity:
public class WebViewActivity extends Activity {
private WebView webViewurl;
ImageView back;
AndroidInterface AMW = AndroidInterface.GetInstance();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
back = (ImageView) findViewById(R.id.backbutn);
webViewurl = (WebView) findViewById(R.id.webView1);
webViewurl.getSettings().setJavaScriptEnabled(true);
webViewurl.getSettings().setBuiltInZoomControls(true);
final Activity activity = this;
webViewurl.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
webViewurl.loadUrl("http://example.com");
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
webView = (WebView) findViewById(R.id.youtubelink);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("your url");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.co.in");
this code works fine...
Above code opens the link in your App.
Ok, so I've got my app done, and I'm working on minor tweaks for it, and one thing is I would prefer my web links to launch in webview instead of the stock browser... I've tried so many things and I keep getting errors and eclipse keeps launching the debug perspective and I'm stuck..
// XXXX.edu Button
Button XXXX_Button = (Button)findViewById( R.id.XXXX_Button );
XXXXXX_Button.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
Uri uri = Uri.parse( "http://www.XXXXXX.edu/" );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
}
});
You need to extend WebViewClient, and launch the url within that.
public class WebActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = (WebView) findViewById(R.id.wv);
webview.setWebViewClient(new WebC());
webview.loadUrl(baseUrl);
}
public class WebC extends WebViewClient
{
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
... etc.
And in your layout xml,
<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/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>