OnTouch worked for 1 Button, but not for 3 - android

im new with Android, but have experiance in c# and c++.
Im just trying to display whenever a button is pressed. I searched and found out, that the onClick event cant do that, therefore i use the onTouch-event.
For the first button it was fine and worked. Then I added a sencond and a third one, but now the application crashes, when I execute it.
If I delete the second and the third button with their events, the application still crashes. Not before I delete the first Button and his event the application stops crashing.
here the code of my main.code and the layout-xml:
package com.android.kurve;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
boolean left=false;
boolean right=false;
final Button buttonLeft = (Button) findViewById(R.id.button1);
final Button buttonRight = (Button) findViewById(R.id.button2);
final Button buttonMove = (Button) findViewById(R.id.button3);
final TextView text = (TextView) findViewById(R.id.textView1);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLeft.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
left=true;
text.setText("left");
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
left=false;
}
return true;
}
});
buttonRight.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
right=true;
text.setText("right");
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
right=false;
}
return true;
}
});
buttonMove.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
text.setText("move");
return true;
}
});
}
}
and here the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="207dp"
android:text="Left" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button1"
android:layout_marginRight="31dp"
android:text="Right" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="24dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Move" />
</RelativeLayout>
Thanks for the help.

Maybe the problem could be that you are initializing the layout element globally instead to initializing them in the "onCreate" method. This method is called when the activity is created, so this is the place where all the layout behaviour or values should be initialized.

Related

button hovering not working in android

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- <TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="24dp"
android:text="#string/name" /> -->
<EditText
android:id="#+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etname"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Login" />
</RelativeLayout>
I have a button, I want when I "hover" over the button it shows a hello message with a toast.
I have a button in the layout. I tried to fetch it by findViewById in the FirstActivity. Then I tried using button.setOnHoverListener but it isn't working.
package com.example.datapass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
final EditText name ;
Button loginButton ;
//final Context context = this;
/** Called when the activity is first created. */
//name= (EditText) findViewById(R.id.etname) ;
final TextView tv = (TextView) (findViewById(R.id.textView1) );
loginButton = (Button) findViewById(R.id.button1);
loginButton.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hover", "Bring yor cursor over the button");
if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
{
//tv.setText("hi");
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
#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;
}
}
Can anyone explain what's wrong?
It would be worth changing your onHover method to be like below and break once it carries out an individual action:
#Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
I would also advise changing this line as this is not standard format/syntax:
final TextView tv = (TextView) (findViewById(R.id.textView1) );
To be this instead:
final TextView tv = (TextView) findViewById(R.id.textView1);
However I think your actual issue is more relating to the support on mobile devices of hovering as none of your code actually seems like it would break this code section. The majority of devices can only recognise hovers whilst using a stylus and not actually just with a finger. Only some of the higher end devices can actually recognise a hover action with just a finger. This might be something to note and may be advisable to use a swipe or a click instead of a hover to display the Toast.

Button OnClickListener not working

I am new to Android. I am trying to get input from the EditText and displaying it in TextView with the help of button. When I click the button nothing happens. Please help, here is my MainActivity.java code -
package com.example.addname;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView v1;
EditText t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
t1 = (EditText) findViewById(R.id.editText1);
v1 = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and here is my 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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="48dp"
android:text="Button" />
</RelativeLayout>
Instead of
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
You can use
if(t1.getText().isEmpty()) // isEmpty() is only available from API 9 and Above
{
v1.setText(t1.getText().toString());
}
or this
if(t1.getText().trim().length == 0)
{
v1.setText(t1.getText().toString());
}
Don't compare strings using ==. You're actually comparing reference of an object. To compare strings, use equals() method.
I'd recommend comparing strings with .equals() since Objects compared with == usually fails (== compares references, not Object content).
if(t1.getText().toString().equals("")))
in your case though,since you're comparing an empty String, you can also use
if (t1.getText().toString().length == 0)
Then note that once you set the text, you're setting v1's text to an empty string since your logic indicates that only upon an empty string you set v1's text. So I'd recommend getting rid of the if altogether:
#Override
public void onClick(View arg0) {
v1.setText(t1.getText().toString());
}

Android: ImageView that extends onTouchEvent for Drag

I wrote a simple cards game where the user plays his card
doing TAP on one of the three imageviews (the cards)
I'd like to add the possibility of playing the card
by dragging the ImageView on the "table"
(the table is another layout or simply another part of the screen).
I tryed to use the techinc indicated at
http://blahti.wordpress.com/2011/01/17/moving-views-part-2/
but as it uses AbsoluteLayout, it introduces a lot of limitations
on my current layout, more it requires adjustments depending
to the device screen resolution where the app runs.
I'd like to avoid this continue using
-if possibile- the RelativeLayout.
Maybe the starting point is extenting the ImageView
adding the onTouch support but i couldn't reproduce
the wished effect (drag)
Any idea or suggestion or sample code?
Thanks!
Just to give an idea, this is the draft of layout.
The 3 cards below should be moved via drag.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="#55335588"
android:layout_above="#+id/player_cards_layout"
>
<ImageView android:id="#+id/img_pc_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#336699"
>
<ImageView android:id="#+id/img_user_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
<ImageView android:id="#+id/img_user_card_2"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_1"
/>
<ImageView android:id="#+id/img_user_card_3"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_2"
/>
</RelativeLayout>
</RelativeLayout>
here is nice example from link. i hope this will help you.
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center" android:id="#+id/LinearLayout01">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/btn"
android:text="Drag Me"></Button>
</FrameLayout>
src/com/beanie/example/Home.java
package com.beanie.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;
public class Home extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private Button btn;
private FrameLayout layout;
private int status;
private LayoutParams params;
private ImageView image;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(btn.getDrawingCache());
layout.addView(image, params);
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
}

Android: View crashes when I have two EditText boxes with #+id defined in the xml

Another newbie question here. I'm trying to create a form which takes several text input fields from the user, however my view keeps crashing/failing to load with the following error reported in the log:
Window already focused, ignoring focus gain of:com.android.internal.view.IInputMethodClient$Stub$Proxy#628a9148
This is what my xml looks like (this crashes)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">"
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter contact details\n\nName:"/>
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
android:hint="#string/compose_name"></EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nSurname:"/>
<EditText
android:id="#+id/surname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_below="#id/name"
android:maxLength="30"
android:maxLines="1"
android:hint="#string/compose_surname"></EditText>-->
<Button
android:id="#+id/new_contact_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
</ScrollView>
However if I remove just the line:
android:id="#+id/surname"
It works (well at least it loads the view, of course I can't access the content in the EditText field without creating an id for it).
The view also fails if I add #+id tags to both the TextView fields (but works if I add only one).
What's going on here? I thought you should be able to label multiple view fields in your UI (all the examples in my book let me do this)?
I'm using NetBeans to develop on.
EDIT: Adding Javacode:
package org.me.savingsdepositrecord;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class NewContact extends Activity
implements OnClickListener, View.OnKeyListener {
//private EditText nameField;
//private String name;
private Button submitButton;
EditText nameField = null;
EditText surnameField = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_contact);
nameField = (EditText) findViewById(R.id.name);
nameField.setOnKeyListener(this);
surnameField = (EditText) findViewById(R.id.surname);
submitButton = (Button) findViewById(R.id.new_contact_button);
// Set up click listeners for all the buttons
submitButton.setOnClickListener(this);
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String name = nameField.getText().toString();
String surname = surnameField.getText().toString();
// TODO: Save Nickname setting (strNicknameToSave)
return true;
}
return false;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.new_contact_button:
Intent i = new Intent(this, MainActivity.class);
finish();
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
Yes, you can label multiple fields. It might be a good to take out the android:layout_below attribute since that isn't supported in LinearLayout.
Otherwise, your Java code might be the culprit. Can you post it as well?
Edit: I also tested your code and found no errors, although I had to remove the '-->' and the #string references.

Android - onKey events don't fire

I'm having a problem where it seems I can't get key events to fire at all in the emulator (it was working before, but, somehow.. something broke).
Here is some sample code that should fill the second text box with the text of the first when a key is pressed in the first (or if the button is clicked). But it doesn't do that.
Am I doing something wrong?
Activity class:
package abc.def;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class Fill extends Activity {
/** Called when the activity is first created. */
EditText e1;
EditText e2;
Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1 = (EditText) findViewById(R.id.EditText01);
e2 = (EditText) findViewById(R.id.EditText02);
b = (Button) findViewById(R.id.Button01);
e1.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
e2.setText(e1.getText());
return false;
}
});
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
e2.setText(e1.getText());
}
});
}
}
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<EditText android:text="" android:id="#+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
<EditText android:text="" android:id="#+id/EditText02" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
<Button android:text="Click" android:id="#+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
You should use e1.getText().toString() to get result.
LogCat will be useful to find out the values that are passed around. You can add the following LogCat for finding out similar issues
Log.d("SOMENAME",e1.getText().toString);
I got this to work by implementing TextWatcher and using addTextChangedListener() instead. No clue why this works when the onKeyListener does not - I suspect the keyboard events aren't even reaching my widget for some reason..

Categories

Resources