Skip to content Skip to sidebar Skip to footer

Set/disable Focus On A Certain Item In Gridview

I have a GridView with items which I defined in an adapter. Those items are composted by TextView and ImageView. I want to control the focus on GridView, It looks like this: (1) (2

Solution 1:

So for external keyboard i have one idea.

1.Declare one map

Map<View,View>right=new HashMap<View,View>();
 View currentViewFocus;

2.Assign value of android UI control

void  putValueOnDownMap(){
          down.put(androidautocomextview1,textview2);
        down.put(textview2, editetxt3);
        down.put(editetxt3, button4);
            down.put(button4, androidautocomextview1);
        } 

3. On key callback do this

     View currentViewFocus =  androidautocomextview1;
@Override  
public boolean onKeyUp(int keyCode, KeyEvent event) {
     if(productname.hasFocus()){  
                 if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
             View temp = currentViewFocus;

             if(temp.getClass().equals(AutoCompleteTextView.class)){
/**
* First it will check that view class object belongs to which child class if it *belongs to autoComplete textview then it will type-cast super class object to child *class and call requestFocus() method to get focus.
*/

            AutoCompleteTextView auto_temp = (AutoCompleteTextView)temp;

                 if(auto_temp.isPopupShowing()){   
/**If popup is showing then on press of down D-pad in popup it should be remain 
*     navigate on popup
*/
                     auto_temp.requestFocus();
                 }
                 else{       

/**Now we made our selection from autocomplete textview popup by “Enter key”
 *Then time to navigate in  textview2 . Pass currentViewFocus as key and get value *from down map again assign new value in   currentViewFocus again check it child *class again type-cast and so on.
*/
                     temp = down.get(currentViewFocus);   
                     currentViewFocus = temp;
                     if (temp == null){
                     }
                     else{
                         if(temp.getClass().equals(TextView.class)){
                             TextView txtv = (TextView)temp;

                                 txtv.requestFocus();
                                 currentViewFocus = temp;


                         }
                         else{    
                             temp.performClick();
                             temp.requestFocus();

                             currentViewFocus = temp;

                         } 
                     }


                 }      
             }    
             else{ 
                 // and so on we first check that class of super class // type-cast according to child class//  perform stub.
                }
                 }
//=====================================================================/** So-far we are  navigating through D-pad down key now suppose that our current 
*   focus on  editetxt3 and we want to navigate through D-pad UP key.
*/if(keyCode == KeyEvent.KEYCODE_DPAD_UP){

                      return super.onKeyUp(keyCode, event); 
        }

Solution 2:

if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                    && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                if (mType == 0 && currentPos == (disablePos + 1) && disablePos >= 1) {
                    ((GridView) v).getChildAt(disablePos).setSelected(false);
                    ((GridView) v).getChildAt(disablePos - 1).requestFocus();
                    ((GridView) v).setSelection(disablePos - 1);
                    returntrue;
                } else {
                    returnfalse;
                }if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                    && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                if (mType == 0 && currentPos == (disablePos + 1) && disablePos >= 1) {
                    ((GridView) v).getChildAt(disablePos).setSelected(false);
                    ((GridView) v).getChildAt(disablePos - 1).requestFocus();
                    ((GridView) v).setSelection(disablePos - 1);
                    returntrue;
                } else {
                    returnfalse;
                }

Post a Comment for "Set/disable Focus On A Certain Item In Gridview"