Code example for GridView

Methods: setSelection

0
         
        final int targetPosition = mGridView.getAdapter().getCount() / 2;
         
        mActivity.runOnUiThread(new Runnable() {
            public void run() { 
                mGridView.setSelection(targetPosition);
            } 
        }); 
        getInstrumentation().waitForIdleSync();
         
        boolean found = false;
        int childCount = mGridView.getChildCount();
        for (int i=0; i<childCount; i++) {
            View child = mGridView.getChildAt(i);
            if (child.getId() == targetPosition) {
                found = true;
                break; 
            } 
        } 
        assertTrue("Selected item not visible in list", found);
Stop searching for code, let great code find you!  Add Codota to your java IDE