Home > Blockchain >  TextView in Android XML is not showing all of my text
TextView in Android XML is not showing all of my text

Time:12-29

A have a constraint layout with some images and texviews. My textview has the top, bottom and start constraint in the imageview. But the issue is texviews do not display all the text.

I can't change the groupview and need to keeping use the constraint layout.

The design view example image posted in the question

Your TextViews extend off the edge of the screen, because they're not constrained to that edge, and their width is set to wrap_content, i.e. "as wide as the text they contain". Because your text doesn't fit on the screen, the end of the TextView ends up pushed out of view.

You need to do things the other way around - the TextView needs to be set/constrained to a suitable size, so the text contents fit to that. That means either setting a fixed layout_width, or using constraints like layout_constraintEnd_toEndOf="parent" to lock the end edge to the side of the screen (with layout_width="0dp" to allow the TextView to size to fit that constraint).

That way, the width is limited, and the text needs to flow to the next line if it's too long. You can keep layout_height as wrap_content to allow it to grow as that wrapping happens - obviously if there are too many line wraps, it'll grow too tall, and then you'll have to worry about things like the maxLines and ellipsize attributes, adding a maxHeight, or otherwise restricting the height. If both dimensions are restricted, then that puts a limit on how much text you can display!

It's also possible to keep the TextView with a wrap_content width, and place it inside a ScrollView which is constrained to a certain size on the screen - that would allow you to drag the text to see more of it (basically sliding the view from side to side so you can see different parts of it through the "window" the ScrollView creates) but that really depends on what you want.


From your layout, I'm guessing you just want a fixed label next to each image. So I'd:

  • constrain the end of the TextView to the end of the parent (you'll want some padding/margin in there somewhere too)
  • constrain the top and bottom of the TextView to the top and bottom of the ImageView (so it's vertically centred)
  • maybe rewrite your labels so they're not as long (some writing guidance here)

You also need to test how it looks with various screen sizes, at various system font sizes (if the user has set their phone to use large text, anything that uses sp dimensions will be larger and take up more room). If your labels are long, and the TextView could end up taller than the ImageView, you might have to start using Barriers so things can be constrained to the appropriate elements depending on which is largest. (Which is another good reason to keep your labels concise if you can!) There are lots of ways to define these limits about how your space is used, it's up to you how you want to do it.

CodePudding user response:

I rendered your view in android studio, your views are going out of the screen just add this line to all your text views to prevent them from going further than the parent or screen

    app:layout_constraintEnd_toEndOf="parent"
  • Related