Example usage for android.view View requestFocus

List of usage examples for android.view View requestFocus

Introduction

In this page you can find the example usage for android.view View requestFocus.

Prototype

public final boolean requestFocus() 

Source Link

Document

Call this to try to give focus to a specific view or to one of its descendants.

Usage

From source file:android.support.v17.leanback.widget.GridWidgetTest.java

public void testFocusOutOfEmptyListView() throws Throwable {

    mInstrumentation = getInstrumentation();
    Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
    intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.horizontal_linear);
    final int numItems = 100;
    intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, numItems);
    intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
    mOrientation = BaseGridView.HORIZONTAL;
    mNumRows = 1;//from ww w .j a  v a  2s  . c o m
    initActivity(intent);

    final View horizontalGridView = new HorizontalGridViewEx(mGridView.getContext());
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            horizontalGridView.setFocusable(true);
            horizontalGridView.setFocusableInTouchMode(true);
            horizontalGridView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
            ((ViewGroup) mGridView.getParent()).addView(horizontalGridView, 0);
            horizontalGridView.requestFocus();
        }
    });

    assertTrue(horizontalGridView.isFocused());

    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);

    assertTrue(mGridView.hasFocus());
}

From source file:com.android.launcher3.folder.Folder.java

@Thunk
void replaceFolderWithFinalItem() {
    // Add the last remaining child to the workspace in place of the folder
    Runnable onCompleteRunnable = new Runnable() {
        @Override//from   w  w  w  .j  a  va  2  s.com
        public void run() {
            int itemCount = mInfo.contents.size();
            if (itemCount <= 1) {
                View newIcon = null;

                if (itemCount == 1) {
                    // Move the item from the folder to the workspace, in the position of the
                    // folder
                    CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
                    ShortcutInfo finalItem = mInfo.contents.remove(0);
                    newIcon = mLauncher.createShortcut(cellLayout, finalItem);
                    LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container, mInfo.screenId,
                            mInfo.cellX, mInfo.cellY);
                }

                // Remove the folder
                mLauncher.removeItem(mFolderIcon, mInfo, true /* deleteFromDb */);
                if (mFolderIcon instanceof DropTarget) {
                    mDragController.removeDropTarget((DropTarget) mFolderIcon);
                }

                if (newIcon != null) {
                    // We add the child after removing the folder to prevent both from existing
                    // at the same time in the CellLayout.  We need to add the new item with
                    // addInScreenFromBind() to ensure that hotseat items are placed correctly.
                    mLauncher.getWorkspace().addInScreenFromBind(newIcon, mInfo.container, mInfo.screenId,
                            mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);

                    // Focus the newly created child
                    newIcon.requestFocus();
                }
            }
        }
    };
    View finalChild = mContent.getLastItem();
    if (finalChild != null) {
        mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
    } else {
        onCompleteRunnable.run();
    }
    mDestroyed = true;
}

From source file:org.chromium.chrome.browser.tab.Tab.java

/**
 * Request that this tab receive focus. Currently, this function requests focus for the main
 * View (usually a ContentView)./*from   w  w w  .  j  a v a  2  s.c  om*/
 */
public void requestFocus() {
    View view = getView();
    if (view != null)
        view.requestFocus();
}

From source file:com.huangj.huangjlibrary.widget.drawerlayout.DrawerLayout.java

void dispatchOnDrawerOpened(View drawerView) {
    final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
    if ((lp.openState & LayoutParams.FLAG_IS_OPENED) == 0) {
        lp.openState = LayoutParams.FLAG_IS_OPENED;
        if (mListeners != null) {
            // Notify the listeners. Do that from the end of the list so that if a listener
            // removes itself as the result of being called, it won't mess up with our iteration
            int listenerCount = mListeners.size();
            for (int i = listenerCount - 1; i >= 0; i--) {
                mListeners.get(i).onDrawerOpened(drawerView);
            }//from ww w.  jav a  2  s . c o  m
        }

        updateChildrenImportantForAccessibility(drawerView, true);

        // Only send WINDOW_STATE_CHANGE if the host has window focus.
        if (hasWindowFocus()) {
            sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
        }

        drawerView.requestFocus();
    }
}

From source file:org.bangbang.support.v4.widget.VerticalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;//from   w  ww .  j a  v  a2 s .  c o  m

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_DOWN) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextBottom = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
            final int currBottom = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
            if (currentFocused != null && nextBottom >= currBottom) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_UP) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_DOWN || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_UP || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.gome.ecmall.custom.VerticalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;/*ww  w  .j  a v a 2s. co m*/

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_UP) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page
            // left.
            final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextTop >= currTop) {
                handled = pageTop();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page
            // right.
            final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextTop <= currTop) {
                handled = pageBottom();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageTop();
    } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageBottom();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.brantapps.viewpagerindicator.vertical.VerticalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;//from  w ww .j av a 2s  .c  o m

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_UP) {
            // If there is nothing above, or this is causing us to
            // jump down, then what we really want to do is page up.
            final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextTop >= currTop) {
                handled = pageUp();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            // If there is nothing below, or this is causing us to
            // jump up, then what we really want to do is page down.
            final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextTop <= currTop) {
                handled = pageDown();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move up and nothing there; try to page.
        handled = pageUp();
    } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move down and nothing there; try to page.
        handled = pageDown();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:ael.com.loterias.c_login.java

public void PRC_INTENTAR_REGISTRO() {

    // Si el check est pinchado recordar en las preferences
    PRC_RECORDAR_MAIL_PASSWORD();/*from   w  w w  . j  a  v  a  2  s .co  m*/

    mRetypePasswordView = (EditText) findViewById(R.id.retype_password);
    mButtonRegister = (Button) findViewById(R.id.register_button);

    if (mRetypePasswordView.getVisibility() == View.VISIBLE) {

        boolean cancel = false;
        View focusView = null;

        // Reset errors.
        mEmailView.setError(null);
        mPasswordView.setError(null);
        mRetypePasswordView.setError(null);

        // Store values at the time of the login attempt.
        mEmail = mEmailView.getText().toString();
        mPassword = mPasswordView.getText().toString();
        mRetypePassword = mRetypePasswordView.getText().toString();

        // Check for a valid password.
        if (TextUtils.isEmpty(mPassword)) {
            mPasswordView.setError(getString(R.string.st_la_Field_Required));
            focusView = mPasswordView;
            cancel = true;
        } else if (TextUtils.isEmpty(mRetypePassword)) {
            mRetypePasswordView.setError(getString(R.string.st_la_Field_Required));
            focusView = mRetypePasswordView;
            cancel = true;
        } else if (mPassword.length() < c_login.LENGTH_PASSWORD) {
            mPasswordView.setError(getString(R.string.st_la_Invalid_Password));
            focusView = mPasswordView;
            cancel = true;
        } else if (mRetypePassword.length() < c_login.LENGTH_PASSWORD) {
            mRetypePasswordView.setError(getString(R.string.st_la_Invalid_Password));
            focusView = mRetypePasswordView;
            cancel = true;
        }

        // mPasswordView.setError("*" + mRetypePassword + "-" + mPassword + "*");
        // Check for a valid email address.
        if (TextUtils.isEmpty(mEmail)) {
            mEmailView.setError(getString(R.string.st_la_Field_Required));
            focusView = mEmailView;
            cancel = true;
        } else if (!mEmail.contains("@")) {
            mEmailView.setError(getString(R.string.st_la_Invalid_Email));
            focusView = mEmailView;
            cancel = true;
        } else if (!isValidEmail(mEmail)) {
            mEmailView.setError(getString(R.string.st_la_Email_Error));
            focusView = mEmailView;
            cancel = true;
        } else if (!mPassword.equals(mRetypePassword)) {
            mPasswordView.setError(getString(R.string.st_la_Password_Not_Mistmach));
            focusView = mPasswordView;
            cancel = true;
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
            c_logging.getInstance().log(c_logging.LOG_INFO, "Cancelado");
        } else {
            // Show a progress spinner, and kick off a background task to
            // perform the user login attempt.
            // Anulado temporalmente
            // PRC_CARGAR_LOGIN_TASK(context, getString(R.string.st_la_Check_Connection_Thinking), getString(R.string.STR_OPERATION_REGISTER_USER));
        }

    } else {
        mRetypePasswordView.setVisibility(View.VISIBLE);
        mButtonRegister.setText(R.string.st_la_Nuevo);
    }
}

From source file:com.dian.diabetes.widget.VerticalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;//from w  w w  .ja  va 2  s  .  c  o  m
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_UP) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp >= currUp) {
                handled = pageUp();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageDown();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageUp();
    } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageDown();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:android.support.custom.view.VerticalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;//w  w  w  . j a va 2 s  .c  om

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_UP) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.

            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;

            if (currentFocused != null && nextUp >= currUp) {
                handled = pageUp();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.

            final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
            final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
            if (currentFocused != null && nextDown <= currDown) {
                handled = pageDown();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageUp();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageDown();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}