What exaclty is the meaning of MATCH_CONSTRAINT? - android

I am new to Android and recently I came up with a term 'match constraint' while using ConstraintLayout.
As per doc it says 'Dimension will be controlled by constraints', I don't understand what exactly mean by this ?
As far as I could understand , it's somehow can be used as replacement of match-parent but not sure how ?

First of all, lets look at what the word Constrain means. According to Google, Constrain means to
Compel or force to follow a particular course of action
Severely restrict the scope, activity or extent of
Bring about by compulsion
When using ConstraintLayout we align/position our items by applying Constraints to that particular item. What these Constraints do is that they limit (or allow) the position of that item in the screen. Lets say I have a button which I constrain to be between the left and right edge of the screen. By doing so, the button can move anywhere in the screen as long as it is within the left and right edges of the screen. Similar is the case if I constrain the button to be between the top and bottom of the screen.
Now what does MATCH_CONSTRAINT mean? It means, that the view will take up as much space as the Constraints allow it to take. So, if I constrain a view to be between the left and right edge of the screen, then the view will expand its width to be equal to the width of the screen (if no margins are set).

It means it will take the available space in the main constraint layout. For more details, you can read it from here

Related

Can the space of Safe Area be filled with anything?

I would like to have a bottom aligned button which has text positioned above safe area in a way that allows me to fill that space with the color of button and, ideally, is clickable too.
I was thinking about adding a Container with Gesture Detector there but can't figure out how to do it in a way that won't ruin layout on phones without safe area. That's how huge it looks when wrapped in SafeArea. Ideally there would be little to no margin above text in this button, something like this.
You can use flutter_screenutil instead of SafeArea, knowing the bottom and top safearea height by these 2 functions:
ScreenUtil.bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen
ScreenUtil.statusBarHeight //Status bar height , Notch will be higher Unit px
you can then control the height of safearea yourself

How does fill_parent in Android actually work under the hood?

I see great value in fill_parent, and I'd like to make my own implementation of it so I can do more than just fill the remaining space (ex: fill 80% of the remaining space, etc.)
How does fill_parent actually work under the hood? As in how does it compute the space remaining, how does it work at runtime, etc.
I've done similar things to fill_parent in the past where I calculate the space an element should take up based on the current screen size, how much of the screen the element should take up, etc. but I want to know specifically how Android does it with fill_parent.
Try creating a custom View or ViewGroup and you will find out.
There's 3 stages on bringing a View to your screen:
measure
layout
draw
In measure the parent tells the child how much space is available. It may do that in respect to the childs layout parameters. So if the child says match_parent (fill_parent is deprecated) the parent will pass in either its own size, or the remaining space (most of the time...)
The child then takes the available size, calls setMeasuredDimenstion(allTheSpaceIGot) and that's measuring for you.
Next up during layout, the parent checks the childrens measured sizes. It then sets the childrens bounds (top, left, bottom, right) accordingly.
Finally in onDraw every child draws itself within its bounds.
To sum this up:
Child gives parent information about its wishes.
Parent offers child some available space.
Child says "I'll take it".
Parent gives child its final restraints
Child draws itself within the constraints
If you want to assign say 60% to a view you should have a look at creating a custom ViewGroup (since that is who actually decides on the childs dimensions)
I also wrote a blog post about custom views that goes into more detail, followed by how to create a custom layout.
The entire source code for Android is open source, freely available within a few clicks on Google, so you can read it and study it all you want.
But just a fair warning, it's definitely no small task you're trying to accomplish, as there are an enormous amount of cases you have to account for.
If you want a layout to take X percent of available height/width, take a look at PercentageRelativeLayout
Just FYI: 'fill_parent' is deprecated, use 'match_parent' instead. They literally do the exact same thing, it's simply a different word.

Partially hide Layout programmatically?

What should I do to partially hide some layout from code? Should I play with setMargins?
Basically, I want to detect size of the screen and then I want that element X becomes visible only 10% of its width? I know how to do it all except how to make it partially hidden or offscreen?
Look at these images to see what I want to achieve. Image 1 - UI element is in the center. Image 2 - I set in code it's moved to top offscreen thus becoming only partially visible.
I found out that here as well the best solution is to use Animation (TranslateAnimation) by setting this on the first screen load and setting its speed to minimum value of 1 millisecond. The transition in reality is unnoticeable to a human eye.
Also do not use margins as they simply "push" element from one side without making it goes offset. If you have any child in the layout you want to offset, they all will be squeezed as you increase margin values.
NOTE: if you ever want to use animation on that screen on the element you offset before, take into account the negative value of offset. Otherwise your animation will not look nice. To avoid bumping effect, take special care of fromXDelta value.

Dialog content not centered horizontally on Droid X

I'm creating a Dialog, setting a content view with two Buttons, and displaying it. Oddly enough, even though I properly centered it vertically and horizontally, on the Droid X extra blank space appears at the top and on the right side.
I discovered the top is reserved for the Dialog title; even when that title is blank, the space is kept empty. The workaround there is easy enough--I set a title.
The right side, however, baffles me. When I test the same app on other devices it works beautifully; the Droid X, however, keeps the right side of the screen empty. When the root layout of my Dialog's layout has a width of match_parent or fill_parent, it does not extend to that empty area. However, if I manually set a fixed large width, it does extend as far as it needs to--no more empty space on the right side. That's hardly an ideal solution though.
Does anyone know how to get around this Droid X layout quirk so Dialogs do not have that empty space on the right side?
After some experimenting, I found that if I change the margin and/or padding sizes, suddenly the Dialog becomes centered! For example: margins of 4dp creates an offset. But making the top, bottom, left, and right margins all larger and different makes the problem go away.
This sort of behavior generally means one of two things: There's a bug in how the layout is drawn, or I have a gross misunderstanding of how these things work. Both seem equally likely.
So my answer is: play with the margin (and padding) numbers. It's possible that you can find something that both works and is aesthetically pleasing.
Sorry, I know this answer sucks. So it goes.

Android ScrollView: Automatically scroll to a few pixels (dips) above a certain view

I am aware that I should decide how many pixels based on density.. not hard coded.
I have section headers and items displayed in a scrollview and I want to scroll to a certain section header (the one for the current date) while making it clear that it is not the top of the screen (show about half of the previous item).
How can I do that? I do not want this automatic scroll to be animated.
With View.requestRectangleOnScreen() you can scroll to show a particular rectangle (the linked method allows disabling animation). You still will have to calculate that rectangle, but once you have the position of the particular child (your header) you can easily do that getting the size of the ScrollView. You could consider the small offset you want in calculating that rectangle or just do a scrollBy() after requestRec....() (the former way is better I think).
An easy way to use a density independent dimension is to define it in xml as a dimension resource.
-- edit: you get the position of the child in parent with getLeft() / getTop().

Categories

Resources