Which layout is best for showing horizontal items in android - android

I need to show one image and 3 textview and one radio button horizontally in android which layout using will be best that is linear layout or relative layout and why??

In my opinion, RelativeLayout will be the better choice. RadioButton will probably come at the end right? RelativeLayout can do that easily. I'm guessing ImageView comes at the left end. And 3 TextView in the middle. RelativeLayout is a good choice to implement this.

If you want to show all view in just horizontally and you also don't want to do any more customization then linear layout is good and If you want to more customization then Relative Layout(Customizations means like you want to make one view height based on another view height width or something).

Use the Constraint layout for horizontal aligning of the Views. You can set constraints and give them horizontal bias. Read about constraint layout Link
.

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.

What is the best practise for padding or margin in Android?

I am relatively new to Android UI. I always get confuse in providing margin to different view like should i provide bottom margin or should i use top margin(to view below it). Also should i use RelativeLayout or LinearLayout if both can solve my problem.
Thanks
It Depends on your need
Linear Vs Relative
If you just want to stack your TextView and Button horizontally or vertically you should go with LinearLayout.
If you want to make a layout that is more complex for example you can have an ImageView covering all of the screen and Button over the ImageView or if you want your layout elements to be stack on corners or at bottom, RelativeLayout is your Guy.
Top margin vs Bottom Margin
It doesn't make much a difference its a personal preference, I Use margin-bottom on first element rather than margin-top for second element.
One noticeable difference is when you are working with Show layout bounds during development. You can see here those pink coloration indicate that it is using margin on its view while padding has no coloration. Recently I prefer to use padding if applicable with my requirements as it seems more cleaner to inspect UI when Show layout bounds is enabled from Developer option.
The image is not mine and was just use as a quick sample.
If both Linear and RelativeLayout will solve your layout, then you should use Linear as it is faster to render.
With regards to top vs. bottom Margin. That's entirely your preference and how you want to think about the elements. Does Item A always sit 40dp above the next item or does Item B always sit 40dp below the previous item?

Center view to another in RelativeLayout

I have two views that I want to center vertically in a RelativeLayout.
Is there a way to do this without using gravity/layout_gravity ?
My problem behind this question :
I need to do a layout with several squares and under each one a TextView. The main problem is that the TextViews must overlap. Only one TextView will be visible at a time. Each TextView has a different lengh.
I started with a RelativeLayout but encountred the previous problem. And I can't group the views 2 per 2 because I need to set a layout_toRightOf of the previous square.
Current layout :
For the moment, I have set a magic number in layout_marginLeft for each square (to the border of the parent view) but it isn't clean at all.
Thanks
Use android:layout_centerInParent="true" to center something into a RelativeLayout
You can use android:layout_below="#id/your_first_view" to put your second View below the first !
If you want to do more complex stuff you may separate your different Views and store the into new LinearLayout that you set to horizontal or vertical depending on your needs.
Another trick can be to create empty Views with small height or width and that can help you to position thing around them !
This combined to the toRightOf toLeftOf stuff will do what you want
Finaly, I kept the the layout_marginLeft but I put the values in my res/values/dimens.xml, it is cleaner and I can have a dimens.xml per screen dimension.
A trick is to set the width of the textViews deliberately big so it won't depend on the strings lenght.

How to layout TextViews side-by-side with spacing in android?

I have a very simple layout with two side by side textviews. Both have the same parent layout that fills the screen horizontally.
I need them to have a visible space between them so that they are visually seperated when both have text. I also need the left textview to take up about 2/3 the screen width and let the other have the rest.
This is fairly easy to do with LinearLayout and a few margin settings, but if either one of the views has no text, I need the other one to fill the entire width.
I'm not quite sure how to have the layout do that without setting the empty view's visibility to GONE in code. Is there any good, efficient way to do all of these things at once? Feel free to use any layout you wish to make it work.
have you tried this using a relative layout? there is a property for layout_alignWithParentIfMissing that might give you what you need...

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