AlertDialog OnKeyListener - How to get Focused Item?

by coderiver » Mon, 22 Dec 2008 10:12:26 GMT


Sponsored Links
 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!

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



AlertDialog OnKeyListener - How to get Focused Item?

by coderiver » Tue, 23 Dec 2008 09:28:41 GMT


 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!



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


Sponsored Links


AlertDialog OnKeyListener - How to get Focused Item?

by coderiver » Sat, 27 Dec 2008 21:31:37 GMT


 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.
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Query related to adding a new flag in the WindowManager (Similar to FLAG_IGNORE_CHEEK_PRESSES)

Hi All,

I am working on an adding a FLAG similar to FLAG_IGNORE_CHEEK_PRESSES.
I have added an entry in the WindowManager.java for my flag and in the
Phone application activity incallscreen.java i have added this flag
along with FLAG_IGNORE_CHEEK_PRESSES using
getWindow().addFlags(WindowManager.LayoutParams.MY_FLAG_ENTRY). Also
in the dispatchPointer method of WindowManagerService.java i am
checking for this current window flag along with
FLAG_IGNORE_CHEEK_PRESSES.

I have following queries:
1. Is this only i need to do to create a new flag and application can
then register for this one ?
2. Also i commented the statement
getWindow().addFlags(WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES)
in the InCallScreen.java but still i could see that the condition
if ((target.mAttrs.flags &
 
WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
being satisfied. Can someone let me about this?

Thanks in advance
Rahul

-- 

2. Batere N1

dear para suhu,
mo konsultasi nih
N1 saya rasanya cepat sekali abis batere nya. dari malam hari dalam keadaan
fully charged -- dikantor tidak dicolek2 -- begitu pulang kantor sudah
kuning. jadi penasaran kira2 apa yang jalan di background hingga nyedot
batere nya ya?
adakah tips untuk bikin batere N1 lebih awet?

lalu kalo sedang di charge, berasa hangat cenderung ke panas ya. apakah
rekan2 yang lain juga mengalami yang sama?

thanks

-kujo-

-- 
"Indonesian Android Community [id-android]" 

3. White Paper - Sony Ericsson Vivaz, the HD ready phone

4. OOT: lagi di gedung indosat

5. Need help on how to execute a ARM executable on Zoom2 board

6. Solved Wta: nandroid backup

7. CHEEK_EVENT flag