android webView zoom in out problems - android

I am developing an app and there is webview tab, on it i give zoom in out from .getSettings().setSupportZoom(true);. However when i zoom out (the view gets smaller and smaller until minus button(zoom out)disabled itself)the webview showing white background that i don't even set a background on the layout. how to make webview on zoom out have some resolution which will fit the height of the screen? and I am curious if we can make the zoom controls always seen?(it only appear whenever we touch the screen and make some move on it(tap, hold and move))
here is my java code from webview
public class WebViewActivity extends Activity {
private WebView webView;
//private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
#Override
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.somewebsitename.com");
}
}
and here is my xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
thanks in advance

Try this
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);

Related

Am trying to Use button to Load html file from asset folder

Am trying to Use button to Load html file from asset folder
but after clicking the button the html file refuse to load
Activity_main.xml
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="open"
android:text="lawal"
android:textSize="20dp" />
<WebView
android:layout_width="368dp"
android:layout_height="495dp"
android:id="#+id/webView"
/>
Main.java
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view) {
webView = (WebView)findViewById(R.id.webView);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///Android_asset/1.html");
setContentView(view);
}
}
android_asset should be in lower case.Change your button onClick() code as below. here test.html should be replaced by your file name.
you need to create assets folder inside main folder.
WebView webView = view.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///android_asset/test.html");
Initialize the webview in oncreate after setting the content view
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
and then load your url in open function
webView.loadUrl("file:///android_asset/1.html");

Android 8: WebView - vertical scrollbar - incorrect work

I has activity with webView. Here layout xml:
<?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">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here activity code:
public class TestActivity extends AppCompatActivity {
public static final String HTML_TEXT = "HTML_TEXT";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
String htmlText = getIntent().getExtras().getString(HTML_TEXT);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String resultHTML = "<html><head><meta charset=utf-8></head><body>" + htmlText + "</body></html>";
webview.loadDataWithBaseURL("fake://", resultHTML, "text/html", StringUtil.encodingUTF8, null);
}
}
And here result:
And as you can see on right side success show scrollbar when scrolling.
This work on Android 4.0, 5.0, 6.0, 7.0. OK.
But on Android 8.0 (Emulator) when scrolling to item "Test many posts 7/15" (about half screen) scrollbar is hide. I'm scrolling to bottom of screen but scrollbar not show.
Here result:
and scrolling to bottom of screen (scrollbar is hide):
How fix this?
in your onCreate() method try this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_web_view_layout);
browser = (WebView)findViewById(R.id.your_webview);
WebSettings mWebSettings = browser.getSettings();
mWebSettings.setBuiltInZoomControls(true);
browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
browser.setScrollbarFadingEnabled(false);
browser.loadUrl("file:///android_asset/index.html");
}

Android WebView density issue

Got some problems with the WebView class. I've got a WebView inside a RelativeLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webViewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00"
android:orientation="vertical" >
<WebView
android:id="#+id/mWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#0000FF"/>
<Button
android:id="#+id/adminBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Admin" />
</RelativeLayout>
The WebView is contained in a Fragment, code is this
public class WebViewFragment extends Fragment {
private WebView webView;
private Button adminBtn;
private String url = "http://www.my-local.guide"
#SuppressLint("SetJavaScriptEnabled")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.webview_fragment_layout,
container, false);
webView = (WebView) rootView.findViewById(R.id.mWebView);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setInitialScale(120);
webView.setWebViewClient(new WebViewClient() {
});
webView.loadUrl(url);
adminBtn = (Button) rootView.findViewById(R.id.adminBtn);
adminBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCheckPasswordDialog();
}
});
return rootView;
}
public static WebViewFragment get() {
WebViewFragment fr = new WebViewFragment();
return fr;
}
private void showCheckPasswordDialog() {
CheckPasswordDialog checkPasswordDialog = CheckPasswordDialog.get();
checkPasswordDialog.show(getFragmentManager(), SHOW_PASSWORD_DIALOG);
}
}
It seems like the WebView scales his width to 960px. I write a js script on the page and it tells me that page's width is 960px.
I need to set a larger page's width to use Bootstrap as framework in my embedded websites.
Using google.com as url anyway I can get all the screen's width, so I'm guessing if I have to use specific css rules for 960ps width or if I can modify the WebView's width
thank you for your time
Finally I fix it, I was adding to many functions that was overriding themselves
webView = (WebView) rootView.findViewById(R.id.mWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setInitialScale(100);
this code works

How to embed an infographic iframe in android's webview

I am new to android development and i'm doing a an app where i need to embed an iframe in android's webview..
iframe sample is
<iframe src="//e.infogr.am/chart-61108188168" width="550" height="680" scrolling="no" frameborder="0" style="border:none;"></iframe>
Can you just set up a simple webview pointing to the iFrames url like so?
Create a webview in the xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Then in your code set it up as:
public class WebActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webcontent);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://websitesby.projectcaruso.com/");
}
}

Web View Zooming Control

htmlp = URLEncoder.encode(desc,"utf-8").replaceAll("\\+"," ");
WebView wv=(WebView)findViewById(R.id.webView1);
wv.setInitialScale(70);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadData(htmlp,"text/html", "utf-8");
In htmlp contains HTML content(tags). Now I have to enable zooming control, but using above code it is not working. Is anything I have to enable in xml part of the webview.
Thanks in advance
This works good in my case.Try it for your case..
In manifest file..
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
webview.xml
<?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"
>
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
In activity..
public class Main extends Activity {
private WebView myWebView;
private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.webview);
this.myWebView = (WebView) this.findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.myWebView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
this.myWebView.loadUrl("http://www.facebook.com");
}
}
wv.getSettings().setBuiltInZoomControls(false);
change to false
At last I have solved this problem. The problem was in the design section. Just changed the layout param to this:
WebView
android:layout_width="match_parent"
android:layout_height="match_parent"

Categories

Resources