0

I have a vertical LinearLayout, which I want to fill with horizontal LinearLayouts (which act like rows). Each of these should effectively be identical, except several items within them use a number, which should increase each time.

For example:

<LinearLayout android:id="@+id/row1" >
    <SeekBar
        android:id="@+id/seekBar"
        android:tag="1"
    />
</LinearLayout>
<LinearLayout android:id="@+id/row2" >
    <SeekBar
        android:id="@+id/seekBar"
        android:tag="2"
    />
</LinearLayout>
<LinearLayout android:id="@+id/row3" >
    <SeekBar
        android:id="@+id/seekBar"
        android:tag="3"
    />
</LinearLayout>

In JavaScript, I might use something like this to 'automate' their creation:

for (var i = 0; i < count; i++) {
    child = document.createElement("div");
    child.id = "row" + i;
    parent.appendChild(child);
}

Is there a way to 'automate' the creation of these, such that I can just have a single 'model' of one, then iterate over it several times, 'passing in' the number each time? I can use a Java-based or XML-based solution.

I've tried using for each one, which solved part of the problem, but couldn't find an effective way to 'pass in' a different number each time. (The numbers are to identify the inputs within the rows from Java - if there's a better way to manage this, that could also be helpful).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Z Smith
  • 254
  • 2
  • 15

1 Answers1

1

You can build Layout by Java code in the Activity - onCreate

You can dynamic modify of layout
For example: Build 3 seekbars and delete the first seekbar by id.

public class MainActivity extends AppCompatActivity {

    ArrayList<Integer> seekbarsID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        seekbarsID = new ArrayList<>();

        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        for (int i = 1; i < 4; i++) {
            SeekBar seekbar = new SeekBar(this);

            int id;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
               id = View.generateViewId();

            }else{
                id = 99999999+i;

            }
                seekbar.setId(id);
                seekbarsID.add (id);

            seekbar.setTag(i);
            layout.addView(seekbar);



        }


        setContentView(layout);
        layout.removeView(findViewById(seekbarsID.get(0)));

    }
}
slee
  • 239
  • 1
  • 4
  • if I want to be able to do this within different fragments, should this code be run within the fragment.onCreate() method, or elsewhere? And how do I use findViewById() (or otherwise access layouts) within that method (or wherever it is to be run)? – Z Smith Oct 21 '17 at 08:05
  • Elsewhere. You need storage the id in global variable that your system can call back the id, you also need storage the class or id "Layout" to global variable for future process. You also can get all Children and check the class type: https://stackoverflow.com/questions/8395168/android-get-children-inside-a-view – slee Oct 21 '17 at 09:12