Tuesday 18 March 2014

Android Dynamic TextView Tutorial


Android provides a very exiting feature Dynamic User Interface, By which user can adds as much Views (TextView,EditText,RadioGroup, Layouts etc.) at run time without much worry.
  While learning i got some tutorials which are much complicated to new Android learners. so i decided to provide a Simple Android Dynamic View tutorial.

In this tutorial, There is a EditText called boxes, in boxes you set the number of EditText and TextView, then you click on Button click to add more then it creates number of boxes defined in the edit box.
Below is code.

package com.example.dynamicv;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    int i = 0;
    private static int n =0;
   
    List<EditText> editlist = new ArrayList<EditText>();
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //    setContentView(R.layout.activity_main);
       
        ScrollView scrl = new ScrollView(this);
       
        final LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        scrl.addView(ll);
        Button btn = new Button(this);
        btn.setText("click to add more");
        ll.addView(btn);
       
        final EditText boxes = new EditText(this);
        boxes.setHint("Set No. Of TextBoxes");
        boxes.setInputType(InputType.TYPE_CLASS_NUMBER);
        ll.addView(boxes);
        btn.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                try {
                   
                    n = Integer.parseInt( boxes.getText().toString());
                    Log.d("nnnnnnnnnn ", ""+n);
                    if(n>0){
                       
                        for(int j=1;j<=n;j++){
                            i++;
                            TextView tv = new TextView(MainActivity.this);
                            tv.setText("TV"+i);
                            ll.addView(tv);
                           
                            EditText edit = new EditText(MainActivity.this);
                            edit.setText(i+")-");
                            editlist.add(edit);
                            ll.addView(edit);
                        }
                    }else{
                        Toast.makeText(MainActivity.this, "Plz set the no. of boxes", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }   
               
            }
        });
       
        Button bt = new Button(this);
        bt.setText("save");
        bt.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
               
               
                if(n>0){
                    StringBuilder str = new StringBuilder();
                    for(EditText edt : editlist){
                        str.append(edt.getText().toString()+"\n");
                        //    String s =edit.getText().toString();
                    }
                    Log.d("strrrrrrr",""+str);
                    Toast.makeText(MainActivity.this, str,Toast.LENGTH_LONG).show();
                }
                else
                    Toast.makeText(MainActivity.this, "no data found",Toast.LENGTH_LONG).show();
           
            }
        });
        ll.addView(bt);
       
        MainActivity.this.setContentView(scrl);
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}



screen shots of the above code
Enter number of boxes
now click Button "click to add more".
now fill the edit boxes and press the Button "save"

Thats all you need to do. now enjoy that enough for the basic.