Im pretty new to android coding and I just created a webview app for my church. The apk was generated successfully but I still keep getting this annoying white space at the bottom of the screen.
To date I have removed all the android:padding references in the content_main.xml file but still am having no success.
Ive also searched on here to see if anyone else had the same issue but those threads only helped me get rid of the padding code and not this annoying white space at the bottom.
If you need me to post any code please let me know. Thanks in advance!
Here is a picture of what I am talking about with the white block at bottom
activity_main.xml------------------
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
activity_main.xml------------------
content_main.xml-------------------
<ProgressBar
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:id="#+id/progressBar1"/>
<TextView
android:layout_below="#+id/progressBar1"
android:layout_height="wrap_content"
android:id="#+id/LoadingText"
android:layout_width="fill_parent"
android:text="Loading, Please Wait.."
android:textSize="20sp"
android:gravity="center_vertical|center_horizontal">
</TextView>
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
content_main.xml------------------
MainActivity.java------------------
package org.communionchapelefca.ccsatx;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private WebView mWebView;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://communionchapelefca.org/edy-home");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
webView.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{ //if back key is pressed
if((keyCode == KeyEvent.KEYCODE_BACK)&& mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Exit");
// set dialog message
alertDialogBuilder
.setMessage("Do you really want to exit?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
MainActivity.java------------------
For the html code for the page we use Wordpress to host it. I've disabled all footers and such for what I think are the likely culprits. When opening the page (www.communionchapelefca.org/edy-home) it does not display the white block at the bottom.
Let me know if I need to post any other XML or java files. Thanks for your help.
Well all I did to fix it was hit the enter key about five times (to add space between the last text within the document). Then I added text to the bottom of my html document and then colored that text the same color as the background. Not a pretty fix but it worked.
Related
I am working on WebView. I don't know whether it's possible or not. I am opening an url inside of this view which is basically a login page. Now want to get the information whether user has successfully logged in or not. If yes then navigate to custom page (Android native) else keep on that page.
Thanks in advance.
We can achieve it via using this..
Create a function of javascript in the HTML page of Login Success/Failue HtmL page.
in this javascript function ,call your native Function by Using addJavascriptInterface:
package com.example.androidjsdemo;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
// Here you can Start WhatEver Activty launch///////
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webMy = (WebView) findViewById(R.id.webMy);
webMy.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings setting = webMy.getSettings();
setting.setJavaScriptEnabled(true);
//File file = new File("andoid");
webMy.loadUrl("file:///android_asset/mine.html");
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
mine.html
<HTML>
<script>
function called()
{
// alert("hi");
Android.showToast("Hi Testing");
}
</script>
<Body>
Hi Sumit
<button onclick="called()">click me</button>
</Body>
</HTML>
activity_main.xml
<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="com.example.androidjsdemo.MainActivity" >
<WebView
android:id="#+id/webMy"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
When user complete successful singing he should redirect to other web page with url that indicate the login was successful. it can look something like http://www.yourwebsite.com/islogin?yes.
than all you need to do is to check in your app what the login status by verify that the url contain islogin and that it set to yes and do whatever you want.
here a small sample to help you begin:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(isUserLoggedIn(url)){
doWork();
return true;
} else {
return false;
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
public void onPageFinished(WebView view, String url) {
}
});
For Example below is your login URL which you load in webview for login
String loginURL = "http://www.myurl.com/login";
Two possibility, when user click on login button
1) Success
Navigate to Other page as per web code. So we need to check it and open our native application screen.
2) Fail
Same login page but display error message according to user input.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(loginURL)) {
// Login Fail
return false;
} else {
// Login Success
// Open Android Native screen
return true;
}
}
});
Please Help
When you press the Back Button to get out of the application
I tried a lot of solutions, but did not succeed
I will put you code
MainActivity
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity{
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://www.alrofaiy.com/");
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
setProgress(progress * 100);
}
});
webview.setWebViewClient(new InsideWebViewClient());
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(webview.canGoBack()){
webview.goBack();
}
}
private class InsideWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.item1:
try {
Intent abb = new Intent(Intent.ACTION_SEND);
abb.setType("text/palin");
abb.putExtra(Intent.EXTRA_SUBJECT , "APP");
String aliwi = "/n /n/n";
aliwi = aliwi + "https:// /n/n";
abb.putExtra(Intent.EXTRA_TEXT, aliwi);
startActivity(Intent.createChooser(abb,""));
}
catch (Exception o)
{o.toString();}
return true;
case R.id.item2:
Intent AAA = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:info#alrofaiy.com?subhect= "));
startActivity(AAA);
return true;
case R.id.link1:
webview.loadUrl("http://www.alrofaiy.com/login_in");
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<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="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:padding="0dp">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusableInTouchMode="false"
android:padding="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/text_call"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/webView"
android:layout_alignEnd="#+id/webView"
android:autoLink="phone"
android:clickable="true"
android:linksClickable="false"
android:textColor="#fff"
android:background="#000"
android:paddingTop="2dp" />
</RelativeLayout>
What's the solution?
When you run the application and view the site does go out when you press the back button
#Override
public void onBackPressed() {
super.onBackPressed();
if(webview.canGoBack()){
webview.goBack();
}
}
Should be
#Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
} else {
super.onBackPressed();
}
}
i've been trying to make an app with a buttonbar with add/exit buttons at the bottom, and a EditText at the top.
when i hit the add button i want the app to add checkboxes with the text from the EditText.
but when i try to launch the app it force closes.
the logcat show absolutely nothing from my app,
the console says:
[2014-06-24 18:41:06 - CheckList] Starting activity com.pzayx.checklist.CheckList on device 7a0ec297
[2014-06-24 18:41:06 - CheckList] ActivityManager: WARNING: linker: app_process has text relocations. This is wasting memory and is a security risk. Please fix.
[2014-06-24 18:41:06 - CheckList] ActivityManager: WARNING: linker: app_process has text relocations. This is wasting memory and is a security risk. Please fix.
[2014-06-24 18:41:07 - CheckList] ActivityManager: Starting: Intent {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.pzayx.checklist/.CheckList }
[2014-06-24 18:41:07 - CheckList] Attempting to connect debugger to 'com.pzayx.checklist' on port 8960
i cant seem to find much information about "app_process has text relocations. This is wasting memory and is a security risk. Please fix" and suspect the demonspawn lies there.
here is the source of the CheckList.java
package com.pzayx.checklist;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
public class CheckList extends Activity {
LinearLayout lil = (LinearLayout) findViewById(R.id.LinearMain);
EditText mTextInput;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_check_list);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setupWidgets();
}
private void setupWidgets() {
mTextInput = (EditText) findViewById(R.id.editText1);
Button addBtn = (Button) findViewById(R.id.button_add);
addBtn.setOnClickListener(addListener);
Button exitBtn = (Button) findViewById(R.id.button_exit);
exitBtn.setOnClickListener(exitListener);
}
private OnClickListener addListener = new OnClickListener() {
public void onClick(View v) {
doSaveButtonPress();
}
};
private OnClickListener exitListener = new OnClickListener() {
public void onClick(View v) {
doExitButtonPress();
}
};
public void doSaveButtonPress() {
String text = mTextInput.getText().toString();
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(i);
cb.setText(text);
lil.addView(cb);
mTextInput.setText("");
i++;
}
public void doExitButtonPress() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Exit Application?");
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
And here is my layout source
<LinearLayout
android:layout_below="#+id/LinearMain"
android:id="#+id/LinearLayoutButtons"
style="?android:buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:divider="?android:attr/dividerVertical"
android:orientation="horizontal"
android:showDividers="middle" >
<!-- <requestFocus /> -->
<Button
android:id="#+id/button_add"
style="?android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/add"
android:layout_gravity="bottom" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dip"
android:layout_height="40dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="4dip"
android:layout_marginTop="4dip"
android:background="?android:attr/dividerVertical"
android:layout_gravity="bottom"/>
<Button
android:id="#+id/button_exit"
style="?android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/exit"
android:layout_gravity="bottom" />
<!-- Checkbox Area -->
</LinearLayout>
<LinearLayout
android:id="#+id/LinearMain"
android:layout_width="match_parent"
android:layout_height="420dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/editText"
android:ems="10" />
</LinearLayout>
</RelativeLayout>
would appreciate it if anyone could help me make some sense out of it!
Thank you!
The phone back button works in the webview,but when I want to exit from the Webview to the application this didn't do anything.
I put a button that go back to the application but I did't that remain that way.
JAVA:
package com.wiralss.adb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class about extends Activity implements OnClickListener{
WebView webView;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.instagrem);
webView = (WebView) findViewById(R.id.webView1);
Button B123=(Button)findViewById(R.id.button1235);
B123.setOnClickListener(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1235:
Intent B = new Intent(this, MainActivity.class);
startActivity(B);
break;
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
public void onBackPressed (){
if (webView.isFocused() && webView.canGoBack()) {
webView.goBack();
}
else {
super.onBackPressed();
finish();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
webView.goBack();
return true;
}
return false;
}
}
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" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<Button
android:id="#+id/button1235"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="580px"
android:layout_weight="0.05"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
Another question.
To remove the button I need to delete?:
Button B123=(Button)findViewById(R.id.button1);
B123.setOnClickListener(this);
And?
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent B = new Intent(this, MainActivity.class);
startActivity(B);
break;
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
Tank you very much.
You don't need the call to super.onBackPressed() as you are controlling the finishing of your activity yourself.
Equally, you don't really need the webView.isFocused() method call.
You can rewrite your onBackPressed method to read like this:
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
I'm not too sure on your motives for including your button1235 Button, is that something you are wanting to remove?
Remove onKeyDown method implementation as the way you implement it will case onBackPressed to never be called. onBackPressed should be only:
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
To remove the button it's enough to make it gone, but first of all you need to declare it as class member:
private Button B123;
and initialize it as you do in onCreate:
webView = (WebView) findViewById(R.id.webView1);
B123 = (Button) findViewById(R.id.button1235);
B123.setOnClickListener(this);
To make it gone:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1235:
B123.setVisibility(View.GONE);
Intent B = new Intent(this, SecondActivity.class);
startActivity(B);
break;
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
I would change the layout as well as you're getting some lint errors since you're using px instead of dp. The whole layout can be improved.
So I have a WebView inside of a LinearLayout with an EditText Box and a Button to make the application search for the web. When I click go on my button it takes me to the stock android browser. how do I make It so that it stays within my WebView when I click go?
here is my layout:
<?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"
>
<EditText
android:id="#+id/web_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"/>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/go"
android:gravity="right"/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
here is my source:
package straightapp.com.Browser;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class Browser extends Activity {
WebView mWebView;
Button button;
EditText text;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.straightapp.com");
text=(EditText)findViewById(R.id.web_address);
button=(Button) findViewById(R.id.go);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
openBrowser();
}
});
text.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_ENTER){
openBrowser();
return true;
}
return false;
}
});
}
public void openBrowser(){
Uri uri=Uri.parse(text.getText().toString());
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks
When I click go on my button it takes me to the stock android browser.
That's because that is what you told Android to do. startActivity() starts an activity.
how do I make It so that it stays within my WebView when I click go?
Call loadUrl() on your WebView instead of calling startActivity().
To be precise, you can try following change:
public void openBrowser(){
//Ensure that URL entered is a valid one
mWebView.loadUrl(text.getText().toString());
}
Change your xml file as below:
<?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"
tools:context=".Browser"> <!--Just add this line -->
<EditText
android:id="#+id/web_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"/>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/go"
android:gravity="right"/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
First declare the webview and progressbar
progressBar = (ProgressBar)findViewById(R.id.progressBar);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
Copy and paste this code below onCreate method
public class myWebClient extends WebViewClient {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}