Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Droid: How to reorder a linearlayouts contents programmatically?

I have a scrollview with a list of buttons. I want to have the instructions at the beginning of this list when the app is first used, but then move the instructions to the end off screen after the user has used it once as the instructions won't really be needed again but I still want them accessible. I have no idea where to start!

Edit: Different explanation as requested.

My main layout has a table. The bottom row is a HorizontalScrollView, containing a LinearLayout, containing 6 Buttons. If we call these Buttons 1, 2, 3, 4 ,5 ,6 as they are ordered in the .xml layout, I want in code to be able to reorder them to "6, 1, 2, 3, 4, 5".

like image 265
Tickled Pink Avatar asked Jan 02 '13 11:01

Tickled Pink


1 Answers

Just find your views then removethen from linearlayout and add by new order.

ln1= (LinearLayout)findViewById(R.id.ln1);

    btn1= (Button)findViewById(R.id.button1);
    btn2= (Button)findViewById(R.id.button2);
    btn3= (Button)findViewById(R.id.button3);
    btn4= (Button)findViewById(R.id.button4);
    btn5= (Button)findViewById(R.id.button5);
    btn6= (Button)findViewById(R.id.button6);

    ln1.removeAllViews();

    ln1.addView(btn6);
    ln1.addView(btn1);
    ln1.addView(btn2);
    ln1.addView(btn3);
    ln1.addView(btn4);
    ln1.addView(btn5);
like image 118
Talha Avatar answered Sep 23 '22 21:09

Talha