1

How do I make all Spinners in my Fragment focusable?

Setting android:focusableInTouchMode and android:focusable in my layout's XML is having no effect.

More generally, I'm having trouble looping through my Fragment's controls and finding all controls of a specific type, such as all Spinners or all EditTexts.

rmirabelle
  • 6,268
  • 7
  • 45
  • 42

2 Answers2

3

This was very tricky for me, so I figured I would post my solution here. This solves a particular problem (how to make spinners focusable) but also addresses a more general problem (how to loop through controls in a fragment.

public class MyFragment extends Fragment {

    private static ArrayList<Spinner> spinners = new ArrayList<Spinner>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // inflate the layout
        View layout = inflater.inflate(R.layout.my_fragment_xml, container, false);

        // cast our newly inflated layout to a ViewGroup so we can 
        // enable looping through its children
        set_spinners((ViewGroup)layout);

        // now we can make them focusable
        make_spinners_focusable();

        return layout;
    }

    //find all spinners and add them to our static array

    private void set_spinners(ViewGroup container) {
        int count = container.getChildCount();
        for (int i = 0; i < count; i++) {
            View v = container.getChildAt(i);
            if (v instanceof Spinner) {
                spinners.add((Spinner) v);
            } else if (v instanceof ViewGroup) {
                //recurse through children
                set_spinners((ViewGroup) v);
            }
        }
    }

    //make all spinners in this fragment focusable
    //we are forced to do this in code

    private void make_spinners_focusable() {            
        for (Spinner s : spinners) {
            s.setFocusable(true);
            s.setFocusableInTouchMode(true);
            s.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.performClick();
                    }
                }
            });
        }
    }


}
rmirabelle
  • 6,268
  • 7
  • 45
  • 42
1

The chosen answer with set_spinners() does not work if there are nested layouts in the activity. getChildCount() gives only first level childs. Better to use getAllChildrenBFS from this answer :

private List<View> getAllChildrenBFS(View v) {
    List<View> visited = new ArrayList<View>();
    List<View> unvisited = new ArrayList<View>();
    unvisited.add(v);

    while (!unvisited.isEmpty()) {
        View child = unvisited.remove(0);
        visited.add(child);
        if (!(child instanceof ViewGroup)) continue;
        ViewGroup group = (ViewGroup) child;
        final int childCount = group.getChildCount();
        for (int i=0; i<childCount; i++) unvisited.add(group.getChildAt(i));
    }

    return visited;
}
Community
  • 1
  • 1
Yahia
  • 805
  • 7
  • 25