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:com.tct.mail.ui.ConversationViewFragment.java

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        mOriginalKeyedView = view;//from   ww w . j a  v  a2  s  .  com
    }

    if (mOriginalKeyedView != null) {
        final int id = mOriginalKeyedView.getId();
        final boolean isActionUp = keyEvent.getAction() == KeyEvent.ACTION_UP;
        final boolean isLeft = keyCode == KeyEvent.KEYCODE_DPAD_LEFT;
        final boolean isRight = keyCode == KeyEvent.KEYCODE_DPAD_RIGHT;
        final boolean isUp = keyCode == KeyEvent.KEYCODE_DPAD_UP;
        final boolean isDown = keyCode == KeyEvent.KEYCODE_DPAD_DOWN;

        // First we run the event by the controller
        // We manually check if the view+direction combination should shift focus away from the
        // conversation view to the thread list in two-pane landscape mode.
        final boolean isTwoPaneLand = mNavigationController.isTwoPaneLandscape();
        final boolean navigateAway = mConversationContainer.shouldNavigateAway(id, isLeft, isTwoPaneLand);
        if (mNavigationController.onInterceptKeyFromCV(keyCode, keyEvent, navigateAway)) {
            return true;
        }

        // If controller didn't handle the event, check directional interception.
        if ((isLeft || isRight)
                && mConversationContainer.shouldInterceptLeftRightEvents(id, isLeft, isRight, isTwoPaneLand)) {
            return true;
        } else if (isUp || isDown) {
            // We don't do anything on up/down for overlay
            if (id == R.id.conversation_topmost_overlay) {
                return true;
            }

            // We manually handle up/down navigation through the overlay items because the
            // system's default isn't optimal for two-pane landscape since it's not a real list.
            final int position = mConversationContainer.getViewPosition(mOriginalKeyedView);
            final View next = mConversationContainer.getNextOverlayView(position, isDown);
            if (next != null) {
                if (isActionUp) {
                    next.requestFocus();

                    // Make sure that v is in view
                    final int[] coords = new int[2];
                    next.getLocationOnScreen(coords);
                    final int bottom = coords[1] + next.getHeight();
                    if (bottom > mMaxScreenHeight) {
                        mWebView.scrollBy(0, bottom - mMaxScreenHeight);
                    } else if (coords[1] < mTopOfVisibleScreen) {
                        mWebView.scrollBy(0, coords[1] - mTopOfVisibleScreen);
                    }
                }
                return true;
            } else {
                // Special case two end points
                // Start is marked as index 1 because we are currently not allowing focus on
                // conversation view header.
                if ((position == mConversationContainer.getOverlayCount() - 1 && isDown)
                        || (position == 1 && isUp)) {
                    mTopmostOverlay.requestFocus();
                    // Scroll to the the top if we hit the first item
                    if (isUp) {
                        mWebView.scrollTo(0, 0);
                    }
                    return true;
                }
            }
        }

        // Finally we handle the special keys
        if (keyCode == KeyEvent.KEYCODE_BACK && id != R.id.conversation_topmost_overlay) {
            if (isActionUp) {
                mTopmostOverlay.requestFocus();
            }
            //TS: wenggangjin 2014-11-21 EMAIL BUGFIX_845619 MOD_S
            //                return true;
            return false;
            //TS: wenggangjin 2014-11-21 EMAIL BUGFIX_845619 MOD_E
        } else if (keyCode == KeyEvent.KEYCODE_ENTER && id == R.id.conversation_topmost_overlay) {
            if (isActionUp) {
                mConversationContainer.focusFirstMessageHeader();
                mWebView.scrollTo(0, 0);
            }
            return true;
        }
    }
    return false;
}

From source file:com.linkbubble.ui.ContentView.java

void setTabAsActive() {
    try {/*from w  w  w.  j  av a  2  s.c  om*/
        if (mUrlTextView.getText().toString().equals(getContext().getString(R.string.empty_bubble_page))) {
            mTitleTextView.performClick();
        } else {
            View view = mWebRenderer.getView();
            if (null != view) {
                view.requestFocus();
            }
        }
    } catch (NullPointerException exc) {
        // We have that exception sometimes inside Android SDK on requestFocus,
        // we would better to not get focus than crash
    }
}

From source file:com.tylz.jiaoyanglogistics.view.LazyViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;/*  www  .j  a v 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_LEFT) {
            // 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.
            if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
                handled = pageLeft();
            } 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.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || 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.android.ex.chips.RecipientEditTextView.java

private boolean focusNext() {
    final View next = focusSearch(View.FOCUS_DOWN);
    if (next != null) {
        next.requestFocus();
        return true;
    }/*from w w w.j  a  v  a2  s  . com*/
    return false;
}

From source file:com.tencent.tws.assistant.support.v4.view.ViewPager.java

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

    boolean handled = false;
    // tws-start::modify this view must be descendant 20141230
    if (currentFocused != null) {
        ViewParent mParent = currentFocused.getParent();
        if (mParent == null) {
            return handled;
        }
    }
    // tws-end::modify this view must be descendant 20141230
    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // 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.
            if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
                handled = pageLeft();
            } 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.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || 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:de.uni_koblenz_landau.apow.LoginActivity.java

/**
 * Checks input and setups database.//from  ww  w.  j a  va  2 s  .  c o  m
 */
public void setup(View view) {
    if (mSetupTask != null || !mFirstRun) {
        return;
    }

    // Reset errors.
    mSetupDatabasePasswordView.setError(null);
    mSetupDatabaseConfirmView.setError(null);
    mSetupServerUsernameView.setError(null);
    mSetupServerPasswordView.setError(null);

    // Store UI values.
    mDatabasePassword = mSetupDatabasePasswordView.getText().toString();
    mDatabaseConfirm = mSetupDatabaseConfirmView.getText().toString();
    mServerUsername = mSetupServerUsernameView.getText().toString();
    mServerPassword = mSetupServerPasswordView.getText().toString();

    Boolean cancel = false;
    View focusView = null;

    // Check input.
    if (mSetupLocationView.getAdapter() != null) {
        mLocationID = ((ListViewItem) mSetupLocationView.getSelectedItem()).getId();
    } else {
        Toast.makeText(this, R.string.error_connection, Toast.LENGTH_SHORT).show();
        focusView = mSetupLocationView;
        cancel = true;
    }

    if (TextUtils.isEmpty(mDatabasePassword)) {
        mSetupDatabasePasswordView.setError(getString(R.string.error_field_required));
        focusView = mSetupDatabasePasswordView;
        cancel = true;
    } else if (mDatabasePassword.length() < 4) {
        mSetupDatabasePasswordView.setError(getString(R.string.error_invalid_password));
        focusView = mSetupDatabasePasswordView;
        cancel = true;
    }

    if (TextUtils.isEmpty(mDatabaseConfirm)) {
        mSetupDatabaseConfirmView.setError(getString(R.string.error_field_required));
        focusView = mSetupDatabaseConfirmView;
        cancel = true;
    } else if (!mDatabaseConfirm.equals(mDatabasePassword)) {
        mSetupDatabaseConfirmView.setError(getString(R.string.error_confirm_password));
        focusView = mSetupDatabaseConfirmView;
        cancel = true;
    }

    if (TextUtils.isEmpty(mServerUsername)) {
        mSetupServerUsernameView.setError(getString(R.string.error_field_required));
        focusView = mSetupServerUsernameView;
        cancel = true;
    }

    if (TextUtils.isEmpty(mServerPassword)) {
        mSetupServerPasswordView.setError(getString(R.string.error_field_required));
        focusView = mSetupServerPasswordView;
        cancel = true;
    }

    if (TextUtils.isEmpty(mNFCTag)) {
        mSetupNFCText.setError(getString(R.string.error_no_tag));
        focusView = mSetupNFCText;
        cancel = true;
    }

    // When there are no error start login task.
    if (cancel) {
        focusView.requestFocus();
    } else {
        mStatusMessageView.setText(R.string.login_setup_progress);
        showProgress(true);
        mSetupTask = new SetupTask(Helper.hash(mNFCTag + mDatabasePassword), mServerUsername, mServerPassword,
                mLocationID);
        TaskActivityReference.getInstance().addTask(SetupTask.TASK_ID, mSetupTask, this);
        mSetupTask.execute();
    }
}

From source file:com.ubiLive.GameCloud.Browser.WebBrowser.java

/** open new browser in UI thread */
private void handleOpenNewWindow(String url) {
    ChildBrowser mChildBrowser = new ChildBrowser(Utils.checkUrl(url), mContext, this, mChildViewList);
    View currentView = mChildBrowser.getChildView();
    mRootView.removeAllViews();// w w  w . j ava 2  s  . co  m
    mRootView.addView(currentView);
    mChildViewList.add(0, currentView);
    mChildWebviewLst.add(0, mChildBrowser.getChildWebview());
    currentView.requestFocus();
    mChildBrowser.setOnClildBrowserListener(new ClildBrowserListener() {
        @Override
        public void closeCurrentWindow() {
            removeChildBrowser();

        }
    });
}

From source file:beichen.douban.ui.view.LazyViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;/* w  w w  .  j  av a2s  .  com*/

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // 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.
            if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
                handled = pageLeft();
            } 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.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || 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:de.andacaydin.bidirectionalviewpagerlibrary.BiDirectionalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;/*  www.ja va  2s.  c  om*/
    boolean handled = false;
    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // 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 nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pageLeft();
            } 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 nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || 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:maximsblog.blogspot.com.formuladict.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 (mListener != null) {
            mListener.onDrawerOpened(drawerView);
        }//from  w  w  w  .j  a  v a  2 s .com

        updateChildrenImportantForAccessibility(drawerView, true);

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

        drawerView.requestFocus();
    }
}