There was one particular issue that I ran into that is worth noting, for its solution was not immediately obvious. I was making a two-column GridLayout, where an image was on the left and text was on the right. Here's a simplified version of this layout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Space android:layout_width="100dp" /> <TextView android:text="@string/two_cities" /> </GridLayout>
The two Views would align next to each other nicely, but the TextView on the right would end up overflowing off the edge (the example below is not cropped - this is how it appears on the device):
What was happening was that the TextView would take up the entire width of the GridLayout, instead of taking up just the width of the cell (as I expected).
It turns out the solution is to set the width of the TextView to zero, then let the View fill its space dynamically using layout_gravity:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Space android:layout_width="100dp" /> <TextView android:layout_width="0dip" android:layout_gravity="fill_horizontal" android:text="@string/two_cities" /> </GridLayout>
Now the problem is solved:
You need both of the highlighted attributes above; one or the other won't fix the problem. However, it will nicely solve the issue and should be kept in mind anytime you setup a GridLayout child to fill horizontally or vertically.
I was missing the android:layout_width="0dp". Not sure why that is necessary, but it certainly worked well.
ReplyDeleteI believe the reason this is necessary is because the rendering happens in two steps:
Delete1. Determine the width of the view based on layout_width.
2. Adjust the width based on layout_gravity.
If the width is already set to a large size due to step #1, it won't reduce it in step #2. But if the width starts small, it will expand due to layout_gravity.
I think you meant
ReplyDeleteapp:layout_gravity="fill_horizontal"
instead of
android:layout_gravity="fill_horizontal"
Thanks a lot. You ended my headache :-)
ReplyDeleteThanks, you solved my problem.
ReplyDeleteThank you for posting this! Just spent hours trying to figure out wtf was going on until I found this and problem solved :)
ReplyDeletethanks a lot for this, saved me a couple of hrs.
ReplyDeleteNot a lot of working answer on internet . Thanks
ReplyDeleteThanks for sharing this! It was super helpful.
ReplyDeletehey, i am having an issue with this trick. If the text is in the right most cell then this works well but it doesn't work when the text is in the left most cell which I want to scale dynamically and the cell on the right is fixed.
ReplyDeleteDitto to what others have said - this was SUPER helpful. Been pulling my hair out trying to figure out why and you solved it for me. Thanks SO much!!!
ReplyDeleteOmg, thank you so much. Have been struggled with this for all day :'(
ReplyDeleteYou are a real scholar, sir. Thank you so much.
ReplyDelete