Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add scrollview in layout and linearlayout in scrollview

How do I dynamically add a scrollview in my layout? and once I get that scrollview added I want to add a linearlayout inside it so I can now add controls in that linearlayout?

like image 373
SleepNot Avatar asked Aug 03 '12 05:08

SleepNot


2 Answers

I hope it will helpful to you.

Try this Code...

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);

        ScrollView sv = new ScrollView(this);
        sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        ll.setOrientation(1);
        sv.addView(ll);

        for(int i = 0; i < 20; i++) 
        {
            Button b = new Button(this);
            b.setText("Button "+i);
            ll.addView(b);
        }

        rl.addView(sv);


        /* If you want to set entire layout as dynamically, then remove below lines in program :
         * setContentView(R.layout.activity_main);
         * RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
         * rl.addView(sv);
         * 
         * And Add below line :
         * this.setContentView(sv);

        */

    }
like image 95
Balaji Avatar answered Nov 01 '22 05:11

Balaji


try this

http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/

like image 34
Youddh Avatar answered Nov 01 '22 03:11

Youddh