Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Sat, 07 Mar 2009 10:53:20 GMT


Sponsored Links
  have a list activity that creates a header row above the data rows
from the adapter. I want to receive click events when the user selects
the header or a data row (but my data rows have check boxes in them,
so this part is tricky). When I use the touch screen, I get this
expected behavior:

1. Tapping on any row causes a click event and a dialog appears.
2. Long-pressing on a data row causes a context menu to appear.
3. Long-pressing on the header row does *not* show the context menu.

However, when I use the arrow keys (emulator) or trackball (G1), I get
this unexpected behavior:

1. Selecting any row fails to cause any click events, even though the
row's appearance changes like it's being clicked.
2. Long-pressing on the header row *does* show the context menu, which
I don't want.
3. Occasionally, it doesn't focus the correct row when I move up or
down (e.g. it skips from the header to the last data row).

What am I doing wrong?

Jesse

(Complete project: http://hansprestige.com/android/DpadBug.zip)

////////// DpadActivity.java //////////

package com.hansprestige.DpadBug;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DpadBugActivity extends ListActivity {
private static final int DIALOG_TEST = 1;

private OnClickListener testListener = new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_TEST);
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView lv = getListView();
registerForContextMenu(lv);

findViewById(android.R.id.empty).setOnClickListener
(testListener);

View addItem = getLayoutInflater().inflate
(R.layout.list_header,
null, false);
addItem.setOnClickListener(testListener);
lv.addHeaderView(addItem);

fillData();
}

private void fillData()
{
String[] items = new String[] { "Item A", "Item B", "Item C" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_row, R.id.TextView01, items) {
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
View v = super.getView(position, convertView, parent);
v.setOnClickListener(testListener);
v.setLongClickable(true);
return v;
}
};
setListAdapter(adapter);
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEST:
return new AlertDialog.Builder(this)
.setTitle("Hello!")
.setPositiveButton("Close", null)
.create();
}
return super.onCreateDialog(id);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
Context



Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Wed, 11 Mar 2009 08:31:10 GMT


 nybody?

On Mar 5, 6:54pm, Jesse McGrew <jmcg...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Trackball doesn't give me click events, but touch screen does

by Streets Of Boston » Wed, 11 Mar 2009 12:17:51 GMT


 'm not sure, but try this:
Add a key-event listener to your item instead of the onClick:
...
...
v.setOnKeyListener(myKeyListener);
...

And in that listener, handle the key-codes KeyEvent.KEYCODE_DPAD_x
where x = CENTER, DOWN, UP, LEFT or RIGHT.


On Mar 10, 8:30pm, Jesse McGrew <jmcg...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Trackball doesn't give me click events, but touch screen does

by Romain Guy (Google) » Thu, 12 Mar 2009 07:39:03 GMT


 i Jesse,

You should **not** set individual click listeners on the list items.
This will cause all sort of problems, like the ones you're seeing. The
main reason behind that is that ListView handles clicks/long clicks on
the items already. Instead you should use
ListView.setOnItemClickListener().

On Mar 5, 6:54pm, Jesse McGrew <jmcg...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Thu, 12 Mar 2009 08:57:48 GMT


 omain,

Thanks for your response. I'm not sure if that approach will work,
though. My ListActivity's onListItemClick() method is called when I
click on an item with the trackball, but *not* when I touch it.

Perhaps the root problem is that my list rows have focusable items in
them (check boxes). The built-in Alarm Clock application has something
similar, though, and I can see from the source code that Alarm Clock
sets individual click listeners on the check boxes and the clock
views.

I'm trying to get the same behavior as Alarm Clock, where tapping on a
list row brings up an activity to edit the row, and the check box can
only be selected by tapping on the check box itself. I think that
precludes using multiple-choice mode for the list view, correct?

Jesse

On Mar 11, 4:38pm, "Romain Guy (Google)" <romain...@google.com>
wrote:
--~--~---------~--~----~------------~-------~--~----~



Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Thu, 12 Mar 2009 09:07:26 GMT


 


It looks like I can handle both cases by setting a click listener on
the row *and* responding to onListItemClick().

Now I wonder if there's a way to make the context menu only appear for
data rows, not the list header row. (This works as expected with
touch, but with the trackball, I still get a context menu on the
header row.)

Jesse
--~--~---------~--~----~------------~-------~--~----~



Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Thu, 12 Mar 2009 09:28:57 GMT


 


Interesting. Unfortunately I can't find any examples of TouchDelegate,
and the class documentation is confusing.

I gather that I need to pass the bounds of the check box (relative to
its container) into the TouchDelegate constructor. Once I have a
TouchDelegate instance, what do I do with it? I see a method called
setTouchDelegate() - do I call that on the check box, the list row, or
the view group that directly contains the check box? And is that
method enough to get the touch events relayed to the delegate, or do I
need to listen for touch events and forward them myself?

Jesse
--~--~---------~--~----~------------~-------~--~----~



Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Thu, 12 Mar 2009 10:01:31 GMT


 




Also: would it still be possible to focus the check box using the
trackball? I assumed TouchDelegates were only for touch, and so the
trackball would only be able to move between list rows. In AlarmClock,
the trackball can move horizontally between the clock view and check
box inside a row, as well as moving vertically between rows.

Jesse
--~--~---------~--~----~------------~-------~--~----~



Trackball doesn't give me click events, but touch screen does

by Jesse McGrew » Thu, 12 Mar 2009 11:07:36 GMT


 Aha! Adding a call to that method fixes both the context menu problem
and the click handler problem. Thanks.

Jesse







--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. How do we get the hardware id?

I've looked through a lot of the Android code and I can't find a
method to get the hardware id of the device where our app is
installed. How do we access it from our apps?

Thanks,

Jade
--~--~---------~--~----~------------~-------~--~----~

2. Compile system image and flashing to G1,But no sound.

I compiled android source code to system image, and flash to my
android dev phone 1,but no sounds out,such as can not playback music
out, who can tell me how to fix it?
--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

3. cupcake(android1.5): HOME key is not working

4. android livecd

5. Showing travel path which will mach the real road on map.

6. How to make second DDMS work on the PC which already running a DDMS?

7. Removing the default interpolator for xml based animation effects for views