using phones back button to exit from webview - android

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.

Related

How to remove white padding block from bottom of android webview app?

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.

Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data

I have a problem when getting state of activity. I have 2 activities: MainActivity and Activity2. In MainActivity I have put an editText and a button name GO. In Activity2, I have a button name BackMainActivity. What I want is that: I put a text, for example:"abc" into EditText then click button GO. App will navigate to Activity2. After that, I click button BackMainActivity, app will navigate to MainActivity and data in EditText is restore as "abc".
I already used onSaveInstanceState and onRestoreInstanceState. App run through onSaveInstanceState before going to Activity2. But if I go back to MainActivity, onCreate(), savedInstanceState is null. Can you show me the reason ? I want to store data of mainActivity in bundle. So how can I do ? Thank you very much !
MainActivity
package com.example.demosavedataactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText t;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
String a = t.getText().toString();
outState.putString("text",a);
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
t.setText(savedInstanceState.getString("text"));
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onDestroy");
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onPause");
super.onPause();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onRestart");
super.onRestart();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onResume");
super.onResume();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onStart");
super.onStart();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
Log.w("<<<<<<<<<<<<<<<<<<<", "onStop");
super.onStop();
}
}
Activity2
package com.example.demosavedataactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity {
Button back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}
});
}
}
activity2.xml
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CombackA1" />
</LinearLayout>
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"
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" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginLeft="50dp"
android:layout_marginTop="63dp"
android:text="Button" />
</RelativeLayout>
Just do something like:
1: Fetch data from onSavedInstanceState in your oncreate() and put it in your edittext. Something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
/*Just fetch data from savedInstanceState */
if(savedInstanceState != null){
t.setText(savedInstanceState.getString("text"));
}
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
And in your Activity2 set a FLAG_ACTIVITY_REORDER_TO_FRONT flag with your intent.like:
Intent intent = new Intent(Activity2.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
For a better idea just check this conversation.
In the onClick function of your Activity2 you don't go back to the previous MainActivity but you open a new MainActivity.
Just finish Activity2 by calling finish() then your first MainActivity will come up again. It should be working with the hardware back button of your device.

I am unable to set Onclick event on Imageview in android

Where ever i click on ImageView the onclick method is not hapenning. i mean it is not redirecting to my main.xml
//package name : bunk
//My cesem.XML :
//just a Textview and a Image View
- Indented four spaces.
<?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:padding="25dp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Your Semester"
android:textSize="25dp"
android:gravity="center"
android:id="#+id/tvSemCe"
android:layout_marginBottom="20dp"
/>
<ImageView
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"
/>
</LinearLayout>
//Class file: Cesem.java
package com.bunk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class Cesem extends Activity implements OnClickListener{
ImageView back;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cesem); // cesem.xml
back=(ImageView) findViewById(R.id.back);// back is ImageView
back.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == back) {
setContentView(R.layout.main);
}
}
}
if (v.getId() == R.id.back)
instead of
if (v == back)
try to set the element as clickable:
back.setClickable(true);
Or maybe you can't see a feedback beacuse of the content of click callback (setContentView..)
Try to log something inside the click callback, such as
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == back) {
Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
setContentView(R.layout.main);
}
}
Change your code as:
public class Cesem extends Activity implements OnClickListener{
ImageView back;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cesem); // cesem.xml
back=(ImageView) findViewById(R.id.back);// back is ImageView
back.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.back) {
Toast.makeText(getApplicationContext(), "Toast 1",Toast.LENGTH_SHORT).show();
Activity.this.setContentView(R.layout.main);
}
else
{
Toast.makeText(getApplicationContext(), "Toast 2",Toast.LENGTH_SHORT).show();
}
}
}
}
and register your Activity in manifest as:
<activity
android:name=".Cesem" />

ontouch and onclick on a textview not working

have a look at the code below...
i am setting the onclick listener on the textview and the ontouch listener but none of them is working...
what seems to be the problem>....?
AlertDialog.Builder builder;
AlertDialog a;
TextView text,txtNewUser ;
ImageView image;
View layout;
Button ok;
String pswrd;
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final Context mContext = this;
Dialog dialog = new Dialog(mContext);
inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
ok = (Button) findViewById(R.id.buttonOK);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
layout = inflater.inflate(R.layout.custom_dialog_private_space,
(ViewGroup) findViewById(R.id.layout));
text = (TextView) dialog.findViewById(R.id.tvDesc);
txtNewUser = (TextView) dialog.findViewById(R.id.tvNewUser);
image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.lock_symbol_android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
a = builder.create();
a.show();
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
return true;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
here is the code of textview from the xml of the dialog layout
<?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:id="#+id/layout" >
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvNewUser"
android:text="New User?"
android:layout_gravity="top|left"
android:clickable="true"
/>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/tvDesc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Enter password"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="100"
android:inputType="textPassword" >
</EditText>
<Button
android:id="#+id/buttonOK"
android:layout_width="100dp"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:text="OK" />
</LinearLayout>
Instead of infalter use window class
return false in on Touch listener
set contenet view to your main activity
package com.collabera.labs.sai;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HomePage extends Activity {
AlertDialog.Builder builder;
AlertDialog a;
TextView text, txtNewUser;
ImageView image;
Button ok;
String pswrd;
public Window mWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_private_space);
ok = (Button) findViewById(R.id.buttonOK);
final Context mContext = this;
Dialog dialog = new Dialog(HomePage.this);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
dialog.show();
mWindow = dialog.getWindow();
Log.w(" dialog is " + mWindow.toString(), "-------");
text = (TextView) mWindow.findViewById(R.id.tvDesc);
txtNewUser = (TextView) mWindow.findViewById(R.id.tvNewUser);
image = (ImageView) mWindow.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
}
I think.. you should also return
return super.onTouchEvent(event);
from onTouch function.. instead of true because true means the event is consumed by touch and other actions are not called for this event.. in this case onClick
this is the way i used to make imageview clickable:
public class Main extends Activity **implements OnClickListener** {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView profile_btn=(ImageView) findViewById(R.id.x_Btn);
x_btn.setOnClickListener(this) ;
ImageView food_btn=(ImageView) findViewById(R.id.y_Btn);
ybtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.x_Btn:
//do some thing;break;
case R.id.y_Btn:
//do some thing;break;
}
}
}
In toast both cases you have written clicked so change to touch in onTouch listener and return false at onTouch Listener:
public class HomePage extends Activity {
private TextView txtNewUser;
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = getApplicationContext();
txtNewUser = (TextView) findViewById(R.id.tvNewUser);
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched ", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}

WebView inside application?

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

Categories

Resources