I have a simple app that is based on WebView.loadUrl("http://www.example.com").
When a user clicks on a URL, the default behavior is to immediately show a blank page, wait until the page is loaded, and then show this page.
I managed to show a splash screen in lieu of the blank page. However, is it possible to keep the current page rendered instead, and render the next page only when it is fully loaded?
This is, I feel, a hacky solution. I offer it as a temporary fix until someone posts the "real" way to do this.
MainActivity class:
public class MainActivity extends Activity
{
WebView webView1, webView2;
EditText editText;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.edit_url);
webView1 = (WebView) findViewById(R.id.webView_1);
webView2 = (WebView) findViewById(R.id.webView_2);
webView1.setWebViewClient(client);
webView2.setWebViewClient(client);
webView1.loadUrl(editText.getText().toString());
}
private WebViewClient client = new WebViewClient()
{
public void onPageFinished(WebView webView, String url)
{
if (webView == webView1)
webView2.setVisibility(View.GONE);
else
webView1.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "WebView " + (webView == webView1 ? "One" : "Two") + " is showing", 0).show();
}
};
private void loadUrl(String url)
{
if (webView1.getVisibility() == View.GONE)
webView1.loadUrl(url);
else
webView2.loadUrl(url);
}
public void onClick(View v)
{
loadUrl(editText.getText().toString());
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.webkit.WebView android:id="#+id/webView_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.webkit.WebView android:id="#+id/webView_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText android:id="#+id/edit_url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:layout_gravity="center_vertical"
android:text="http://www.example.com" />
<Button android:id="#+id/button_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Load" />
</LinearLayout>
</LinearLayout>
Related
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;
}
}
I have one Edit Text, 5 buttons and a Webview. Webview load url when user type url in edit text and tap Go button or hit Enter on soft keyboard.
I have three problems here.
1-WebView loads website for example "www.google.com" but soft keyboard does not hide itself. I want to hide it like other browser do.
2-When user open a specific url and then try to remove it from Edit Text to open a different url, webview start loading that incomplete url and get focus automatically on every letter removing. For example user loads "www.google.com" then he try to remove url from end and as he remove "m", webview try to load "www.google.co" then edit text is out of focus and webview stole focus from it then user have to tap in edit text again to remove url but it tries to load after every letter removing. I need Webview to load url only when user tap on Go button or hit Enter on soft keyboard.
Update (forgot to add third problem, here it is)
3-Where can i check when WebView CanGoBack and CanGoForward because i want to set visibility of Back and Forward buttons to false at startup and enabled these two buttons while they CanGoBack and CanGoForward. Where should i put my piece of code to check these conditions?
I hope you guys can understand what i am trying to say. Sorry for my poor english.
Here is my xml code.
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="13"
tools:context="com.hbss.chatapp.rashidfaheem.hybridsoftwaresolutions.rashidfaheem.youseebrowser.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/etUrl"
android:layout_weight="3"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GO"
android:id="#+id/btnGo"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back"
android:id="#+id/btnBack"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Forward"
android:id="#+id/btnForward"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear"
android:id="#+id/btnClear"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reload"
android:id="#+id/btnReload"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wb"
/>
</LinearLayout>
</LinearLayout>
Here is my java code.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etUrl;
Button btnGo, btnBack, btnForward, btnClear, btnReload;
WebView wb;
String url;
View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wb = (WebView) findViewById(R.id.wb);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.setWebViewClient(new ourClient());
etUrl = (EditText) findViewById(R.id.etUrl);
btnGo = (Button) findViewById(R.id.btnGo);
btnBack = (Button) findViewById(R.id.btnBack);
btnForward = (Button) findViewById(R.id.btnForward);
btnClear = (Button) findViewById(R.id.btnClear);
btnReload = (Button) findViewById(R.id.btnReload);
btnGo.setOnClickListener(this);
btnReload.setOnClickListener(this);
btnBack.setOnClickListener(this);
btnForward.setOnClickListener(this);
btnClear.setOnClickListener(this);
etUrl.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction()==KeyEvent.ACTION_DOWN && i==KeyEvent.KEYCODE_ENTER) {
return true;
}
load();
return false;
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnGo:
load();
break;
case R.id.btnBack:
if (wb.canGoBack())
wb.goBack();
break;
case R.id.btnForward:
if (wb.canGoForward())
wb.goForward();
break;
case R.id.btnReload:
wb.reload();
break;
case R.id.btnClear:
wb.clearHistory();
break;
}
}
public void load(){
url = etUrl.getText().toString();
if (!url.startsWith("http://")) {
url = "http://" + url;
}
try {
wb.loadUrl(url);
wb.requestFocus();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), " " + e, Toast.LENGTH_LONG).show();
}
}
}
use this method to hide the keyboard:
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow()
.getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
call it in your load() method like:
hideSoftKeyboard(MainActivity.this);
For your second question you need to call your method inside the condition like:
etUrl.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((keyEvent.getAction()==KeyEvent.ACTION_DOWN) && (i==KeyEvent.KEYCODE_ENTER)){
load(); //add it here
return true;
}
return false;
}
});
EDIT
For the webview go back feature: override onKeyDown in your activity and check condition like:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && wb.canGoBack()) {
wb.goBack();
//show your back button here
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
//show other buttons
return super.onKeyDown(keyCode, event);
}
You can add the check in your onResume() and load() also and show your button accordingly..
#Override
protected void onResume() {
super.onResume();
if(wb.canGoBack()){
//show back button}
else{
//other button
}
}
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.
I have an activity that displays web pages. On some phones (e.g, Huawei Ascend) , this activity crashes in a way that causes the application to restart, but doesn't cause the usual android error popup for reporting. (Other phones (e.g. Nexus) never crash at all.) It does appear to always crash while a web page is loading, but not at any particular point e.g. sometimes at shoulOverrideUrlLoading, sometimes during onPageStarted or onPageFinished.
I think the WebView is causing this--
08-29 21:08:22.577: A/libc(957): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 957
--but I can't work out why.
Here's the activity:
public class WebDisplay extends Activity
{
private String sentUrl = "";
public WebView myWebView;
public ProgressBar spinner;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d("WEBVIEW", "onCreate");
getWindow().setBackgroundDrawableResource(R.color.white);
Bundle extras = getIntent().getExtras();
if (null != extras)
{
sentUrl = extras.getString("displayURL");
}
setContentView(R.layout.webdisplay);
myWebView = (WebView) findViewById(R.id.webDisplay);
spinner = (ProgressBar) findViewById(R.id.webLoading);
// WebView displays default to showing things in exact pixel size at 100%
// resolution, meaning it generally opens on a zoomed in portion of the
// top left of the web page and refuses to let you zoom out, so we have
// to force it to display pages a bit more reasonably.
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
// By default, there is no javascript support or zoom controls, so
// turn both of those on.
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// We want links to continue opening inside the app.
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBVIEW", "loadUrl="+url);
view.loadUrl(url); // open link in the same web view.
return true;
}
#Override
public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
Log.d("WEBVIEW", "onPageStarted="+url);
super.onPageStarted(view, url, favicon);
if (View.GONE == spinner.getVisibility()) { spinner.setVisibility(View.VISIBLE); }
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBVIEW", "onPageFinished="+url);
super.onPageFinished(view, url);
if (View.VISIBLE == spinner.getVisibility()) { spinner.setVisibility(View.GONE); }
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String url) {
Log.d("WEBVIEW", "onPageError="+url+"::descr="+description);
}
});
// Try to get YouTube videos working.
myWebView.setWebChromeClient(new WebChromeClient() {
});
// So the user doesn't have to back through their entire history to get
// back to the app, we provide a "done browsing" button.
Button doneButton = (Button) findViewById(R.id.doneButton);
doneButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
finish();
}
});
ImageButton backButton = (ImageButton) findViewById(R.id.webBackButton);
backButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
myWebView.goBack();
}
});
ImageButton fwdButton = (ImageButton) findViewById(R.id.webForwardButton);
fwdButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
myWebView.goForward();
}
});
ImageButton refreshButton = (ImageButton) findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
myWebView.reload();
}
});
myWebView.loadUrl(sentUrl);
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
myWebView.restoreState(savedInstanceState);
}
}
And here's the loaded layout:
<?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"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/webNavBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/abs__background_holo_light"
android:layout_alignParentBottom="true"
android:padding="2dp" >
<Button
android:id="#+id/doneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/refreshButton"
android:layout_alignTop="#+id/refreshButton"
android:background="#drawable/custom_button"
android:padding="5dp"
android:text="#string/web_done"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageButton
android:id="#+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/custom_button"
android:src="#drawable/ic_action_refresh"
android:contentDescription="#string/web_refresh"
android:textColor="#color/white"
android:padding="5dp" />
<ImageButton
android:id="#+id/webBackButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/webForwardButton"
android:background="#drawable/custom_button"
android:src="#drawable/navigation_back"
android:padding="5dp"
android:contentDescription="#string/web_back"
android:textColor="#color/white" />
<ImageButton
android:id="#+id/webForwardButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/custom_button"
android:src="#drawable/navigation_forward"
android:contentDescription="#string/web_forward"
android:textColor="#color/white"
android:padding="5dp" />
</RelativeLayout>
<WebView
android:id="#+id/webDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/webNavBar" />
<ProgressBar
android:id="#+id/webLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="80dp"
style="#android:style/Widget.ProgressBar.Large"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ETA: Turning hardware acceleration off like this
myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
seems to stop the SIGSEV thing, but also stops YouTube videos playing (I still have audio, but no image).
make sure the WebView is the first element of the RelativeLayout.
or don't use RelativeLayout, just try LinearLayout.
or remove the attributes
android:layout_alignParentTop="true"
android:layout_above="#id/webNavBar"
of WebView
webSettings.setJavaScriptEnabled(true);
Settings above causes "Fatal signal 11 (SIGSEGV)" error for some sites using java script. I reproduced this error on Samsung Galaxy Tab 3 And Galaxy tab 3 lite. Android version is 4.4.2 for both devices.
If I remove "setJavaScriptEnabled" function, it is working fine.
I am aware that this question has been asked before, but I have tried every "solution" to the other questions, but I still have not been able to achieve what I am trying to do. I have searched and searched, and tried and tried. Please forgive me. I simply want to display my row of buttons above my WebView, so that it is consistent with the rest of the activities in my app (they all have the row of buttons at the top). It seems like I have tried every possible combination of layouts, but to no avail. It displays correctly in the Graphical Layout (Eclipse), but on the phone and emulator it only displays the WebView, fullscreen, no buttons.... Any help will be very much appreciated!
Here's my 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" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/menu_panel">
<ImageButton
android:id="#+id/bHomeMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_home"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="Home" />
<ImageButton
android:id="#+id/bItsMe247Menu"
android:layout_toRightOf="#+id/bHomeMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_selected_hcu_light_blue"
android:src="#drawable/icon_menu_login"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="It'sMe" />
<ImageButton
android:id="#+id/bFindUsMenu"
android:layout_toRightOf="#+id/bItsMe247Menu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_find_us"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="FindUs" />
<ImageButton
android:id="#+id/bEmailMenu"
android:layout_toRightOf="#+id/bFindUsMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_email"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="Email" />
<ImageButton
android:id="#+id/bRatesMenu"
android:layout_toRightOf="#+id/bEmailMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_rates"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="Rates" />
</LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wvItsMe"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
EDIT: Here's my java code, and a note below it:
package com.dummyapp.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.dummyapp.app.SimpleGestureFilter.SimpleGestureListener;
public class ItsMe247 extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
private WebView itsMeLogin;
// ImageButton menuHome, menuFindUs, menuEmail, menuRates;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.itsme247);
// menuHome = (ImageButton)findViewById(R.id.bHomeMenu);
// menuFindUs = (ImageButton)findViewById(R.id.bFindUsMenu);
// menuEmail = (ImageButton)findViewById(R.id.bEmailMenu);
// menuRates = (ImageButton)findViewById(R.id.bRatesMenu);
detector = new SimpleGestureFilter(this,this);
itsMeLogin = new WebView(this);
Toast.makeText(this, "Just Swipe To Exit", Toast.LENGTH_LONG).show();
itsMeLogin.getSettings().setJavaScriptEnabled(true); // JavaScript enabled
getWindow().requestFeature(Window.FEATURE_PROGRESS); // Show progress bar of page loading
final Activity itsMeActivity = this;
itsMeLogin.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
itsMeActivity.setProgress(progress * 100);
}
});
itsMeLogin.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(itsMeActivity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
itsMeLogin.loadUrl("http://www.google.com");
setContentView(itsMeLogin);
// itsMeLogin.setWebViewClient(new WebViewClient() {
// #Override
// public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
// return super.shouldOverrideUrlLoading(view, url);
// }
// });
//******************* MENU BUTTONS START HERE ********************************************************//
// menuRates.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent ratesIntent = new Intent(ItsMe247.this, Rates.class);
// ItsMe247.this.startActivity(ratesIntent);
//
// }
//
// });
//
//
// menuFindUs.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent contactIntent = new Intent(ItsMe247.this, Contact.class);
// ItsMe247.this.startActivity(contactIntent);
//
// }
// });
//
// menuEmail.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent emailIntent = new Intent(ItsMe247.this, Email.class);
// ItsMe247.this.startActivity(emailIntent);
//
//
// }
// });
//
// menuHome.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent itsMeIntent = new Intent(ItsMe247.this, MainActivity.class);
// ItsMe247.this.startActivity(itsMeIntent);
// }
// });
//***************************** MENU BUTTONS END HERE ********************************************************//
}//end onCreate method
#Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
public void onSwipe(int direction) {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT:
Intent funIntent = new Intent(ItsMe247.this, MainActivity.class);
ItsMe247.this.startActivity(funIntent);
//ItsMe247.this.finish();
break;
case SimpleGestureFilter.SWIPE_LEFT:
Intent funIntent2 = new Intent(ItsMe247.this, Contact.class);
ItsMe247.this.startActivity(funIntent2);
//ItsMe247.this.finish();
break;
case SimpleGestureFilter.SWIPE_DOWN:
break;
case SimpleGestureFilter.SWIPE_UP:
break;
}
}//end onSwipe method
public void onDoubleTap() {
}
}
I have all the menu buttons commented out (the buttons I'm trying to get to display above webview) until I finally get them to display through the xml. However, whenever I UNcomment the buttons and their listeners, the app crashes when trying to enter this activity.
Your code isn't using your XML layout, you're creating your own WebView in code and setting that as the view so it bypasses your XML completely.
I've provided a few changes here that should make it work. Pay particular attention to the setContentView line and the following line, these are what use the XML layout. You may need to change the name of the layout (R.layout.whatever) and the webview id (R.id.webview). There may be a few parse errors from me putting it here, might have missed a closing bracket. I deleted much of your commented out code just to keep it simple.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.xmlLayout );
itsMeLogin = (WebView)findViewById( R.id.webview );
detector = new SimpleGestureFilter(this,this);
Toast.makeText(this, "Just Swipe To Exit", Toast.LENGTH_LONG).show();
itsMeLogin.getSettings().setJavaScriptEnabled(true); // JavaScript enabled
getWindow().requestFeature(Window.FEATURE_PROGRESS); // Show progress bar of page loading
final Activity itsMeActivity = this;
itsMeLogin.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
itsMeActivity.setProgress(progress * 100);
}
});
itsMeLogin.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(itsMeActivity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
itsMeLogin.loadUrl("http://www.google.com");
}
Here's my XML file from a similar layout as well, though yours will probably work with the edited code.
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:columnCount="3"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<EditText
android:ems="4"
android:imeOptions="actionNext"
android:inputType="number"
android:layout_gravity="center_horizontal"
android:maxLength="4"
android:selectAllOnFocus="true"
/>
<EditText
android:ems="4"
android:imeOptions="actionDone"
android:inputType="textCapCharacters|textNoSuggestions"
android:layout_gravity="center_horizontal"
android:maxLength="3"
android:selectAllOnFocus="true"
/>
<Button android:id="#+id/submit"
/>
<WebView android:id="#+id/webview"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_height="0dp"
android:layout_width="0dp"
/>
</GridLayout>