Justin
Justin

Reputation: 18176

How to display a Button inline within a multiline TextView

I have to embed a button "inline" within a multiline textview, like so:

enter image description here

I'm not sure how to do this in Android, as there are no layout elements or attributes (that I'm aware of) that allow aligning a Button where text ends in a multiline TextView. And I really don't want to use a WebView for this and do it with html / css, as this screen is already heavy enough. Any ideas how to accomplish it?

Upvotes: 1

Views: 567

Answers (3)

Anup Ammanavar
Anup Ammanavar

Reputation: 432

You can create a textview(tvDescription) with the maxLines=5 and ellipsize=end

I'm assuming the maximum lines you wan to show is 5

android:ellipsize="end"
android:maxLines="5"

Add a view(btnIndicator currently button. You can change as per your requirements) which always stays on the bottom right of the textView. This view contains the text (View More and View Less). Suggest you to use constraint layout which will make your task easy.

Assuming you have got the reference to Textview as tvDescription.

And the view at the bottom right as btnIndicator. Add onClickListener to this view.

btnIndicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int maxLines = tvDescription.getLineCount();
                if (maxLines == 5) {
                    maxLines = Integer.MAX_VALUE;
                    btnIndicator.setText("View Less")
                }
                else {
                    maxLines = 5;
                    btnIndicator.setText("View More")
                }
                /*
                * Here you can append the text VIEW MORE(when max lines is 5)
                * or ignore if it is already expanded
                * */
                tvDescription.setMaxLines(maxLines);
            }
        });

Upvotes: 0

Kofi Nyarko
Kofi Nyarko

Reputation: 253

ClickableSpan is the answer to your problem!

Upvotes: 1

Ian D
Ian D

Reputation: 71

Android strings accept unicode and html characters, which includes arrows

Upvotes: 0

Related Questions