[JAVA] [Android] Creating more buttons

More button

parts

contentsTextView: Text view for which you want to make the display area variable readMoreTextView: Text view used as a "more" button

specification

If the content has more than 8 lines of text → Display "more"

Implementation details

Use getEllipsisCount () on Android 5 and above Can't it be used below that? Since it is a little bit, it is judged whether to display "more" depending on whether the number of displayed text lines is larger or smaller than the maximum number of lines in the text view.

code

MainActivity.java



public void setView (){

            contentsTextView.setMaxLines(8);
            contentsTextView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    int lineCount = contentsTextView.getLineCount();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        //It seems that it will not work properly unless it is android 5 or higher

                        int hiddenLineCount = contentsTextView.getLayout().getEllipsisCount(lineCount - 1);
                        if (hiddenLineCount > 0) {
                            readMoreTextView.setVisibility(View.VISIBLE);
                        } else {
                            readMoreTextView.setVisibility(View.GONE);
                        }
                    } else{
                        //android less than 5

                        if(lineCount > 8){
                            readMoreTextView.setVisibility(View.VISIBLE);
                        }else{
                            readMoreTextView.setVisibility(View.GONE);
                        }
                    }

                    return true;
                }
            });
}

@Override
    public void onClick(View v) {

        if (v.getId() == readMoreTextView.getId()){

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    contentsTextView.setMaxLines(100);
                    contentsTextView.setEllipsize(null);

                } else {

                    contentsTextView.setMaxLines(1000);
                    contentsTextView.setEllipsize(null);
                }
            }
    }


Recommended Posts

[Android] Creating more buttons
Links for creating Android apps (for beginners)
Notes on creating android plugins for Unity