Example usage for android.view View isFocused

List of usage examples for android.view View isFocused

Introduction

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

Prototype

@ViewDebug.ExportedProperty(category = "focus")
public boolean isFocused() 

Source Link

Document

Returns true if this view has focus

Usage

From source file:Main.java

public static void showSoftKeyboard(View view) {
    if (view == null)
        return;/*w  w w . j ava2s  .co  m*/
    view.setFocusable(true);
    view.setFocusableInTouchMode(true);
    if (!view.isFocused())
        view.requestFocus();

    InputMethodManager inputMethodManager = (InputMethodManager) view.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, 0);
}

From source file:com.android.printspooler.widget.PrintContentView.java

private void ensureImeClosedAndInputFocusCleared() {
    View focused = findFocus();

    if (focused != null && focused.isFocused()) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive(focused)) {
            imm.hideSoftInputFromWindow(getWindowToken(), 0);
        }/*  ww w .ja va2  s. c om*/
        focused.clearFocus();
    }
}

From source file:android.support.car.app.CarFragmentActivity.java

private static String viewToString(View view) {
    StringBuilder out = new StringBuilder(128);
    out.append(view.getClass().getName());
    out.append('{');
    out.append(Integer.toHexString(System.identityHashCode(view)));
    out.append(' ');
    switch (view.getVisibility()) {
    case View.VISIBLE:
        out.append('V');
        break;/*from   w  w  w.  j a v  a  2s.c  om*/
    case View.INVISIBLE:
        out.append('I');
        break;
    case View.GONE:
        out.append('G');
        break;
    default:
        out.append('.');
        break;
    }
    out.append(view.isFocusable() ? 'F' : '.');
    out.append(view.isEnabled() ? 'E' : '.');
    out.append(view.willNotDraw() ? '.' : 'D');
    out.append(view.isHorizontalScrollBarEnabled() ? 'H' : '.');
    out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
    out.append(view.isClickable() ? 'C' : '.');
    out.append(view.isLongClickable() ? 'L' : '.');
    out.append(' ');
    out.append(view.isFocused() ? 'F' : '.');
    out.append(view.isSelected() ? 'S' : '.');
    out.append(view.isPressed() ? 'P' : '.');
    out.append(' ');
    out.append(view.getLeft());
    out.append(',');
    out.append(view.getTop());
    out.append('-');
    out.append(view.getRight());
    out.append(',');
    out.append(view.getBottom());
    final int id = view.getId();
    if (id != View.NO_ID) {
        out.append(" #");
        out.append(Integer.toHexString(id));
        final Resources r = view.getResources();
        if (id != 0 && r != null) {
            try {
                String pkgname;
                switch (id & 0xff000000) {
                case 0x7f000000:
                    pkgname = "app";
                    break;
                case 0x01000000:
                    pkgname = "android";
                    break;
                default:
                    pkgname = r.getResourcePackageName(id);
                    break;
                }
                String typename = r.getResourceTypeName(id);
                String entryname = r.getResourceEntryName(id);
                out.append(" ");
                out.append(pkgname);
                out.append(":");
                out.append(typename);
                out.append("/");
                out.append(entryname);
            } catch (Resources.NotFoundException e) {
            }
        }
    }
    out.append("}");
    return out.toString();
}

From source file:org.openmrs.mobile.activities.login.LoginFragment.java

private void initListeners() {
    mLoginSyncButton.setOnClickListener(new View.OnClickListener() {
        @Override//  w  w w  .  j  av a 2  s. c o  m
        public void onClick(View view) {
            final SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(OpenMRS.getInstance());
            boolean syncState = prefs.getBoolean("sync", true);
            SharedPreferences.Editor editor = PreferenceManager
                    .getDefaultSharedPreferences(OpenMRS.getInstance()).edit();
            editor.putBoolean("sync", !syncState);
            editor.commit();
            setSyncButtonState(!syncState);
        }
    });

    loginValidatorWatcher = new LoginValidatorWatcher(mUrl, mUsername, mPassword, mDropdownLocation,
            mLoginButton);

    mUrl.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (StringUtils.notEmpty(mUrl.getText().toString()) && !view.isFocused()
                    && loginValidatorWatcher.isUrlChanged()
                    || (loginValidatorWatcher.isUrlChanged() && !view.isFocused()
                            && loginValidatorWatcher.isLocationErrorOccurred())
                    || (!loginValidatorWatcher.isUrlChanged() && !view.isFocused())) {
                ((LoginFragment) getActivity().getSupportFragmentManager()
                        .findFragmentById(R.id.loginContentFrame)).setUrl(mUrl.getText().toString());
                loginValidatorWatcher.setUrlChanged(false);
            }
        }
    });

    mLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPresenter.login(mUsername.getText().toString(), mPassword.getText().toString(),
                    mUrl.getText().toString(), initialUrl);
        }
    });

    mForgotPass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            forgotPassword();
        }
    });

    mShowPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                mPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                mPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        }
    });
}

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

public void testFocusFinder() throws Throwable {
    mInstrumentation = getInstrumentation();
    Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
    intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear_with_button);
    intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 3);
    intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
    initActivity(intent);//from  ww  w  .  j a va2  s .  c om
    mOrientation = BaseGridView.VERTICAL;
    mNumRows = 1;

    // test focus from button to vertical grid view
    final View button = mActivity.findViewById(R.id.button);
    assertTrue(button.isFocused());
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    assertFalse(mGridView.isFocused());
    assertTrue(mGridView.hasFocus());

    // FocusFinder should find last focused(2nd) item on DPAD_DOWN
    final View secondChild = mGridView.getChildAt(1);
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            secondChild.requestFocus();
            button.requestFocus();
        }
    });
    assertTrue(button.isFocused());
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    assertTrue(secondChild.isFocused());

    // Bug 26918143 Even VerticalGridView is not focusable, FocusFinder should find last focused
    // (2nd) item on DPAD_DOWN.
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            button.requestFocus();
        }
    });
    mGridView.setFocusable(false);
    mGridView.setFocusableInTouchMode(false);
    assertTrue(button.isFocused());
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    assertTrue(secondChild.isFocused());
}

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

public void testBug27258366() throws Throwable {
    mInstrumentation = getInstrumentation();
    Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
    intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear_with_button_onleft);
    intent.putExtra(GridActivity.EXTRA_CHILD_LAYOUT_ID, R.layout.horizontal_item);
    intent.putExtra(GridActivity.EXTRA_NUM_ITEMS, 10);
    intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
    intent.putExtra(GridActivity.EXTRA_UPDATE_SIZE, false);
    initActivity(intent);//  www  .  j a  v a 2s  . c om
    mOrientation = BaseGridView.VERTICAL;
    mNumRows = 1;

    // move item1 500 pixels right, when focus is on item1, default focus finder will pick
    // item0 and item2 for the best match of focusSearch(FOCUS_LEFT).  The grid widget
    // must override default addFocusables(), not to add item0 or item2.
    mActivity.mAdapterListener = new GridActivity.AdapterListener() {
        public void onBind(RecyclerView.ViewHolder vh, int position) {
            if (position == 1) {
                vh.itemView.setPaddingRelative(500, 0, 0, 0);
            } else {
                vh.itemView.setPaddingRelative(0, 0, 0, 0);
            }
        }
    };
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            mGridView.getAdapter().notifyDataSetChanged();
        }
    });
    Thread.sleep(100);

    final ViewGroup secondChild = (ViewGroup) mGridView.getChildAt(1);
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            secondChild.requestFocus();
        }
    });
    sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
    Thread.sleep(100);
    final View button = mActivity.findViewById(R.id.button);
    assertTrue(button.isFocused());
}

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   w w w .jav a 2  s  .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.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

/**
 * Handle scrolling in response to an up or down arrow click.
 *
 * @param direction The direction corresponding to the arrow key that was
 *                  pressed//from ww  w . ja v a  2 s . co  m
 * @return True if we consumed the event, false otherwise
 */
public boolean arrowScroll(int direction) {

    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);

    final int maxJump = getMaxScrollAmount();

    if (nextFocused != null && isWithinDeltaOfScreen(nextFocused, maxJump, getHeight())) {
        nextFocused.getDrawingRect(mTempRect);
        offsetDescendantRectToMyCoords(nextFocused, mTempRect);
        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
        doScrollY(scrollDelta);
        nextFocused.requestFocus(direction);
    } else {
        // no new focus
        int scrollDelta = maxJump;

        if (direction == View.FOCUS_UP && getScrollY() < scrollDelta) {
            scrollDelta = getScrollY();
        } else if (direction == View.FOCUS_DOWN) {
            if (getChildCount() > 0) {
                int daBottom = getChildAt(0).getBottom();
                int screenBottom = getScrollY() + getHeight() - getPaddingBottom();
                if (daBottom - screenBottom < maxJump) {
                    scrollDelta = daBottom - screenBottom;
                }
            }
        }
        if (scrollDelta == 0) {
            return false;
        }
        doScrollY(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
    }

    if (currentFocused != null && currentFocused.isFocused() && isOffScreen(currentFocused)) {
        // previously focused item still has focus and is off screen, give
        // it up (take it back to ourselves)
        // (also, need to temporarily force FOCUS_BEFORE_DESCENDANTS so we are
        // sure to
        // get it)
        final int descendantFocusability = getDescendantFocusability(); // save
        setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        requestFocus();
        setDescendantFocusability(descendantFocusability); // restore
    }
    return true;
}

From source file:com.hippo.widget.BothScrollView.java

/**
 * Handle scrolling in response to a left or right arrow click.
 *
 * @param direction The direction corresponding to the arrow key that was
 *                  pressed/*  w  w w  . ja va  2 s. co m*/
 * @return True if we consumed the event, false otherwise
 */
public boolean arrowScrollHorizontally(int direction) {

    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);

    final int maxJump = getHorizontalMaxScrollAmount();

    if (nextFocused != null && isWithinDeltaOfScreen(nextFocused, maxJump, getWidth(), getHeight())) {
        nextFocused.getDrawingRect(mTempRect);
        offsetDescendantRectToMyCoords(nextFocused, mTempRect);
        int scrollDelta = computeScrollXDeltaToGetChildRectOnScreen(mTempRect);
        doScrollX(scrollDelta);
        nextFocused.requestFocus(direction);
    } else {
        // no new focus
        int scrollDelta = maxJump;

        if (direction == View.FOCUS_LEFT && getScrollX() < scrollDelta) {
            scrollDelta = getScrollX();
        } else if (direction == View.FOCUS_RIGHT && getChildCount() > 0) {

            int daRight = getChildAt(0).getRight();

            int screenRight = getScrollX() + getWidth() - getPaddingRight();

            if (daRight - screenRight < maxJump) {
                scrollDelta = daRight - screenRight;
            }
        }
        if (scrollDelta == 0) {
            return false;
        }
        doScrollX(direction == View.FOCUS_RIGHT ? scrollDelta : -scrollDelta);
    }

    if (currentFocused != null && currentFocused.isFocused() && isOffScreen(currentFocused)) {
        // previously focused item still has focus and is off screen, give
        // it up (take it back to ourselves)
        // (also, need to temporarily force FOCUS_BEFORE_DESCENDANTS so we are
        // sure to
        // get it)
        final int descendantFocusability = getDescendantFocusability(); // save
        setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        requestFocus();
        setDescendantFocusability(descendantFocusability); // restore
    }
    return true;
}

From source file:com.peerless2012.twowaynestedscrollview.TwoWayNestedScrollView.java

/**
 * Handle scrolling in response to an up or down arrow click.
 *
 * @param direction//  ww  w  . ja va  2 s .  com
 *            The direction corresponding to the arrow key that was pressed
 * @return True if we consumed the event, false otherwise
 */
public boolean arrowScroll(int direction) {

    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);

    final int maxJump = getMaxScrollYAmount();

    if (nextFocused != null && isWithinDeltaOfScreen(nextFocused, maxJump, getHeight())) {
        nextFocused.getDrawingRect(mTempRect);
        offsetDescendantRectToMyCoords(nextFocused, mTempRect);
        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
        doScrollY(scrollDelta);
        nextFocused.requestFocus(direction);
    } else {
        // no new focus
        int scrollDelta = maxJump;

        if (direction == View.FOCUS_UP && getScrollY() < scrollDelta) {
            scrollDelta = getScrollY();
        } else if (direction == View.FOCUS_DOWN) {
            if (getChildCount() > 0) {
                int daBottom = getChildAt(0).getBottom();
                int screenBottom = getScrollY() + getHeight() - getPaddingBottom();
                if (daBottom - screenBottom < maxJump) {
                    scrollDelta = daBottom - screenBottom;
                }
            }
        }
        if (scrollDelta == 0) {
            return false;
        }
        doScrollY(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
    }

    if (currentFocused != null && currentFocused.isFocused() && isOffScreen(currentFocused)) {
        // previously focused item still has focus and is off screen, give
        // it up (take it back to ourselves)
        // (also, need to temporarily force FOCUS_BEFORE_DESCENDANTS so we
        // are
        // sure to
        // get it)
        final int descendantFocusability = getDescendantFocusability(); // save
        setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        requestFocus();
        setDescendantFocusability(descendantFocusability); // restore
    }
    return true;
}