Android's webview doesn't show google maps - android

If i start this application, i can see the webview correctly, but it doesn't load google maps (https://maps.google.com/maps?q=43.0054446,-87.9678884&t=m&z=7), but it loads the normal google page (https://www.google.it/#q=43.0054446%2C-87.9678884). Why? Is there a way to fix it?
public class MainActivity extends Activity
{
WebView myWebView;
String mapPath = "https://maps.google.com/maps";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.mapview);
String map2="?q=43.0054446,-87.9678884&t=m&z=7";
myWebView.loadUrl(mapPath+map2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Now i have another problem. This is my xml Layout.
<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"
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=".MainActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_height="#string/hello_world"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<WebView
android:id="#+id/mapview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I want to display the WebView in the upper half of the screen, but when the link is loaded, the page is fullscreen (even if i insert something else under the webview). Why?

I just removed the S from the httpS and it works. I don't know if this can help.

For me I was missing the myWebView.getSettings().setJavaScriptEnabled(true);.
This is my code:
//set the webview and have open the map
mywebview = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mywebview.getSettings();
webSettings.setBuiltInZoomControls(true);
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.setWebViewClient(new Callback());
mywebview.loadUrl(mapPath+map2);
Thanks to the commenter adding that, I was going a little nuts.

Related

Problem: Loading Mapbox in Webview (Android Studio Project)

I have an html file in my website:
http://manizani.ir/hamyar_gas_php/map/map_app.html
I want to show it on my Android Application. I created an Activity and use a Webview to load above url. But nothing is loaded and it shows me just an empty activity.
I can load any other web pages successfully, but not about my url.
Please help me to solve it.
Thanks a lot
activity_map_web.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MapWebActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</LinearLayout>
</RelativeLayout>
//////////////End of file//////////////////
MapWebActivity.java :
public class MapWebActivity extends AppCompatActivity {
WebView WV;
String url = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_web);
try {
WV = (WebView) findViewById(R.id.webview_intro);
WV.getSettings().setLoadsImagesAutomatically(true);
WV.getSettings().setJavaScriptEnabled(true);
WV.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WV.getSettings().setBuiltInZoomControls(true);
WV.getSettings().setDomStorageEnabled(true);
WV.getSettings().setAppCacheEnabled(true);
WV.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
WV.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
WV.setWebViewClient(new WebViewClient());
url="http://manizani.ir/hamyar_gas_php/map/map_app.html";
WV.loadUrl(url);
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Mapbox use WebGL and may be is not support on your device.
webview log :
[INFO:CONSOLE(31)] "Error: Failed to initialize WebGL", source: https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js (31)
more at here

Nest ScrollView

there are two webviews in scrollview.
<ScrollView>
<LinearLayout>
<WebView></WebView>
<WebView></WebView>
</LinearLayout>
</ScrollView>
I want to make two webviews looks like one webpage. currently, my solution is make webview's height equal to webview's contentHeight. and disable scrollability of webview. but for very big html. it's not a good solution. webview has to render the whole html. I want to use nestedscrollview. make two webviews' height equals to nestedscrollview's height. but I dont known how to deal the events.
Try this to make two WebView looks like one webpage.
public class MainActivity extends Activity {
String url = "http://www.yahoo.com";
String url1 = "http://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "Activity one");
WebView webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewController());
webview.loadUrl(url);
WebView webview2 = (WebView) findViewById(R.id.webView2);
webview2.getSettings().setJavaScriptEnabled(true);
webview2.setWebViewClient(new WebViewController());
webview2.loadUrl(url1);
}
}
XML code
<?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">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
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="wrap_content" />
<WebView
android:id="#+id/webView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
WebView client
class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

webview is not displaying correct

I am trying to open url under webview. I have the code below:
public class FragmentWebView extends Fragment {
TextView webview_bar_title;
ImageButton menu;
WebView wv;
public FragmentWebView() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container,
false);
menu = (ImageButton) view.findViewById(R.id.webview_menu);
menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity) getActivity()).onBackPressed();
}
});
webview_bar_title = (TextView) view.findViewById(R.id.webview_share);
webview_bar_title.setText("Share");
wv = (WebView) view.findViewById(R.id.fragment_webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://www.google.com");
return view;
}
}
And here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/webview_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/webview_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow_left" />
<TextView
android:id="#+id/webview_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</RelativeLayout>
<WebView
android:id="#+id/fragment_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/webview_actionbar" />
</RelativeLayout>
I am trying to load google page in my webview. But it's not working and gave me errors like The website is not available. The webpage google might be temporarily down or it might have moved permanently to a new address. And suggestion? Thanks in advance.
Make sure you're setting a WebViewClient to your WebView and that you declare the INTERNET permission in the manifest.
To set the client:
wv.setWebViewClient(new WebViewClient());
To declare the permission in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
Those are the only things that I see missing in your code.
Also, it's much more common practice to subclass WebViewClient to handle different page events instead of using the default client object.

findViewById returns null for WebView

I'm trying to build an android app, which just consists of a webview. I took the example from android dev site and modified it. But as simple as it looks, every call to findViewById results in a null pointer.
The .java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = (WebView) findViewById(R.id.webview);
// returns null pointer
webView.loadUrl("file:///android_asset/index.html");
setContentView(webView);
}
}
The layout file:
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And I put following line into the manifest:
<uses-permission android:name="android.permission.INTERNET" />
I'm puzzled ...
Change to
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // muylayout is your layout.xml
WebView webView = (WebView) findViewById(R.id.webview);
// returns null pointer
webView.loadUrl("file:///android_asset/index.html");
}
}
You need to set the content of your layout to the activity before calling findViewById. findViewById looks for a view with the id provided in the current layout inflated.
Just in case the accepted answer is not working. Since i have two different layouts for phone and tablet but only one have WebView which cause findViewById() get null.

Strange behaviour when using WebView and RelativeLayout in fullscreen

EDIT:
Here's the video showing the problem: www.youtube.com/watch?v=5ZZsuMH5T9k I'm really stuck here :/
--
I have an activity which consists of a webview and a button at the bottom. I set the window of the activity to fullscreen using this code in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
InstaFetchApplication.applyCurrentTheme(this);
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_PROGRESS);
if (UserPreferences.getIsFullScreenReadingEnabled(this)) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
// this just do this: window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ActivityUtils.toggleFullScreen(this, true);
}
setContentView(R.layout.view_article);
// ...
}
The problem is that most of the time the webview seems to "push" the button off the screen. Here's the screenshot:
(larger version)
What I would expect to see (and sometimes I do) is here:
(larger version)
Here's the layout I'm using (note that in my app the top layout has to be a RelativeLayout):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Click me!"
/>
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/button"
/>
</RelativeLayout>
I also noticed that if you turn off all animations on the device using system settings, the issue is gone (though there's this glitch when the whole view has a top margin and then it jumps back in the correct position).
Did anyone encounter similar problem and know how to fix it?
Finally I found the answer. Adding WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS flag to the window solved my problem.
See this post: flag_fullscreen + windowNoTitle == button focus bug.
I just added a button to the Hello WebView tutorial, and everything seems to look right. Then I converted main.xml from LinearLayout to RelativeLayout, and everything still seems to look right. I think you are making things too complicated. :-) Try removing most of that initialization code from onCreate() and just specify it in the XML.
Here is my project.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.hellowebview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".HelloWebViewActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
HelloWebViewActivity.java:
package com.example.android.hellowebview;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class HelloWebViewActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_rl);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
main_ll.xml (LinearLayout):
<?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" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="#+id/mainbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/btn_label"
/>
</LinearLayout>
main_rl.xml (RelativeLayout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:layout_above="#+id/mainbtn"
/>
<Button
android:id="#+id/mainbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/btn_label"
/>
</RelativeLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWebViewActivity!</string>
<string name="app_name">HelloWebView</string>
<string name="btn_label">Push Me</string>
</resources>
Try to align the web view to the parent top, and align the button to the parent bottom and to be below the web view. I have found times when, for no real reason, the elements don't quite line up correctly if they are not all strung together in a linear sequence either anchored at the top or at the bottom of the screen.
I presume that you are not overriding the onMeasure() method and changing the measured height of the button. I've created numerous (too many) bugs during development of my apps by miscalculating the height of an element during the layout phase and have spent hours struggling with debugging the layout definition code only to find that it was fine, but the height calculation was feeding invalid data into the layout's rendering phase.
Unless the RelativeLayout is absolutely necessary, You could change to a LinearLayout:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I'm a button"/>
</LinearLayout>
It should give a better result.
To hide the title bar I use:
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Tested on a Galaxy S 2 (Android 2.3.3) and Huawei U8180 X1 (Android 2.2.2) and worked fine with your relative layout.
From the screenshot I have the impression that the problem lies rather with the incomplete progress indicator you requested. The fact that the problem disappears when turning off the animations also supports this theory.
You may try this. The top layout is still RelativeLayout, the only difference with yours is the LinearLayout is wrapping up everything:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight=".90" />
</LinearLayout>
</RelativeLayout>
In the LinearLayout, WebView occupies 90% of entire window(and parent) and the button remaining 10%. The height is set to 0px for both, so the weight can do the work for both of them. The cost for the parsing this xml is insignificant.
And in Java code I did:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
LinearLayout mLinearLayout = (LinearLayout)findViewById(R.id.ll);
Button mButton = new Button(this);
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 0.10f);
mButton.setText("Click me!");
mButton.setLayoutParams(mParams);
mLinearLayout.addView(mButton);
mLinearLayout.invalidate();
}
below code might help you,
webView = (WebView) findViewById(R.id.webview1);
webView.setInitialScale(100);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
view.scrollTo(0, 0);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadDataWithBaseURL(null, yourdata, "text/html", "utf-8", null);
// view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
try {
//your stuff
} catch (Throwable e) {
e.printStackTrace();
}
}
}

Categories

Resources