How to set Weight to make bottom view occupy remaining space Android - android

I want to model my layout screen in the following 2 parts.
Basic Information related Linear layout with expandable view initially 20 % of screen height.
Bottom List view occupy Remaining Space.
But When the top view is expanded then i want to make it height upto 50 % and the bottom listview should start then after.
I have attached image for reference.
I have tried setting the layout_weight of top and bottom layout. But not able to find out exact solution. May be i am missing some points.
Let me know if you need more information from my end.
Thanks in Advance.
regards,
Rakesh

Try to remove the current weight from the top view and in the listview set android:layout_weight="1" and android:layout_height="0dp". This makes the ListView to take whatever height left in the screen.

Related

How to set multiple scroll views size to change dynamically relative to each other?

I have two scroll views in a vertical linear layout.
I want them to be relative to each other so that they fill the entire linear layout and compensate if one cant cover half the screen.
Lets call that scroll views TOP and BOT.
If the screen can display 4 rows and both scroll views have infinite rows, each scroll views should display 2 rows and be able to scroll down to se the rest rows.
If TOP has 1 and BOT infinite rows, BOT should be resized to 3/4 of the linear layout.
If TOP have infinite and Bot has 1 row TOP should still just display 2, i.e. it should never pass the linear layouts vertical center.
Here are some pictures for reference:
my setup with weight set to 0.5/0.5.
result of 0.5/0.5 weight. Notice the gray bar above the BOT title bar. This empty space should be filled by the BOT bar.
if Using fixed size or wrap content the TOP will push the Bot out of view.
How can I have them hugging each other and still set TOP to a maximal height?
Preferable in XML.
Its better to set the weight dynamically. Count the number of items in both views. Set the weight of each view according to the ratios of their number of items. You can refer set weight dynamically for setting weights at run time

Android: how to align element 2 to the right of element 1 while only allowing element 2 to take up as much space as needed

I am trying to align two layouts horizontally but can't seem to get it working. The conditions I need are:
Layout 1 needs to fill up the whole width EXCEPT for the width that layout 2 requires
Layout 2 needs to have a width of 21dp and right / left margin of 17dp.
I can't take a picture because of my job, but here' some ascii art to illustrate what I want:
I attempted using a dummy View between the two layouts so that I could use a relative layout, but that wasn't quite working for me..so any suggestions?
Thanks!
<RelativeLayout layout_width="match_parent">
<Layout1 layout_width="match_parent" layout_toLeftOf="#+id/layout2"/>
<Layout2 id="#id/layout2" layout_width=21dp layout_alignParentRight="true" layout_marginLeft="17dp" layout_marginRight=17dp/>
</RelativeLayout>
Add in the rest of the parameters as needed
Try using a horizontal LinearLayout and set android:layout_weight="1" for layout 2. Like this layout 2 should keep it's size and layout 1 will fill the rest of the available space.

Android- How to display items with variable space between then

I have a horizontal layout to display 5 icons.
I would like to know if there is a way to display the first one always X dp from margin left and the last one X dp from the margin right and the others with the same space between then.
With LinearLayout the space is always the same even when I change the device , so sometimes my horizontal row became much more bigger than the space that the icons use and with RelativeLayout i just can align 3 items in this way ( alignParentLeft, centerInParent, alignParentRight)
With RelativeLayout I think you can easily put three of your icons at the proper place. For the last two icons, I don't think there's something you can do in your xml. Actually I have met the same problem before.
An approach is (maybe not the best one) to calculate the margins of the two icons in your java code. It's really easy to get the parent view width (in pixels) and you know how much space(a percentage) there should be before/after your views. Just a multiplication and addView with LayoutParam is sufficient.
A LinearLayout with android:width="match_parent", android:layout_marginRight and android:layout_marginLeft within a RelativeLayout should work.
The Linear Layout will be xdp from the left and xdp from the right, with the icons spaced evenly inside.
If you use the Linear Layout, you can use the weight attribute to calculate the correct spacing between the images.
Set each of their weights to 1 so that they take an equal space in the row :
android:layout_weight="1"
I hope that can assist you.

Android ellipse overflow priority

I have 2 textviews per horizontal linearlayout row on two rows. All the views are set as 0dp width and weight 1. They all have the same font and text size etc.
The views on the left side are gravity aligned left and the two on the right are gravity aligned right.
When both textviews text length overflow android always gives precedence to the textview on the right and ellipse the views on the left.
Is there a method that can be used to control which view ellipses when both views on the same row would not fit.
Ideally I want the views on the right to ellipse in favor of those on the left. Or failing that make them ellipse evenly per row.
thanks
i don't think there is a feature of order of the views to manage how they are measured.
you can customize the linearLayout by extending it in order to support this feature , but this is too hardcore for this task .
i would suggest putting the problematic views (those that take too much space and you don't with them to take too much space) into a new layout , and set its width to match_parent .
this way , it should take the rest of the space at the end of the measurements of the other views ,

Android - How to place an item on a layout relatively to the top?

How could I proceed to place an item on a LinearLayout, with a given space from the top? For instance, I would like to place the logo on the half of the top middle of the layout.
Thanks
There is no way to add percentages.
You can add two layouts with the same weight to split the screen in two and then add the logo with a centered gravity to the upper layout,
Or you can measure the screen and add the right amount of margins.
Use the paddingTop attribute of the view class.

Categories

Resources