[android-developers] Re: AlertDialog OnKeyListener - How to get Focused Item?

2008-12-27 Thread coderiver

I figured this out so I thought I would share.  The handleKeyPress
method that is called from my OnKey handler is part of my Activity
object.  So "this" is an activity not a dialog.  So to get the focused
item is pretty simple, I needed to keep a reference to the AlertDialog
(mAlertDialog in the example):

public boolean handleKeyPress(DialogInterface dialog, int keyCode,
KeyEvent event) {

 ListView lv = mAlertDialog.getListView();
 if( lv != null ) {
 int selectedPos = lv.getSelectedItemPosition();
 Log.d("keyPress", "handleKeyPress : selectedPos: " +
selectedPos);
 }

You also have to test for the right key and only choose the down
button action, but this is the part I had trouble with.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: AlertDialog OnKeyListener - How to get Focused Item?

2008-12-22 Thread coderiver

I am thinking I need to override the dialog and track the position of
the focused item myself.
That seems kind of risky in case they ever get out of synch.  Am I
missing something obvious?

Thanks!

On Dec 20, 1:52 pm, coderiver  wrote:
> I have an AlertDialog with an OnKeyListener.  I need to find the
> focused item when the Call key is pressed.
> This AlertDialog also has an OnClickListener that works great - I get
> the position of the item from my view when it is clicked.
> What I also want to do - is get the position of the currently
> highlighted (focused right?) item when the CALL key is pressed.
> I can detected the CALL key event in my handler - no problem.  But I
> do not know how to get the item that had the focus when the key was
> pressed.
>
> Here is my OnKey handler and some of the things I have used to try and
> get info
>
>     public boolean handleKeyPress(DialogInterface dialog, int keyCode,
> KeyEvent event) {
>
>         Log.d("keyPress", "handleKeyPress :
> this.getSelectedItemPosition(): " + this.getSelectedItemPosition());
>         Log.d("keyPress", "handleKeyPress : this.getSelectedItemId():
> " + this.getSelectedItemId());
>
>         AlertDialog ad = (AlertDialog)dialog;
>         View fv = this.getCurrentFocus();
>         if( fv != null ) {
>              Log.d("keyPress", "handleKeyPress  : fv.getId(): " +
> fv.getId());
>         }
>
>         ListView lv = this.getListView();
>         if( lv != null ) {
>             View fvC = lv.getFocusedChild();
>             if( fvC != null ) {
>                 Log.d("keyPress", "handleKeyPress : fvC.getId(): " +
> fvC.getId());
>             }
>             int selectedPos = lv.getSelectedItemPosition();
>             Log.d("keyPress", "handleKeyPress : selectedPos: " +
> selectedPos);
>         }
>
>         return false;
>     }
>
> Thanks for your help!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] AlertDialog OnKeyListener - How to get Focused Item?

2008-12-21 Thread coderiver

I have an AlertDialog with an OnKeyListener.  I need to find the
focused item when the Call key is pressed.
This AlertDialog also has an OnClickListener that works great - I get
the position of the item from my view when it is clicked.
What I also want to do - is get the position of the currently
highlighted (focused right?) item when the CALL key is pressed.
I can detected the CALL key event in my handler - no problem.  But I
do not know how to get the item that had the focus when the key was
pressed.

Here is my OnKey handler and some of the things I have used to try and
get info

public boolean handleKeyPress(DialogInterface dialog, int keyCode,
KeyEvent event) {

Log.d("keyPress", "handleKeyPress :
this.getSelectedItemPosition(): " + this.getSelectedItemPosition());
Log.d("keyPress", "handleKeyPress : this.getSelectedItemId():
" + this.getSelectedItemId());

AlertDialog ad = (AlertDialog)dialog;
View fv = this.getCurrentFocus();
if( fv != null ) {
 Log.d("keyPress", "handleKeyPress  : fv.getId(): " +
fv.getId());
}

ListView lv = this.getListView();
if( lv != null ) {
View fvC = lv.getFocusedChild();
if( fvC != null ) {
Log.d("keyPress", "handleKeyPress : fvC.getId(): " +
fvC.getId());
}
int selectedPos = lv.getSelectedItemPosition();
Log.d("keyPress", "handleKeyPress : selectedPos: " +
selectedPos);
}

return false;
}



Thanks for your help!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---