What is the exact meaning of match_parent? - android-layout

I just want to check my understanding is right. In android studio, layout attributes there are layout_width, layout_height. I know both of them as Widget / Layout 's width and height. and we can assign match_parent there. According to my understanding, match_parent always means the widget's height/width is same as the Activity's.
Is my understanding correct??

Match parent means. The widget you're assigning let's suppose height then it'll require the same height as the parent widget /layout
For example if i give 420dp height to my linearlayout and then inside it i provide an imagview as match parent then it's gonna have the same height as linear layout

match_parent is a constant value for views to stretch according to their parents or.
let's say there is a RelativeLayout in your layout XML, whatever widget (View) you put in your root view would stretch according to the parent (which is root view or RelativeLayout in our case).

Related

Which layout would be the best one for my application?

I'm new to android. I want to ask which layout is best and easy to use in xml files.
I'm confused with constraint layout and linear or relative layout.
Relative Layout:
A relative layout displays its views relative to one another, so order is not that important. You can define the top most view at the end of the layout and provide details to show it on top left. The following attributes are used to define relative layouts:
Position relative to screen: You can align a view relative to screen using alignParentTop, centerHorizontal etc.
Position relative to other views: You can align a view relative to another view using above, below, toLeftOf etc.
Margins: You can provide margins using marginTop, marginLeft etc.
Linear Layout:
A linear layout displays its views next to each other either vertically or horizontally. So, if you define views in a row, they will be displayed one after the other. You need to specify orientation to define whether layout is vertical or horizontal. The following attributes are used to define linear layouts:
Weight: It specifies how much space each view spans relative to others. For example, in an e-mail application, you can give less weight to ‘To’ and ‘Subject’, and more weight to ‘Message’.
Gravity: It defines placement of a view’s contents. For example, if a view spans entire screen, but has only one line of text, then you can decide whether it should be displayed on top, center or bottom.
Layout Gravity: It defines the placement of the view itself.

Codename one - scrollable layout restrictions

I have done my own version of the PropertyCross Demo (provided in their demo section).
The problem I currently face is the size of the "Recent Search" area. While I have a non-scrollable container, I can easily define the preferred height. As the Box Layout adheres to the preferred size, all is well, with the little issue of not being able to scroll it and see more than one result:
recentSearchContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS)); recentSearchContainer.setPreferredH((int)(this.getContentP‌​ane().getHeight() * 0.1f));
Once I set the container to scrollable, the preferred height gets overwritten and takes up as much space as it needs, taking too much space from the BorderLayout Center piece above it.
How to manipulate the preferred size of scrollable components?
You don't manipulate the preferred size. Scrollables take up more space so if you need them to take up a specific amount of space you need to use the right type of layout which in this case might not be border layout...
Border layout gives NORTH/SOUTH elements their preferred height which might not be what you want. You might want a grid layout which will divide the height 50/50. You might want a table layout where you can define the height in percentages etc.
For those who are interested, here is the solution:
Setup a table layout with a single column and as many rows as you need (similar to box layout y axis or border layout which only north, center and south).
Set the table layout to non-scrollable so it defaults to 100% of your screen.
add the components with height % of the screen they should take up.
those components can be scrollable and will still stick to the height constraint!
// inside a form object, setup the layout
TableLayout tl = new TableLayout(3, 1);
tl.setGrowHorizontally(true);
setScrollable(false);
setLayout(tl);
...
// and add stuff to it
add(tl.createConstraint().heightPercentage(15), labelDesc);
add(tl.createConstraint().heightPercentage(50), compGroup);
add(tl.createConstraint().heightPercentage(35), recentSearchContainer);
Works like a charm!

Is it possible to set the colour of padding in gtk?

I am aware that it is usually considered bad practice to modify colours within a gtk app, and that usually you respect the user designated theme but is it possible? I just cant get it to work. I am using gtk2hs, a haskell wrapper for gtk. I can easily set colors for all other widgets, just not this one.
Edit: I wanted padding at the top of my textview, so I placed the textview within an alignment widget and set the padding on that. I have tried setting bg, fg, base and text colours for each possible state for the textview, its parent (the alignment widget), and the alignment widgets parent, a vbox.

Even distribution of views in a layout

Is there any way, I can add UI components (Buttons in my case) to any layout (RelativLayout in my case) and width of parent gets evenly distributed among all views.
say parent width = 100;
if I add 10 Buttons - all buttons should be of width 10.
thanks.
m
If you use a LinearLayout you can make use of layout_weight to evenly distribute the size.
For eg, if you have two buttons, to take half width each of its parent, you can give "layout_weight=1" in both the buttons. So both of them would share the space.
Checkout the layout_weight documentation for more details

What are the ideal parameters for the width & height of the main layout for widget?

In the case of Widget, if I use RelativeLayout as the main layout, then what will be the best parameters for layout_width & layout_height i.e. whether I have to use "match_parent" or "wrap_content"? Please provide some reference if possible. If the question is below standard then sorry for that.

Resources