Java Code Examples for android.text.Layout#getLineMax()

The following examples show how to use android.text.Layout#getLineMax() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReflowText.java    From android-proguards with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate the right boundary for this run (harder than it sounds). As we're a letter ahead,
 * need to grab either current letter start or the end of the previous line. Also need to
 * consider maxLines case, which inserts ellipses at the overflow point – don't include these.
 */
private int getRunRight(
        Layout unrestrictedLayout, Layout maxLinesLayout, int currentLine, int index,
        int line, boolean withinMax, boolean isMaxEllipsis, boolean isLastChar) {
    int runRight;
    if (line != currentLine || isLastChar) {
        if (isMaxEllipsis) {
            runRight = (int) maxLinesLayout.getPrimaryHorizontal(index);
        } else {
            runRight = (int) unrestrictedLayout.getLineMax(currentLine);
        }
    } else {
        if (withinMax) {
            runRight = (int) maxLinesLayout.getPrimaryHorizontal(index);
        } else {
            runRight = (int) unrestrictedLayout.getPrimaryHorizontal(index);
        }
    }
    return runRight;
}
 
Example 2
Source File: NiboPlacesAutoCompleteSearchView.java    From Nibo with MIT License 4 votes vote down vote up
private void openSearchInternal(Boolean openKeyboard) {
    this.mLogoView.setVisibility(View.GONE);
    this.mSearchEditText.setVisibility(View.VISIBLE);
    mSearchEditText.requestFocus();
    this.mSuggestionListView.setVisibility(View.VISIBLE);
    mSuggestionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            hideKeyboard();
            NiboSearchSuggestionItem result = mSearchSuggestions.get(arg2);
            if (mSearchListener != null) {
                if (mSearchListener.onSuggestion(result)) {
                    setSearchString(result.getValue(), true);
                    fromEditingToSearch(true, false);
                }
            } else {
                setSearchString(result.getValue(), true);
                fromEditingToSearch(true, false);
            }
        }

    });
    String currentSearchText = getSearchText();
    if (currentSearchText.length() > 0) {
        buildSearchSuggestions(currentSearchText);
    } else {
        buildEmptySearchSuggestions();
    }

    if (mSearchListener != null)
        mSearchListener.onSearchEditOpened();
    if (getSearchText().length() > 0) {
        showClearButton();
    }
    if (openKeyboard) {
        if (showCustomKeyboard && mCustomKeyboardView != null) { // Show custom keyboard
            mCustomKeyboardView.setVisibility(View.VISIBLE);
            mCustomKeyboardView.setEnabled(true);

            // Enable cursor, but still prevent default keyboard from showing up
            OnTouchListener otl = new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            mCustomKeyboardView.setVisibility(View.VISIBLE);
                            mCustomKeyboardView.setEnabled(true);
                            Layout layout = ((EditText) v).getLayout();
                            float x = event.getX() + mSearchEditText.getScrollX();
                            int offset = layout.getOffsetForHorizontal(0, x);
                            if (offset > 0)
                                if (x > layout.getLineMax(0))
                                    mSearchEditText.setSelection(offset);     // Touch was at the end of the text
                                else
                                    mSearchEditText.setSelection(offset - 1);
                            break;
                        case MotionEvent.ACTION_MOVE:
                            layout = ((EditText) v).getLayout();
                            x = event.getX() + mSearchEditText.getScrollX();
                            offset = layout.getOffsetForHorizontal(0, x);
                            if (offset > 0)
                                if (x > layout.getLineMax(0))
                                    mSearchEditText.setSelection(offset);     // Touch point was at the end of the text
                                else
                                    mSearchEditText.setSelection(offset - 1);
                            break;
                    }
                    return true;
                }
            };
            mSearchEditText.setOnTouchListener(otl);
        } else { // Show default keyboard
            mSearchEditText.setOnTouchListener(null);
            InputMethodManager inputMethodManager = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(
                    getApplicationWindowToken(),
                    InputMethodManager.SHOW_FORCED, 0);
        }
    }
}
 
Example 3
Source File: PersistentSearchView.java    From PersistentSearchView with Apache License 2.0 4 votes vote down vote up
private void openSearchInternal(Boolean openKeyboard) {
    this.mLogoView.setVisibility(View.GONE);
    this.mSearchEditText.setVisibility(View.VISIBLE);
    mSearchEditText.requestFocus();
    this.mSuggestionListView.setVisibility(View.VISIBLE);
    mSuggestionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            hideKeyboard();
            SearchItem result = mSearchSuggestions.get(arg2);
            if (mSearchListener != null) {
                if (mSearchListener.onSuggestion(result)) {
                    setSearchString(result.getValue(), true);
                    fromEditingToSearch(true, false);
                }
            } else {
                setSearchString(result.getValue(), true);
                fromEditingToSearch(true, false);
            }
        }

    });
    String currentSearchText = getSearchText();
    if (currentSearchText.length() > 0) {
        buildSearchSuggestions(currentSearchText);
    } else {
        buildEmptySearchSuggestions();
    }

    if (mSearchListener != null)
        mSearchListener.onSearchEditOpened();
    if (getSearchText().length() > 0) {
        showClearButton();
    }
    if (openKeyboard) {
        if(showCustomKeyboard && mCustomKeyboardView != null) { // Show custom keyboard
            mCustomKeyboardView.setVisibility(View.VISIBLE);
            mCustomKeyboardView.setEnabled(true);

            // Enable cursor, but still prevent default keyboard from showing up
            OnTouchListener otl = new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            mCustomKeyboardView.setVisibility(View.VISIBLE);
                            mCustomKeyboardView.setEnabled(true);
                            Layout layout = ((EditText) v).getLayout();
                            float x = event.getX() + mSearchEditText.getScrollX();
                            int offset = layout.getOffsetForHorizontal(0, x);
                            if (offset > 0)
                                if (x > layout.getLineMax(0))
                                    mSearchEditText.setSelection(offset);     // Touch was at the end of the text
                                else
                                    mSearchEditText.setSelection(offset - 1);
                            break;
                        case MotionEvent.ACTION_MOVE:
                            layout = ((EditText) v).getLayout();
                            x = event.getX() + mSearchEditText.getScrollX();
                            offset = layout.getOffsetForHorizontal(0, x);
                            if (offset > 0)
                                if (x > layout.getLineMax(0))
                                    mSearchEditText.setSelection(offset);     // Touch point was at the end of the text
                                else
                                    mSearchEditText.setSelection(offset - 1);
                            break;
                    }
                    return true;
                }
            };
            mSearchEditText.setOnTouchListener(otl);
        } else { // Show default keyboard
            mSearchEditText.setOnTouchListener(null);
            InputMethodManager inputMethodManager = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(
                    getApplicationWindowToken(),
                    InputMethodManager.SHOW_FORCED, 0);
        }
    }
}