Nested LinearLayouts and center aligning them - android

I've been trying to achieve this using nested LinearLayouts. Tried setting both width and height to 0dp . But it's not achieving 80% width and height. But it's working when i'm switching it to just single dimension ( ie, just width or height ). Couldn't get any info on it though i googled a lot .
So, now the question : does the Layout Weight work with both dimensions together?.
Note: I cannot use center gravity it , due to the lack of proper width in the children. They need to get it from linearlayout2 ( fill_parent ) .

1、The LinearLayout weight do not work both with width and height, It depends on the orientation of it. the default is Horizontal.

Related

Determine responsive heights of children in a vertical LinearLayout inside of a ScrollView

I want to give responsive height to children in a vertical Linearlayout for example(some view height must be 25% of the screen height) I know this is simple if I use weight but the problem is that I have the LinearLayout inside a ScrollView this means I dont know the exact height of the LinearLayout so weight doesn't work here.can anyone give me a way to do so.
Use:
scrollView.setFillViewport(true)
This way, the linear layout inside occupies the full height.

Force LinearLayout child to use all available space but at least enough to fit its content

I'm struggling with getting LinearLayout to behave like I want it. Unfortunately, both dimension modes MATCH_PARENT and WRAP_CONTENT don't seem to fit for my purposes.
Here's why: I want the child that is added to the LinearLayout to be completely visible. Nothing should be cut off. So normally, I should use WRAP_CONTENT to achieve this behaviour.
But, if there's more space in the LinearLayout than the child really needs, I also want it to fill that space. This is of course what MATCH_PARENT is for.
However, I can't use MATCH_PARENT because in case there is less space in the LinearLayout than my child needs, using MATCH_PARENT will cut off the child which I don't want.
So this leaves me somewhat puzzled as to how I can achieve what I want: How can I allow a child to fill additional space in the LinearLayout (if available) while at the same time forcing the LinearLayout to be at least as big as the child needs in order to be completely visible?
Put your child view inside a ScrollView with width MATCH_PARENT, height MATCH_PARENTand set both the child's dimensions MATCH_PARENT
Instead of using MATCH_PARENT for your child, use WRAP_CONTENT and set your child's weight to 1,So it will take all the empty space in your LinearLayout. Suppose your LinearLayout's height is 128dp, and your child 's height is 56dp, your child will be 128dp . If you set WRAP_CONTENT on your LinearLayout, it will be 56dp, and it child will still take all the place it needs.
If you want your LinearLayout to have a minimum width or height, and not match_parent, you can do setMinimumWidth() or setMinimumHeight() in your xml layout or in your code programmatycally depending your childs default height and width.

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.

Android - How can I determine an appropriate height for my ListView items?

I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.

TableLayout width anomaly

I have a nested TableLayout (TableLayout->TableRow->TableLayout->TableRow). The inner TableLayouts have their width set to MATCH_PARENT. It is not expanding to match its parent. See attached screenshots.
EDIT:
I can tweak the width using android:weight and setting the android:layout_width="0dp". I still want to understand this behaviour.

Categories

Resources