GridLayout change only one specific element? - android

I have GridLayout 7x7, is it possible to change content only of cell (3,4) and I want to do it dynamically.
Example: I have 7x7 grid of buttons and want to change button on position (3,4) to TextView.
EDIT: Also, is it possible to set zero padding around child?
Thanks!

In GridView you cant access elements specific by coordinates (3,4).
The elements are numbered like in 2D arrays. So your (3,4) element will have (7*3+4)-1 index. And you can access it by.
ViewGroup gridChild = (ViewGroup) mGridView.getChildAt(24);
EDIT: Set padding to zero.
gridChild.setPadding(0,0,0,0);

Related

(Linear)Layout - How to get the number of elements?

Can I somehow extract the number out of a Layout?
I have code similar to this:
linearLayout.addView(randomView, 3);
This means, the element randomView is inserted at position 3 (so it is the 4. element).
I removed another element inside the linearLayout and because of the hardcoded number 3 I ran into an Exception. I want to add my randomView to the end of the layout, so it would be nice if I could do something like this:
linearLayout.addView(randomView, linearLayout.size());
Even if I'm changing the elements of a specific layout, the randomView would then always be at the end of all other elements.
If you don't specify the position view will be added at the end of the ViewGroup.
linearLayout.addView(randomView);
But if you need to specify the position you can get the child count and do this
int childCount = ((ViewGroup)linearLayout).getChildCount();
linearLayout.addView(randomView, childCount);
If you need to access the view inside your linear layout then you can do this
View childView = ((ViewGroup)linearLayout).getChildAt(position);
Why just not do:
linearLayout.addView(randomView);
I remember it adds the view to end of the viewgroup.

How to iterate on GridLayout cells?

I would like to iterate programmatically over a GridLayout cells, the iteration should be applied on the layout bounds, where each cell contains an ImageButton, i understand that the layout is a mix of LinearLayout and TableLayout.
Is there a way to iterate over the GridLayout cells?
Or, what is the equivalent code to pseudo : View v <- layout[i][j] ?

Setting a semi fill_parent layout width in an Android interface

I am trying to create a table row element where the first line contains a title followed by an ImageView that should be all of the way to the right. The TextView should fill the entire width of the phone except for the ImageView at the end.
The solution depends on some details of the circumstances.
If all the rows have the same format
If all the rows have the same column layout, then set android:stretchColumns="0" on the TableLayout. This will make the first column (index 0) stretch to fill any remaining space.
If the header has a different format from the remaining rows
If you have more columns or need a different column layout for the rest of the rows, then you need to do something different. I don't believe any single item can span multiple columns.
If only the positioning is important and you don't really need to span across multiple rows, you can use the android:layout_column attribute on each of the header items. The column number is 0-based, so the first column is 0. The Eclipse layout builder doesn't seem to present this attribute, but it will handle the attribute if you type it into the xml.
If you can't fit your elements inside the same columns the rest of the table uses, then the header doesn't belong in a TableRow. As suggested in another answer, you can use a RelativeLayout instead of the TableRow. Alternately, you could move the header outside of the table.
First, use a RelativeLayout for your row. Use the typical ImageView, give it an id, and use the XML attribute android:layout_alignParentRight="true". Next, use the TextView, use android:layout_toLeftOf="#id/myid" and make sure it's width is "fill_parent".
edit: code tags

How to make a View to fill the space between two views?

I want to layout my views in the following way: [Button] [SomeView] [Button]. I want to set specific sizes for buttons (in mm), and then have the SomeView fill the remaining space between them.
How to achieve this?
Maybe you could set the layout:weight of the [SomeView] to 1 and put all of these views in a linearlayout. Hope it works!

Best android layout for a set of columns

On an Android layout, I'd like to have a set of rows, each with two TextViews. The leftmost column of TextViews should be right-aligned, just left of an imaginary centerline down the screen. The rightmost column should be left-aligned.
Examples of this can be seen at http://stuff.greenberg.org/ScopeCalc.htm
What's the best layout to use?
IMO, TableLayout would be a logical choice with appropriate use of colspan/rowspan.
You can also do this using LinearLayout, with the two sub-views of each row each getting 50% of the width.
Using GridLayout you can apply columnspan and rowspan properties to the views inside the grid.

Categories

Resources