1

I want to add a group of elements (textbox, button, progress bar and text view) and have them appear dynamically when pressing a button in Android. At every button press I want to create a group of the elements and put them in an relative layout. Here is my oncreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // The add button to insert new downloading file
    add = (Button) findViewById(R.id.button_add_url);
    add.setOnClickListener(this);

    // Swapping between pause and resume
    pause = (Button) findViewById(R.id.button_pause_resume);
    pause.setOnClickListener(this);

    download = (Button) findViewById(R.id.button_download);
    download.setOnClickListener(this);

    // The progress is written here
    text = (TextView) findViewById(R.id.textView2);
    text.setTextColor(Color.WHITE);

    progress = (ProgressBar) findViewById(R.id.progressBar1);
}
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61

1 Answers1

0

maybe it would've been easier if you create relative layout as xml containing those elements and then on button click you can inflate that layout to main layout like this (on your button onClick method):

LinearLayout mainLayout = (LinearLayout)findViewById(R.id.personal_data_root);
RelativeLayout addLayout = (RelativeLayout)View.inflate(this, R.layout.layoutcontainingelementsouwanttoaddonclick, null);
mainLayout.addView(addLayout);
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
  • Great it finally worked but appear in the same place, how should i appear them relative to another or in other words the newest in the bottom ? – Mohamed Abo Sham3a Mar 22 '13 at 16:59
  • I suppose you already solved this but if not, check out these questions http://stackoverflow.com/questions/5280844/how-to-align-view-elements-in-relativelayout-dynamically-through-code-in-android, http://stackoverflow.com/questions/14139214/android-dynamically-add-layouts-under-each-other.. basically what you need is relativelayout.layoutparams so you can set some rules about aligning. – Marko Niciforovic Mar 25 '13 at 11:23