Creating Dynamic Views in android

I was just in search of creating views without XML in android and yes, found the solution. So just thought of sharing the same with you all.

I’ve created an easy to understand example for you. Here, in my example I’ve created a button which when clicked each time adds a TextView and an EditText with its serial numbers. Confused?

Just read on and you will understand better.

Let’s start now. First of all I’ve created an object of ScrollView within which I’ll add the LinearLayout . Here is the code:

ScrollView scrl=new ScrollView(this);
 final LinearLayout ll=new LinearLayout(this);
 ll.setOrientation(LinearLayout.VERTICAL);
 scrl.addView(ll);

In the above code, I’ve created an object of ScrollView and also a LinearLayout. Then I’ve set the Orientation for the layout and finally added the view which here is the LinearLayout  to the ScrollView.

NB: If you want to know the purpose of ScrollView, please do refer ScrollView(link).

Next, we are going to add Button using java code.

Button add_btn=new Button(this);
 add_btn.setText("Click to add TextViiews and EditTexts");
 ll.addView(add_btn);

Here, I’ve created the object of Button and then set the Title and finally, added it to the LinearLayout.

Now, with in the Button click  added the TextView and EditText  as shown in the code snippet below:

add_btn.setOnClickListener(new OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 i++;
 TextView tv=new TextView(getApplicationContext());
 tv.setText("Number"+i);
 ll.addView(tv);
 EditText et=new EditText(getApplicationContext());
 et.setText(i+")");
 ll.addView(et); 
 }
 });

So as we click on the Button a TextView and an EditView is created dynamically.

Finally, we need to make these contents added to be visible to the user. So we use setContentView()  and within the function the object of  ScrollView is placed as it holds all the other View.

this.setContentView(scrl);

Now the Whole code:

package com.deepthi.dynamicviews;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class DynamicViewsActivity extends Activity {
 int i=0;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 //setContentView(R.layout.activity_dynamic_views);
 ScrollView scrl=new ScrollView(this);
 final LinearLayout ll=new LinearLayout(this);
 ll.setOrientation(LinearLayout.VERTICAL);
 scrl.addView(ll);
 Button add_btn=new Button(this);
 add_btn.setText("Click to add TextViiews and EditTexts");
 ll.addView(add_btn);
 add_btn.setOnClickListener(new OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 i++;
 TextView tv=new TextView(getApplicationContext());
 tv.setText("Number"+i);
 ll.addView(tv);
 EditText et=new EditText(getApplicationContext());
 et.setText(i+")");
 ll.addView(et); 
 }
 });
 this.setContentView(scrl);
 }
@Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.activity_dynamic_views, menu);
 return true;
 }
}

The Screen Shots:

 

 

 

Hope this post was useful. Keep Reading.. 🙂 Happy coding..:)

Advertisements

, , , , , , ,

  1. #1 by meenu on October 19, 2012 - 5:59 pm

    good tutorial….

    can u explain
    1) y u made only the linearlayout ‘final’
    2)getApplicationContext()
    3)y eclipse didnt allow me to override the onclick method(any application did work alright)

  2. #2 by imran on October 16, 2013 - 8:01 pm

    goood job

  3. #3 by imran on October 16, 2013 - 8:05 pm

    help me in……………….android with web services

  4. #4 by faisal khan on February 11, 2014 - 2:36 am

    can you explain how can we get value from
    these editText when they have same name?

  5. #5 by Vishnu C on May 22, 2014 - 3:41 pm

    We can enter data to all dynamically created EditText. My question is How we can get the entered data from the text view after click a Button?

  6. #6 by nabila on June 4, 2014 - 3:14 pm

    thanks a lot
    its clear and easy
    you have a great way to explain things
    it was really helpful

  7. #7 by Nilesh Panchal on June 18, 2014 - 11:50 am

    nice example …
    thanks..

  8. #8 by Prasad on July 18, 2014 - 6:41 pm

    Besides getApplicationContext() use ‘this’

  9. #9 by App development for great ideas on August 19, 2014 - 10:24 am

    What plugin(s) do you utilize to stop bots from spamming your website?
    Evesry so often, I expeience a few pam isues and I
    will be reallyy grateful if someone like you can give me some
    pointers on how to solve them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: