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] ?
Related
I'm inflating views inside a linearlayout dynamically, however once the linear layout reaches the end of the first row, it cuts off the rest and doesn't start on the second row.
for(int a = 0; a < mSkills.get(i).size(); a++){
View singleSkill = LayoutInflater.from(mContext)
.inflate(R.layout.singleskill, holder.mSkillLayout, false);
TextView skillText = singleSkill.findViewById(R.id.singleskilltext);
skillText.setText(mSkills.get(i).get(a));
holder.mSkillLayout.addView(skillText);
}
For the linear layout I have it set to wrap_content for the height:
<LinearLayout
android:id="#+id/ll_skills"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/tv_description"
android:layout_margin="16dp"/>
I've tried setting it to a defined height e.g 300dp however that doesn't work either.
How can I make the layout start on the second row, once the first is full?
Linear Layout can either fill views horizontally or vertically so the 2nd row you are expecting cant to be done with linear layout only. you can try a horizontal scroll view for that to scroll horizontally. For the exact view-like flow that you described, you can use this 3rd party https://github.com/nex3z/FlowLayout
It can manage the flow of your dynamically inflated view such as if there is no space in the first line then it will put the next view in the second line.
also, you can use material design library chips https://material.io/components/chips/#usage
LinearLayout works exactly how it has to be because you specify it as horizontal. For such behavior, you need RecyclerView With GridLayoutManager or create your own layout;).
Actually it's doing exactly as it should be, LinearLayout is Linear!, and place its subviews in a single horizontal or vertical row.
My advice to you is that create dynamic horizontal LinearLayout as you already doing with TextViews. and put every 3 or 4 textviews (depending on screen size) inside it.
and put all LinearLayouts inside one vertical LinearLayout...
Of course in your case, it's not a good idea, the best thing you can do is to use recycler view. but I consider you have problem with that.
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);
I have a LayoutView in a ScrollView named MyLayout and I want to add a View composed by LayoutView with TextView and EdiText inside this ScrollView according a value,
for example with a method
addMyCustomViewToMyLayout(int x){
....
}
that adds the desired number of elements to the MyLayout in the ScrollView,
The CustomViews added to the ScrollView should be linked to the activity to get and set the value from-to the app.
Is possible?
How could I do?
There's nothing very special about ScrollViews. You can think of them as a boundless View. While normal Layouts like a LinearLayout are bound to an area, then ScrollView will allow its child to be whatever height it wants to be (or width in the case of a HorizontalScrollView).
ScrollViews contain one child. The child size is bound by the width of the ScrollView, but it is not bound by the height. Thus, the child will measure itself based on the idea that it has enough vertical space to show everything.
In this case, you create or inflate your MyLayout however you want. Then you add it to the ScrollView. If you want to add other Views to it, you add it to your MyLayout layout view. Just let the ScrollView do its thing.
I have a few views that I have to place in a 4 x 6 grid. Each of the views should have the same width w and same height h and fill the screen. What I want is to use GridLayout or GridView to draw these.
I know that I can use the stretchMode = "columnWidth" in GridView which sets equal width to each of its children. I also want it to set equal height and fill the screen vertically.
Can I achieve this by using any of GridView or if not possible then GridLayout? I am looking for GridView before switching to LinearLayouts and using weights because I have custom views as the grid items and I would love to use adapters rather than coding for each element.
You can still use Adapters with LinearLayouts or TableLayout, you just have to add the views programatically and call the getView() method yourself. GridView is more meant for a dynamic number of items and handles its own scrolling.
This can be done using something like this in your onCreateView() or whatever:
mAdapter = new FooAdapter();
mList = (LinearLayout) v.findViewById(R.id.list);
for(int i = 0; i< mAdapter.getCount(); i++){
View child = mAdapter.getView(i, null, mList);
mList.addView(child);
}
If you are possibly just changing the data, passing the old views as convertViews may speed things up.
I did a custom layout which extends linear layout. I found that for my case this is a better approach and I had more control over all the touch events on the layout and the children themselves.
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.