Dynamically adding views To Horizontal LinearLayout goes out of the screen - android

I have to create Text Views and Edit Texts dynamically and insert it inside the linear layout(horizontal) which is already declared in the XML file. Number of Text Views and Edit Texts to be inserted varies dynamically. I am creating the views dynamically and adding it to the linear layout.But the problem is that if number of views is more it moves out of the screen in spite of coming in the next line.what should I do to make the views come in next line if no space is available.

There is currently no Android layout with that behaviour.
Nevertheless, it has already been tackled in other threads (such as Android - LinearLayout Horizontal with wrapping children), which provide guidance on how to implement such a layout.

You can't do it with linearlayout, try tablelayout!

Use ScrollView and add your LinearLayout into ScrollView.

Related

Android Horizontal oriented LinearLayout - how to know how many TextViews will fit?

My Idea is adding TextViews with rounded corners background to a horizontal LinearLayout, so if next one wouldn't fit - I will add TextView to another LinearLayout below.
Is there a way to do so? I know it sound like a custom view, but I would like not to bother as much - to adjust height, make click area calculations istead of simple clickListeners
Sounds like a recyclerview using a flexbox layout https://github.com/google/flexbox-layout with flexWrap turned on.
You could also use is in a static layout as well.
With flexWrap it does all the calculations to see if the "item" can fit on a line and if not starts a new line.
Many examples on the github page.
You could keep adding text views in linear layout while checking if newly added text view is outside of linear layout horizontal boundaries, if it is you could remove it from linear layout and add it in new one but I see no reason why you would want to do that.

is it possible to switch from Relative layout to Linear layout without displacing the objects?

I started my application with relative layout. I completed the entire registration form. i wanted to change the radio buttons to spinner. While replacing the radio buttons with spinner my entire form objects gets displaced. Is there a way to change relative layout to linear layout without affecting the objects?
You can add the XML Relativelayout and the result you expect. Sorry i cant comment

How to create linear layout with different orientation within textviews programatically?

How can I create linear layout with different orientation within textviews programatically?
I want to create linear layouts
The first linearlayout with horizontal orientation as parent, then inside first parent layout I want to create 7 linearlayouts and textviews and the 7 created textviews should be below the 7 linearlayots....
Expected result:
The notion of layouts "within" TextViews doesn't exist, it can't be done. We put TextViews in layouts instead.
I'd suggest one big horizontal LinearLayout, in which you add the number of vertical LinearLayout that you need. Each of these vertical layouts containing two textViews, one for the big letter and one for the small word.
You should define part of it in the XML though. The inner layout (the vertical one) could be in an XML file with it's two TextViews. Then for each letter you load the XML using the LayoutInflater and just set the two textView's values.

How to scroll only the widgets(Content) but not the background in android

I defined ScrollView and I put inside some buttons. I have two problems:
If I have a little bit buttons then the background is too small
and does not cover whole screen.
I want to just move buttons but not the the background when I
scroll.
Thank you
Put the scrollview inside a linear layout or a framelayout and add the background to the outer layouts. So when only the items in your scroll view will move.
Also try adding background directly to the scrollview and not to its child elements, that might work without needing to add a extra out layout like I mentioned above.
For your first question, what is the scrollview's layout_width and layout_height values? I'm guessing it's wrap_content. Try changing it to match_parent or fill_parent.
For your second question, blessenm's reply works for me.

Linear Layout Issue at Runtime

I am trying to build a layout dynamically which display some text and image for the most part, but has a series of buttons placed next to each other in the bottom.
I have a linear layout that carries the text, another linear layout that carries the image. And yet another linear layout that carries the buttons that get created in a for loop. I have a main layout aligned vertical that adds the text, image and buttons layout, in that order. To finally generate something like this:
Text ....
Image ...
Button1 Button2 Button3....
The problem is the number of buttons get decided at runtime, so if there are more than 4 buttons, the 5th button gets displayed really tiny. Also, when I tilt the phone, I get only the text and image showing, but no buttons coz the image covers the entire screen.
Layoutting seems to be pretty complicated to me, any help is appreciated!
Thanks
George
You do not need to wrap single views in a linear layout, so add the text and image directly to the root linear layout. You might consider using a relative layout instead of linear for the root.
Using FILL_PARENT and WRAP_CONTENT for the LayoutParams width or height can give some useful results. For example, using FILL_PARENT for the image height might scale it down to leave room for the buttons.
Be careful with LayoutParams because there are lots of them and only the one that matches the ViewGroup class should be used.
One option would be to implement an onLayout method of your own in a custom ViewGroup. You will be passed the dimensions you have to work with and be able to position all the views as you see fit.

Categories

Resources