I am developing the application using PhoneGap. I cannot enable built in zoom in/out in the webview.
I used Following code in onCreate Function
WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setBuiltInZoomControls(true);
But it did not work.
And The Activity class is
activity_main.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Check if you don't have a ScrollView wrapping your Webview.
It seems ScrollView gets in the way of the pinch gesture.
To fix it, just take your Webview outside the ScrollView nd then use the same line:
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
For Cordova 5
This has changed slightly for Cordova 5.1 (I think it changed with 5.0 actually).
To enable Android zooming for Cordova 5, add these lines :
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
and these
WebView webView = (WebView) appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
A full sample of your src/com/YOURPACKAGE.java file:
package com.YOURPACKAGE;
import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
public class MainActivity extends CordovaActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
WebView webView = (WebView) appView.getEngine().getView();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
//settings.setDefaultZoom(ZoomDensity.FAR);
}
}
Use ==>>
web.getSettings().setBuiltInZoomControls(true);
before the line ==>>
web.loadDataWithBaseURL("Your Required params");
Related
I have a weird problem with Andriod WebView. My website's Humberger menu expands and closes fine in my mobile browser but on my Android WebView app when the menu is clicked it's not working (doesn't expand fully to show menu items).
I've already tried the following lines of code from various answers on StackOverFlow:
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
settings.setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
Mobile Browser Working Correctly:
Android WebView App Not Working Correctly
Here is my Andriod WebView App code:
package com.app.pvm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.MyWebView);
webView.setWebChromeClient(new MyChromeClient());
webView.setWebViewClient(new BrowserClient());
WebSettings settings = webView.getSettings();
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("https://test.app");
}
}
XML Code:
<?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=".MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MyWebView"/>
</LinearLayout>
BrowserClient Class code:
package com.app.pvm;
import android.graphics.Bitmap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
class BrowserClient extends WebViewClient {
public BrowserClient(){
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
Note: I'm using Andriod Studio for the WebView App.
your WebView should fit whole window, use match_parent
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/MyWebView"/>
wrap_content set for height makes this inner dropdown wrongly measured, in the meanwhile whole web content stretches WebView to size of its parent (LinearLayout) and became scrollable
I have a webpage that uses Firebase auth to register/login users. I decided to convert the webpage into an app by using Android Studio's webView feature. I've been able to load the web url but when i try to log in, it doesn't work. I don't think firebase auth is working here.
I'm a web dev and not an app developer but I'm following some online tutorials for converting websites to apps using WebView in Android Studio.
Is there any thing I need to add in order to make firebase work in web apps?
Android Studio Code (MainActivity.java):
package com.example.indianrobostore;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
WebSettings webSettings;
private String webURL = "my website url";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.myWebView);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(webURL);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
}
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=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myWebView"/>
</LinearLayout>
Any help would be appreciated. Thanks!
Okay, I went through a few articles and found I need to enable DOM Storage for this to work.
Code : webSettings.setDomStorageEnabled(true);
recently I started creating an Android app with WebView. The app has grown and I changed the activity with WebView to tabbed activity using fragments and ViewPager. And there started my issue. After some headaches I finally managed how to display proper fragments for each tab. Then I simply copied the code for WebView to one of the fragment but the fragment isn't showing the web page I want. Any help will be appreciated!
Here's the code:
fragment with webView
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewFragment extends Fragment {
public WebView mWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mWebView = (WebView) mWebView.findViewById(R.id.webview);
mWebView.loadUrl("google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return inflater.inflate(R.layout.fragment_bugtracker, container, false);
}
}
and 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"
tools:context=".Tabs$BugtrackerFragment">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Things to do like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_bugtracker, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return v;
}
I want to play this video in webview in android
http://www.youtube.com/embed/hLw8RZ10PLo?showsearch=0&autoplay=1&rel=0
But searched for it and i have implemented everything what i got
here is my code
package com.example.webapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebSettings.PluginState;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
final WebSettings.PluginState ON = webSettings.getPluginState().ON;
webSettings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");
webSettings.setPluginState(ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
webSettings.setAllowFileAccess(true);
myWebView.loadUrl("http://www.youtube.com/embed/hLw8RZ10PLo?showsearch=0&autoplay=1&rel=0");
}
#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;
}
}
and this is my xml
<WebView 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:id="#+id/webview" >
</WebView>
I am playing this video in my android 2.3.6 version and in my manifest file, I have used
android:harwareAccelerated="true"
But still my video is not running.
Can Anybody help me, I am stuck in this problem from yesterday.
Use Webview, Create a string for html tags, add iframe tage and pass the height and width of the video and url. and load the string in webview
<iframe width="420" height="345"
src="http://www.youtube.com/embed/XGSy3_Czz8k">
</iframe>
Add the string in loaddata of webview
webView.loadData(strHTMLTags, "text/html", "UTF-8");
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();