Hamburger Menu not working in Android WebView - android

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

Related

Firebase authentication not working when I use my website as a web app in android studio

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);

Android Webview scroll is misbehaving when loading a flash website

I am opening a simple flash based website in my webview using following code:
Java Code:
package com.sample.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class SampleWebViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webView = (WebView) findViewById(R.id.webview);
final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("loading");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progress.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.dismiss();
}
});
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("http://www.fusioncharts.com/demos/features/#linkedcharts-for-easy-drill-down");
}
});
}
}
Main.xml:
<?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" >
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Load URL" />
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp" />
</LinearLayout>
Now when the page is loaded completely and i am trying to scroll down to view rest of the content, the webview is coming on top of "Load URL" button. Check the screenshot:
while it should look like:
Please suggest, what should i do so that it works correctly.
Edit: I am using a Galaxy Note(OSv2.3.5) with the latest version of both Flash Player and Adobe Air installed.
I have also tried this things but I think the only solution to do this is by using only a single webview in your xml, so, you don't have other native controls.
This is really broken on Android. If you try to use the Build in topbar it will also go behind the flash. I'd really recommend you go away (far away) from flash. There is some discussions about it on google groups and some on stackoverflow but all ends without any good results. The only good suggestion I found was using flash to cover other flash components =/

Not opening in WebView but in browser

I want to open the following link in WebView
https://tickets.musiconelive.com/admin/SACValidateBarcode.asp
I am using following code to do that
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://tickets.musiconelive.com/admin/SACValidateBarcode.asp");
but it's not opening in WebView and instead is opening in the browser.
How can I fix this problem?
may this helps you
WebSettings mWebSettings;
WebView mWebView = (WebView)findViewById(R.id.services_detail_magnified_image);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
mWebView.setBackgroundColor(Color.TRANSPARENT);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
mWebView.loadUrl(StaticURL.uChangePassword);
mWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
//show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl (url);
return true;
}
}
i think this will help you.
package com.adySol;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class adySol extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="http://tickets.musiconelive.com/admin/SACValidateBarcode.asp";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(PluginState.ON);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(url);
}
}
Main.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"
android:background="#android:color/white"
>
<WebView android:id="#+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent"></WebView>
</LinearLayout>
Manifest permission :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
You need to set WebViewClient()
This is example in kotlin
webview.apply {
loadUrl("https://www.google.com/")
webViewClient = WebViewClient()
}
As per android documentation
public void setWebViewClient (WebViewClient client)
Sets the WebViewClient that will receive various notifications and requests. This will replace the current handler.
webView=findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR)
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(MyWebViewClient());
webView.loadUrl("https://google.com/");
class MyWebViewClient extends WebViewClient() {
#override
boolean shouldOverrideUrlLoading(WebView view , WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request)
}
}

Opening webview not in new browser

I am implementing a webview. I want two buttons on the top of the web page.
I have one vertical linear layout, inside of which is one horizontal layout with two buttons, and one webview outside of the horizontal layout.
I am simply loading the Google URL in Java code.
Every time I run the application, it opens a new browser on top of the application and the buttons get hidden. It's not showing the buttons above the webview.
Please help and tell me how can I load a URL in the webview without opening another browser, or how I can prevent it by opening a native browser, so that the page is loaded in the webview itself and not a new browser.
Thanks all
Ya. You must implement WebViewClient class and Override shouldOverrideURLLoading() method in this class.
Why ? Because webview just open your "exactly link", if that link redirect other links, android will open default browser for this action.
In your example, as you know, when you connecting to google.com google will redirects to google at your country. Example, if you are in China, google will go to google.com.cn, if in Vietnam, will be google.com.vn.
Here is my simple example: (you can imagine this is an new browser, :laugh)
First is layout xml file:
<?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" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<EditText
android:id="#+id/url"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="Input URL"/>
<Button
android:id="#+id/run"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0"
android:text="GO"/>
</LinearLayout>
<WebView
android:id="#+id/webview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
Here is code of main activity:
package com.basic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class WebViewExample extends Activity{
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview);
Button button = (Button) findViewById (R.id.run);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
gotoPage();
}
});
}
private void gotoPage(){
EditText text = (EditText) findViewById(R.id.url);
String url = text.getText().toString();
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webView.setWebViewClient(new Callback()); //HERE IS THE MAIN CHANGE
webView.loadUrl(url);
}
private class Callback extends WebViewClient{ //HERE IS THE MAIN CHANGE.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
}
Hope this help you :)
Adding the following code before loadUrl() will solve this problem,
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
The shouldOverrideUrlLoading() from WebViewClient does this job. Here goes the Android doc for shouldOverrideUrlLoading,
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url...
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29
My XML implementation:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:background="#android:color/transparent"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
My Java implementation:
WebView webView;
public final String GlobalUrl = "http://slashdot.org/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_application_activity);
webView = (WebView) findViewById(R.id.webView);
loadWebViewLoad(webView);
}
private void loadWebViewLoad(WebView webview) {
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setSupportMultipleWindows(true);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl(GlobalUrl);
}
And final result is:
The layout should something similar to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/button1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"/>
<Button android:id="#+id/button2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1" />
</LinearLayout>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
And you can also refer to the Need help changing from stock android browser to webview to see if you are launching the url correctly.
I had the exact same problem and fortunately after browsing the web for about an hour I mixed some of the things I found and it worked.
this is the code:
WebView webView;
webView = ((WebView) rootView.findViewById(R.id.detail_area));
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(mItem.link);
where "detail_area" was my webview, rootView was my selected item inside a "Master/Detail Flow", link was the URL I wanted to open.
I tried this. its working for me. it does not open new window. it will open webview page only. its hiding for new browser asking window open..
private WebView webView;
String strUrl="url" ;
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(strUrl);
webView.setWebViewClient(new WebViewClient());
Add the below line in your xml file which having webview
tools:context=".MyActivity" (name of your activity)
This might be a late post but it might help other developers...
You need to set setWebViewClient on webview before loading the URL on it like below,
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Some background of the above code, Documentation states in literals words like below about the shouldOverrideUrlLoading method.
* #param view The WebView that is initiating the callback.
* #param request Object containing the details of the request.
* #return {#code true} if the host application wants to leave the current WebView
* and handle the url itself, otherwise return {#code false}.
*/
#Override
#SuppressWarnings("deprecation") // for invoking the old shouldOverrideUrlLoading.
#RequiresApi(21)
public boolean shouldOverrideUrlLoading(#NonNull WebView view,
#NonNull WebResourceRequest request) {
if (Build.VERSION.SDK_INT < 21) return false;
return shouldOverrideUrlLoading(view, request.getUrl().toString());
}
If you see the documentation above for returning the value, it says,
#return {#code true} if the host application wants to leave the current WebView
*and handle the url itself, otherwise return {#code false}.
So it simply says, If you return true from shouldOverrideUrlLoading method, it'll ask the default browser of your device to handle the request of opening the URL and if you return false, then your URL will be loaded through webview only.
Now you can load your URL in webview either after this setWebViewClient call or you can also load your URL inside shouldOverrideUrlLoading method before returning the value.
this code work for me
thanks...
WebView wbView = (WebView) findViewById(R.id.webView);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.setWebViewClient(new WebViewClient());
wbView.loadUrl("http://ppid.polinela.ac.id");

Changing the background of webview in android

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();

Categories

Resources