I have two TextViews with dynamic content and thus dynamic widths I want to arrange next to each other horizontally using Android xml layouts.
If the sum of their intrinsic widths fits into the width of the container, they should each just take the space they need.
If the sum of their widths exceed the container's width, their widths should both be capped in certain ways.
Let's say our left view has a width of 400 and the right one has a width of 200. If the available space is 1000, then there should just be a space of 400 between them. But if there is only an available space of 500, then available space should be distributed:
corresponding to the intrinsic widths: 333, 167
or in a weighted way, for example 1 - 1: 250, 250
or spreading the left view maintaining a minimum width of 50 of the right view: 450, 50
All of these contraints seem to be easy to implement using a horizontal LinearLayout or a ConstraintLayout but it's actually hard to me to achieve both - allowing spreading in case of sufficient space an controlled reduction in case of insufficient space.
Basically, incorporation of WRAP_CONTENT breaks controlled reduction and not using WRAP_CONTENT breaks dynamic distribution of available space, as far as I found out yet.
Could you please provide a working solution?
PS: I don't want to use any custom or third party container view.
Related
I need to create a 2 by 3 grid, where all the screen space is divided evenly - and I know that nesting linear layouts with weights is bad practice, so I'm looking for an alternative solution.
I need the space to be divided evenly, regardless of screen size - 3 rows with 2 columns each.
From what I understand, GridView/GridLayout/TableLayout will not automatically fill space or divide it evenly - so is there any alternative that will suit my needs?
I know you can set the Layout Params programmatically - is this better practice than using nested LinearLayouts with weights?
Thanks for your help
EDIT: To be clear, I intend to display six images, two in each row with three rows. I simply want to divide the screen space evenly - that is, have three rows of the same height, and two columns in each row of the same width.
EDIT2: This is what I'm using as of now, though I am still interested if someone has a better recommendation. I'm not sure this is better than using nested layout_weight values
In my XML I've weighted 3 LinearLayout containers to each take up 1/3 of the available screen height using layout_weight. (XML has a LinearLayout as it's root element)
Each container as two images as children, for which I'm setting the width programmatically as follows:
//Get appropiate width
DisplayMetrics metrics = con.getResources().getDisplayMetrics();
int width = metrics.widthPixels / 2;
//Set gif views to correct width - half of screen size
teamViewGif1.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif2.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif3.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif4.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif5.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
teamViewGif6.setLayoutParams(new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT));
Again, I'm still interested if anyone has a better solution or advice
EDIT3: Changed layout to use relative layout instead, and now setting both the images height and width programmatically.
I would like to reserve space at the bottom of a layout for a row of buttons, and give the rest over to my main view (e.g. a WebView).
I know about LinearLayout using a height of 0dp and a weight ... but that gives a proportional division, e.g. 80% for webview and 20% for buttons (wrapping the buttons in a RelativeLayout and giving it the weight of 20).
On different devices, though, 20% may be overkill for the row of buttons. I need to reserve a specific amount of space for the buttons -- say, 48dp -- and then give the rest of the layout to the WebView.
How is this accomplished?
You can set weight with height="0dp for one view and set only height for another - that way the view with weight will fill remaining space.
I just started using SpriteBuilder and was wondering if something like “autolayout” (in a basic form) was possible for the apps? You see I have made this simple layout ( http://cl.ly/Szs3 ) containing a header (blue-ish) and body (red).
Now what I want is that the header has a fixed height (it has 100% percent width, so that goes wel) and the red block “fills” the rest of the screen. So when the device is a taller device more content at once can be shown.
Is this possible? And if so, how could I acchieve this.
Yes there is such an option, yet the naming is a little bit different.
I uploaded my example project to this GitHub Repo: https://github.com/MakeGamesWithUs/Spritebuilder-Simple-Autosizing
Your top container needs a static height, and a relative position and a Y Anchor Point of 1. This way the top container always has a size of 100 points and is always positioned at the top of the screen:
Your bottom container needs a height inset of 100, this means your container will use the complete height of the parent container, except for 100 points at the top:
You can take a look at:
Auto Layout[About] Constraints
UIStackView
I am currently trying to make a view with four buttons that each take up one quarter of the screen. I know you can use android:layout_weight to set the weight so that the extra space on one axis is filled up, but is there a way to set it so that the height and width are evenly distributed among the four buttons using layout_weight? If not, what is the correct way to do this?
use table layout and create two rows for 4 buttons,determined the height and width
all of these codes are ( XML code)..
should that will working
I notice that in online tutorials people use specific dp values for width and height of any view
For example, android:layout width ="20dp"
I was wondering since we have so many devices and densities would it be better to determine this value programmatically?
For example I want a specific image to occupy 20% of the screen width then I would get the screen width and multiply by 20% and set width accordingly
U know dp is supposed to make it equal size on any screen no matter what density is but this not the case for many devices and example is galaxy s2 and galaxy note
Can you please enlighten me of my ways are correct?
the better way to do it is to use linear layout in your xmls and set layout_weight in it children with the value you want. You can use weight_sum in the linear layout to set the max weight too.
e.g
linear weight_sum = 100 and a textview inside with layout_weight = 20. it means your textview has 20% of the value of the linear.
p.s: for horizontal orientarion, weight = width and width = 0dp
for vertical, weight = height and height = 0
I hope to help you ^^
For anything that dp doesn't adequately compensate for, you can insert images of different resolutions into your alternate draw able folders. They're broadly named for the different screen sizes your app will come in contact with and android will adjust accordingly by itself. In my experience, I try to do as much graphics as I can by xml as I find it far less cumbersome.