I have a ConstraintLayout with two TextViews. I'm currently trying to align & constrain the two TextViews in order to make them read like a sentence in ConstraintLayout.
When TextView 1 gets long, I want TextView 2 to wrap to the next line, so that together - the two TextViews still appear as one piece of text.
The reason I'm doing this with separate TextViews, is that the my first and second textviews have different sources of data and are bound to separate fields in my ViewModel.
Please see the photo below:
Is this possible in ConstraintLayout or any of the other Layouts?
Each Android view fills a continuous area on a screen. Your second TextView violates this characteristic insofar as it will have two disjointed areas and that is not permitted.
I suggest that you use a single TextView and observe the two sources of data to update the view. You might be able to use something like MediatorLiveData to do this.
Related
I am trying to use just one Text composable and have two different texts displayed to the start of the parent and to the end of the parent. Is there a recommended way of doing it? I was looking at the official documentation for Text composable and I could only find that using "buildAnnotatedString", we can have multiple texts vertically stacked on top of each other with the help of withStyle and lineHeight. But I am trying to do something like this:Total $40 But "Total" should be all the way to the left and
"$40" should be all the way to the right.
I've two textviews aligned horizontally. when the second TextView becomes longer it wraps to the next line incorrectly. I want it to start the text from the beginning of the layout instead of just below the TextView.
I've tried flow layout but it wrap all the text to the bottom rather than wrapping just the additional text.
What I want :
The reason I want two TextView is because I'm working on Drag and Drop quiz, so when the user drags the answer to the textview it will change it's content "setText(answer)" to be the answer.
Thank you for helping
Drag and drop image
I don't think that this is possible. You can always use string with arguments to achieve that.
e.g.
<string name="test_string">%1$s %2$s</string>
and then
getString(R.string.test_string, "text view one text", "text view two text")
I'm not sure this is possible. It sounds like an awkward way of going about something quite simple.
My solution would be to have 1 TextView, and join the two strings together that would otherwise be in separate TextViews.
Instead of two textViews, take two string variables and work on them. in the end, just concatenate two strings and display it in the textView.
In order to give it look and feel of two different textViews, you can either use html format, or spannable text.
We might be able to help understand your problem better if you can explain the actual scenario, and why two textViews are necessary.
Right now I'm stuck how to manage to build a specific Activity in my app. I've added an image so I can explain my problem:
So first of all: all the content will be loaded from an API. "Static text" in my image means that I can define these parts in my activity.xml and don't have to do that in my Activity.java because these parts will be always the same for the screen (meaning the size of the elements, the content will be loaded from my API).
The green box should be horizontal scrollable or not depending how many boxes have to be shown here (1 to 3 possible).
The blue box will be generated in my Activity (in the end it should look like a table) and I want to define the layout of a single row in a separate xml (e.g. table_row.xml) so I could change it easily. This table can have up to 100 rows depending on how many are returned by the API.
So my problem right now is: Obviously this whole layout has to be scrollable so my first idea was to use ScrollView and a LinearLayout as child. But I read here on stackoverflow that the performance will be really poor if you use LinearLayout and add Views to it. So everyone recommended using a ListView for this part (meaning the blue box for my Activity). But that would mean only my blue box will be scrollable as you should not use a ListView in a ScrollView.
So my question is: How can I make this whole screen scrollable with a table dynamic in size without losing performance?
Put the first three layouts as ListView Header and make your blue box layout as the list view. By this you'll be able to scroll the complete View i.e. Blue Box, however the first three layouts will be static and won't scroll.
There is compound drawable option in TextView to avoid using a parent ViewGroup and an ImageView next to TextView. Which is neat and faster in performance as said by lint. I'm trying to do something similar. A TextView with 2 texts inside, one aligned to left, one aligned to write. Sounds strange but it will only allowed for single line TextView.
And to do that, I can extend TextView and set its gravity to right side. And inside onDraw let the super class draw on right side, and then draw text on left side.
Problem is, I'm not really sure about all this. My question is, will there be a big performance difference ? I dont have any slower device to test. I will be using this TextView inside an item layout of GridView, Item layout already have many views, it would be nice if I could merge some views to one. But again, will there be a performance difference, like noticeable by user ? And if there will be, the approach I will be using by extending the TextView, is there any problem or I should try some other way ?
Thank you
From what I have read I would imagine that you are using a custom adapter for your GridView?
If so, cant you just use TableRow and insert 2 TextViews inside that with each layout weight set to 1. Then you will have 2 columns in one row?
Just an idea.
i'm trying to layout a fairly basic screen. it's just a details view screen after a list item click.
initially i'm looking to acheive a side by side stacked label value type of screen.
for example, where to the left of colon is static text and to the right of colon being dynamic values based on row clicked.
first name: john
last name: doe
last login date: yesterday
additional info: blah
i've started with a relative layout with a bunch of text views in it all positioned accordingly to acheive something like what i was looking for.
The problem arises when the dynamic data being displayed grows and stretches its enclosing text view. Of course the static labels don't grow and everything gets mis aligned...
I'm wondering if there is a different way of tackling this sort of layout...
i was shying away from stacking a bunch of horizontal linear layouts inside a vertical linear layout.
is table layout the way to go? i've read that "they usually aren’t the best tool for doing so, as they are derived from LinearLayout and not the most efficient of layout controls".
Yes there is. Use a listview with a layout predefined in xml. Use a simplelistadapter and pass it the dataset that you want to populate it with.
Edit
Here is a great tutorial:
http://www.vogella.com/articles/AndroidListView/article.html
For a simple form like you want to create I'd suggest using a TableLayout. It is simple to use. As you said the alternative to a TableLayout would be horizontal LinearLayouts in a parent vertical LinearLayout. Using a TableLayout will also automatically align the right side dynamic content for you. Everything on the right side will be treated as a column so if one resizes, they all resize to match.
In your particular scenario I would believe that the TableLayout would work well (although I have also heard similar issues of efficiency/performance). As long as the entire Viewgroup of this Activity isn't complex, I don't think the performance will be too noticeable.
If you are attempting to make the RelativeLayout version work, perhaps you can try this: Have all of your static labels are aligned to the left using android:layout_alignParentLeft and have each aligned to the top of the dynamic TextView they are corresponding to using android:layout_alignTop. This should keep the static TextViews aligned to the left while aligned to the dynamic view relative to it.
Now that those views are aligned, we can horizontally align the dynamic views to the longest static TextView using android:layout_toRightOf. From there, all the remaining dynamic views can also android:layout_alignLeft to this anchor dynamic TextView, or also align to the longest static TextView in the same manner that the anchor was. This solves the horizontal alignment of all the dynamic TextViews.
Finally, we can set that each dynamic TextView falls under the next, since the dynamic TextViews are our determining our vertical location within this RelativeLayout. Each view can use android:layout_alignBelow to chain the fields to align vertically.
I believe this should work for you and I can edit this post later in the day if you would like a sample of code.