Im working on my first android app. Im having a very strange problem with WebView Component of Android. My website have responsive design. The problem is it resize fine on Emulator but when i run on my device webview show normal design, not responsive. Any idea? this is my code:
MainActivity.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView myWebView;
#SuppressWarnings("deprecation")
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.loadUrl("http://apelarse.com.ar");
myWebView.setWebViewClient(new myWebViewClient());
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(myWebView.canGoBack()){
myWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
Main.xml
<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=".MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
I copy the code from this site: http://samadmalik.com/converting-website-android-app/
Try something like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
I hope it should work (even if webview parameters at first glance look a bit strange).
You might have to add this:
webview.getSettings().setUseWideViewPort(true)
Related
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
I am creating an Android application to work as a simple browser. The user enters a web address and then clicks a button. After this, the application should the website in the WebView. However, after the button is clicked, a notification pops up asking me to choose a browser to complete my action. [The options in the pop up are my default browser, mozilla, etc.]
However, I do not want to use any of them. What should I do ?
This is my .java file:
package com.example.all_in_one;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class SimpleBrowser extends Activity implements OnClickListener{
WebView myBrowser;
EditText URLaddress;
Button Go;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_browser);
myBrowser = (WebView) findViewById(R.id.WV);
URLaddress = (EditText) findViewById(R.id.webaddress);
Go = (Button) findViewById(R.id.go);
Go.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.go:
String address = URLaddress.getText().toString();
myBrowser.loadUrl("http://" + address);
break;
}
}
}
This is my .xml file:
<?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" >
<LinearLayout
android:id="#+id/LL1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<EditText
android:id="#+id/webaddress"
android:inputType="textNoSuggestions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<Button
android:id="#+id/go"
android:text="Go"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
/>
</LinearLayout>
<WebView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/WV"
android:layout_below="#+id/LL1" />
</RelativeLayout>
I have added the Internet permission in my Manifest file.
Please help. Thank you in advance.
You have to pass the WebViewClient class object to browser.setWebViewClient(new BrowserClient()) to open the links in the webview.
private class BrowserClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(tag, "shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
}
and in your onCreate() call like this
myBrowser.setWebViewClient(new BrowserClient());
can you please tell how to remove caching in webview in android. I open one url there one textfield in that I write
come text on that field and go next page .then come to same page write same thing it will open autosuggest (like thing which text I already used previously).
Thanks
here is my code
package com.firstgroup.ui;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ApplicationLoad extends Activity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new MyBrowser());
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.loadUrl("http://192.168.11.90/loginuser/login");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".ApplicationLoad" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Try by putting this in OnCreate():
//context is Activity context
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
Also try this:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
Try following methods of Webview,
mWebView.clearFormData();
mWebView.clearCache(true);
And to remove the EditText's border you need to declare following property in your .xml file.
android:background="#null"
try this for not displaying autosuggetion
mWebView.clearFormData();
mWebView.getSettings().setSavePassword(false);
mWebView.getSettings().setSaveFormData(false);
// turn caching off
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
and use LOAD_CACHE_NORMAL for default behavior.
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 am developing an android game application,I have implemented all the screens.Now i want to change the webview background color,Can anybody guide me.Here is my xml file
<?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"
android:background="#drawable/background">
<WebView
android:id="#+id/webbrowser"
android:layout_width="fill_parent"
android:layout_height="345px"
android:layout_marginTop="46px"/>
<Button
android:id="#+id/Btn"
android:background="#drawable/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="109px"
android:layout_marginTop="37px">
</Button>
</LinearLayout>
And My Java file is
package com.tli.roadtripbingo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class WebView1 extends Activity {
private Button Back;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview);
Back = (Button)findViewById(R.id.back);
WebView webView = (WebView) findViewById(R.id.webbrowser);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.vikingredning.no/skilt.aspx");
webView.setWebViewClient(new HelloWebViewClient());
}
class HelloWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
};
}
Thanks in advance
Regards
Tushar
You can find answer here Change Background color and font color
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.setBackgroundColor(Color.parseColor("#123456"));
You can make the WebView transparent like this:
WebView webView = (WebView) findViewById(R.id.webView);
webView.setBackgroundColor(Color.TRANSPARENT);
<LinearLayout
android:id="#+id/web_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/web_bg_color"
android:gravity="center" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.setBackgroundColor(Color.TRANSPARENT);
mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
It worked for me in lollipop also.
try it once!
You may also want to reload the page after changing colors by using the reload() method:
webView.reload();