Matrix of linear layout and manipulation in code - android

Let say that I have an layout in res/values named layout1.xml.
In this layout I have only one linear layout (the black one on the picture)
The thing I want to do is to add an array of linear layouts just like on the picture.
The red ones are linearlayout with horizontal orientation that contains 5 other linear layouts.
I want to do everything in code and I want to set an onclick listener to each of the layouts so when one is clicked I want to hide it. Everything needs to be putted in a function that will return a Layout and the this method should take prams for the rows and cols
public LinearLayout getLayout(int rows,int cols){
return the_layout;
}

You make a LinearLayout with the constructor new LinearLayout(context)
You add layouts with parentLayout.addView(childLayout).
You should be able to do the rest.

Related

Adding views dynamically to LinearLayout not working as expected

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.

Layout above - center other layouts

Need to put an information layout above the others.. but the half up of layout 3, is below the layout 1..
what i need.. layout 3 above the others, and in the center of the padding
what i have.. layout 1 above 3, and layout 3 above layout 2
using RelativeLayout i get layout 3 below the others, the opposite.
is there an option or something like bring to front??
Any other clue?
Thanks!
Each View's position on the Z-axis is dependent on its order in the XML file. Try placing the layout you want on top after the other two layouts in your XML file. If you want to change the View's position in your java code, you can use
view.bringToFront();
http://developer.android.com/reference/android/view/View.html#bringToFront%28%29

Handling an EditText that was added programatically

I've been making all of my views dynamically, and now I've come to the point where I want to add an EditText for people to write in.
I've been able to accomplish this for the most part, but it doesn't look right. I have a linear layout that I'm adding a relative layout to. I'm making the relative layout have a white background, then adding the EditText. Problem is, it always adds it to the direct center of the relative layout, and options to align it vertically to the top have so far failed.
I also need to be able to pull the text from it later when a separate button was pressed (I know how to make the button work, it's the pulling text from it part I'm a bit iffy on). Here's my code so far:
public void addEditText(LinearLayout L){
EditText myEditText = new EditText(c);
myEditText.setSingleLine(false);
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(scWidth1, 300);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER_VERTICAL);
l1.addView(myEditText);
L.addView(l1);
}
l1.setGravity(Gravity.CENTER_VERTICAL); places the EditText in center vertical of the parent container i.e RelativeLayout, remove that line.

Array containing all instances of views with Tag = x

Is there a way to create an array containing actual instances of views. For example, if I have one LinearLayout called Container that has within it 3 other LinearLayouts with the same Tag attribute and I wanted to get a list containing all 3 LinearLayouts so I can loop through and handle each.
Tried:
LinearLayout[] layouts = (LinearLayout[]) Container.FindViewWithTag(tag);
and
List<LinearLayout> layouts = (List<LinearLayout>) Container.FindViewWithTag(tag);
and
foreach(LinearLayout layouts in Container.FindViewWithTag(tag))
None of these have been acceptable to Android so far. Another acceptable way to handle my situation would be to just be able to assign each LinearLayout a Parent. But I haven't found a way to programatically set a view's parent, only how to get a view's parent.
What I have understood from your question that you want the child of Linear Layout. Yes, you can get the child of linear layout by container.getChild(index)
for(int i=0;i<container.getChildCount();i++){
View child=container.getChildAt(i);
//your processing....
}

How to set two layout one at top and anther at bottom

hi I have 2 layout inside a framelayout and i want one linearlayout at top and another at bottom where i will specify height for both the layouts
and how to set this parameter for the linearLayout "layout_gravity " by code not by XML
Use a surfaceView , the SurfaceView allows you to stack in the Z order.
Docs
make sure your surfaceView is under the views of the views you want to stack.
Hope this helps
first fetch the layout from id.
LinearLayout layout = (LinearLayout)findViewById(R.id.IDOFLAYOUT);
//the set the gravity by this method
layout.setGravity(Gravity.CENTER);

Categories

Resources