Archive for category Android

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

, , , , , , ,

14 Comments