Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add edittext dynamically and retrieve values in android

Tags:

android

i added Edit Text dynamically but not get values ,get values for last Edit Text . please help me...

like image 787
Chirag Patel Avatar asked Aug 16 '11 11:08

Chirag Patel


People also ask

How to add multiple EditText Dynamically in Android?

Use a horizontal linear layout and add two edit texts inside. Try this : LinearLayout linearLayout = new LinearLayout(this); linearLayout. setLayoutParams(new LinearLayout.


1 Answers

public class EnterText extends Activity {

    Button btnMyLine,btnSave;
    LinearLayout LLEnterText;       
    int _intMyLineCount;

     private List<EditText> editTextList = new ArrayList<EditText>();
     private List<TextView> textviewList=new ArrayList<TextView>();
     private List<LinearLayout> linearlayoutList=new ArrayList<LinearLayout>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entertext);



        LLEnterText=(LinearLayout) findViewById(R.id.LlTitle);

        //LLEnterText.setOrientation(LinearLayout.VERTICAL);
        btnMyLine=(Button) findViewById(R.id.btnMyLines);       
        btnSave=(Button) findViewById(R.id.btnSave);


        btnMyLine.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {                       
                    LLEnterText.addView(linearlayout(_intMyLineCount));
                    _intMyLineCount++;
                }
        });     

    btnSave.setOnClickListener(new OnClickListener() {      
            @Override
            public void onClick(View v) {   
                if (ETTitleEnterText.getText().length() == 0)
                {
                    Toast.makeText(EnterText.this, "Please Enter Full Details", Toast.LENGTH_LONG).show();
                }else{

                        for (EditText editText : editTextList) {                            
                            StartTabHost.VARClass._ArrLinesDetails.add(editText.getText().toString());
                        }   
                        for(TextView textview:textviewList){
                            StartTabHost.VARClass._ArrLinesTitle.add(textview.getText().toString());
                        }
                        for(int i=0;i<StartTabHost.VARClass._ArrLinesTitle.size();i++)
                    {
                    Log.d("LinesTitle",StartTabHost.VARClass._ArrLinesTitle.get(i));
                    Log.d("LinesDetails",StartTabHost.VARClass._ArrLinesDetails.get(i));
                    }               

                }

            }
        });
    }

private EditText editText(int _intID) {
            EditText editText = new EditText(this);
            editText.setId(_intID);
            editText.setHint("My lines");
            editText.setWidth(180);         
            editText.setBackgroundColor(Color.WHITE);
            editTextList.add(editText);
            return editText;
        }
    private TextView textView(int _intID)
    {
        TextView txtviewAll=new TextView(this);
        txtviewAll.setId(_intID);
        txtviewAll.setText("My lines:");        
        txtviewAll.setTextColor(Color.RED);
        txtviewAll.setTypeface(Typeface.DEFAULT_BOLD);
        textviewList.add(txtviewAll);
        return txtviewAll;
    }
    private LinearLayout linearlayout(int _intID)
    {
        LinearLayout LLMain=new LinearLayout(this);
        LLMain.setId(_intID);       
        LLMain.addView(textView(_intID));
        LLMain.addView(editText(_intID));
        LLMain.setOrientation(LinearLayout.HORIZONTAL);
        linearlayoutList.add(LLMain);
        return LLMain;

    }   

}
like image 64
Chirag Patel Avatar answered Sep 27 '22 21:09

Chirag Patel