Questions regarding margins in constraint layout - android

emphasized text
If I keep 16dp as margin all around 4 sides,why 16 dp length or width is different from left and top?
In the second image,as you can see I have set height as wrap_content and margin is 0 from top too.But still it is applying space from top.So,why it is doing that?What if I add some margin from top?

Because you have to set the android:layout_width and android:layout_height to match_constraint or the special 0dp value inside the layout.xml file. Otherwise looks like you're using wrap_content so it's respecting the constraints by adapting the view to its size.

Related

match_parent with max_width

I have an ImageView and I want to set something like this:
min_with=match_parent and max_width=value
does someone know how to do that?
I am guessing your parent is not the root parent and your parent width is not match_parent either.
You can set your parent width as wrap_content and set child width as wrap_content but set child min width to some dp so your child won’t have less width than min width but it will hug the parent on all sides.
You can also take advantage of linear view and its weightSum, weight to childrens
We can go in details if you share more data and small drawing of what you want to do.

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.

MATCH_PARENT inside WRAP_CONTENT is giving me problems

I have a bar in the bottom of my layout which consists in a horizontal LinearLayout (like a row) that contains vertical LinearLayouts (Like a cell of a row).
The Horizontal linearLayout (row) haves WRAP_CONTENT height because i want the bar fit its elements height, and the vertical linear layouts (cells) have MATCH_PARENT because they can have for example a item centered vertically inside,
so they are MATCH_PARENT and they must have the Height of the higher height of the cells on the row.
The problem is that the bar is occupying all the height of my screen. I think that there are problems with MATCH_PARENT inside a WRAP_CONTENT.
How can i solve this problem?
To set MATCH_PARENT height to LinearLayouts(cells) you must have to fix some height of row(LinearLayout) otherwise it will covers entire screen

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.

Nested LinearLayouts and center aligning them

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.

Categories

Resources