Reflowable mix of TextViews and EditTexts? - android

I am trying to make a sentence-filling like interface, where I can have TextViews and EditTexts mixed arbitrarily in a paragraph.
I have had a look at this question, but the problem was that when I have [ TextView1, EditText, TextView2 ] and the content of TextView1 consumes 1 full line and 1 fifth of the second line, then TextView1 will have to span the entire width because it is a rectangle. This pushes EditText to the next line, which is ugly.
So is it possible to create such kind of ViewGroup/Layout from existing stuff in Android, or I'd have to make a custom View from scratch?
In case I'm too bad at explaining, this screenshot describes my problem:
Thank you very much.

I think there's some work here :)
You should make two text views - one for line 1, one for line 2. Estimate the text, that fits on the first line, put it in the first text view. Put the rest in the second line, and it'll fit just nice.... I think you know this, and I doubt there is an easier way to do this :)
It's a nice task anyway.
Greets,
Danail

Related

textview doesn't wraps correctly?

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.

Chat layout like whatsapp

Im trying to build a chat layout like whatsapp. I don't understand how they were able to place 'date' at bottom in rightmost corner of layout.
I saw their code, it uses horizontal LinearLayout in which there is
message and date side by side, but then how does the 'I love' part
of message print above date ?
Another problem is that if I use relativelayout to push date to extreme right using alignparentright = true, it expands the entire layout to match parent, which is obvious but undesirable
Please advice me on this
As sketchy as it seems, I can only see that end result being accomplished with four text views.
Name at the top, two lines for the message, and one for the date.
One message line extends all the way across and above the date, the other is below it and next to the date.
You would need to test the textview length to see how much text you can apply to it before it starts to cut off, and apply the remainder to the second message textview.
Gross.
You can try using a FrameLayout in which there is a LinearLayout at the bottom which contains the name of the sender and the message. Then, add a TextView whose layout_gravity is set to bottom|right
You can use the layout and code below to achieve the desired effect.
Source code gist
What I have used is get the width of the text + the time layout and check if this exceeds the container layout width, and adjust the height of the container accordingly. We have to extend from FrameLayout since this is the one which allows overlapping of two child views.
This is tested to be working on English locale. Suggestions and improvements are always welcome :)
Hope I've helped someone looking for the same solution.
I would recommend using the android:layout_gravity="bottom|right" tag on the date TextView. This will attempt to lay out the View as if the container was "pushing" it to the bottom and right corner.
This may or may not work for all Views. But as #codeMagic said, if you saw their code, then it should be apparent, right?

Left and Right aligned Texts in 1 TextView

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.

Is possible to set TextView to have different widths per line?

This is what I get so far, using RelativeLayout and android:layout_toLeftOf and android:layout_toRightOf
[BUTTON1][TEXTVIEW: HERE GOES A TEXT........][BUTTON2]
[...THAT MUST CONTINUE IN THIS LINE]
This is what I need:
[BUTTON1][TEXTVIEW: HERE GOES A TEXT........][BUTTON2]
[..........THAT MUST CONTINUE IN THIS LINE...........]
I know I can do it with multiples textviews, but I don't know how to split the text to fit exactly first and second lines.
So, is possible to set textview to stay "right of" button1 and "left of" button2, but only in "line1"?
Not with TextView that platform provides. You may however write own widget to do layout that way. Peek the source of this widget as starting point http://code.google.com/p/android-flowtextview/
I'm afraid not- in android a View is always rectangular. You can't have a view becoming skinny at the top.

wrap text view around img view

Good evening,
I am trying to wrap my text around an image view like the question is asking here:
Textview wrap around View
in landscape.
but I wish to not do it in HTML / CSS. plus, that question is 2 years old. I am hoping someone has figured out a work around with this.
Also, I wish to keep my portrait view the way it is with the img view under the text view i.e. no wrapping.
Can anyone help me?
A work around I can think off the top of my head and from previous issues with TextView formatting, the best way is to have, say, a RelativeLayout, and just place 3 separate Views inside of it. It will represent the TextView that is right next to the image, the ImageView itself next to the text, and then the last TextView under the first text, but with the layout_width="match_parent". There is no other way to do this with the TextView or ImageView otherwise.
EDIT:
The only other workaround I believe you can do if you are so intent on using 1 TextView, I would suggest using SpannableStringBuilder with SpannableString. I didn't originally give you that as an option for a specific reason. If you go back to that link you posted on your question, one of the answers say they found an answer. I believe that person linked to a good resource, but they are wrong for what your needs are. When using SpannableStringBuilder, you are essentially creating a string that can have potentially many different version of a single string into one, like 3 different colors on a single string, or strings with 3 different sizes, all in one TextView. Like what you need.
Now the link talks about putting an image and having the text wrap around it nicely, but its not that simple, and that example isn't what you need. What you could do to provide it all in one TextView is to add line breaks after the first few lines where you know the image will be to the right, and then not do so after the image. But the thing is it still needs to go in a RelativeLayout. But instead of two TextViews you only need one. Just place the TextView at the top left of whatever the parent is and the ImageView to the top right of whatever the parent is. This simulates a floating element. But I think doing it that way is more trouble than its worth. It really is because you have to do calculations to figure out where to put the line breaks, etc. You are better off using two TextViews in which to make it look like you need. So in my opinion, DO NOT USE SpannableStringBuilder, but if you do not mind trying to figure out all that spacing, that does give you what you need with a single TextView.

Categories

Resources