user1217698
user1217698

Reputation: 1

set text on dynamically created edittext in android?

hai am new in android i have created a dynamically button and edit text creation i have two pages 1 for entering how many button and edit text should be created. (in that page ihave only one edit text filed and button if i enter 3 .three edittext and 3 button will create on next page dynamically) if i press three the next page will be like three button and three edittext

i have want to print hello on the first edit text on pressing enter ur1st time button but when i press that button the value is printed on the third edittext
the code is given below

package a.l;


   public class dyna extends Activity {
TextView t1;
Button btn;
EditText edt;
int i,j,t = 0,mHour,mMinute,count=0,mDate,mYear,mMonth;
Button b;
Toast mToast;
ScrollView sv;
Calendar c = Calendar.getInstance();
SQLiteDatabase myDB ; 
PendingIntent sender;
AlarmManager am;
Intent intent;
int alarmtype;
long timeinmilli;



protected static final int TIME_DIALOG_ID = 1;
 private static final int DATE_DIALOG_ID = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dyna);
t1 = (TextView)findViewById(R.id.textdyna);


 mHour = c.get(Calendar.HOUR_OF_DAY);
 mMinute = c.get(Calendar.MINUTE);
 mDate=c.get(Calendar.DATE);
 mMonth=c.get(Calendar.MONTH);
 mYear=c.get(Calendar.YEAR);
 String date=mDate+"/"+mMonth+"/"+mYear;
 count=1;



        Bundle bu =  getIntent().getExtras();
        String s1 = bu.getString("5");
        int i1=Integer.parseInt(s1);
        String s2 = bu.getString("3");
        String s3 = bu.getString("4");




//  if(date.equals(s2)){     
        sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setBackgroundResource(R.drawable.vec);


        sv.addView(ll);

        for(i = 1; i <i1+1; i++)
        {
           b = new Button(this);

           b.setText(" enter ur"+ i +"st time");
           b.setId(i); 
           b.setTextSize(10);
           b.setPadding(8,3,8,3);   
           b.setTextColor(Color.DKGRAY);
           b.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);     
           b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));        


           c.setTimeInMillis(System.currentTimeMillis());
            c.add(Calendar.SECOND,10);
            alarmtype=AlarmManager.RTC_WAKEUP;
            timeinmilli= c.getTimeInMillis();
           intent = new Intent(dyna.this, MyAlarmReceiver.class);
           sender = PendingIntent.getBroadcast(dyna.this,0, intent, 0);
           am = (AlarmManager)getSystemService(ALARM_SERVICE);



           edt=new EditText(this);
           edt.setId(i);
           edt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));





           ll.addView(b);
           ll.addView(edt);
           System.out.println("am on");

           b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int a=edt.getId();
                    int bh=b.getId();





                    //showDialog(TIME_DIALOG_ID);

                System.out.println("am on button");    

                        // We want the alarm to go off 10 seconds from now.


                      // c.add(Calendar.MINUTE,mMinute);


                        // Schedule the alarm!

   //                           am.set(alarmtype,timeinmilli, sender);
   //
  //                       
   //                           // Tell the user about what we did.
  //                            if (mToast != null) {
  //                                mToast.cancel();
  //                            }
  //                            mToast = Toast.makeText(dyna.this,"Alarm    Started on"+i,Toast.LENGTH_LONG);
  //                            mToast.show();
   //       
                       }
            });



        }



        this.setContentView(sv);    

//  }
  //      do it for ending alarm at this date
   ////   
   //////     if(date.equals(s3))
  ////////  {
  //////          
 ////     }
  //      

}

 private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;


            }


        };
        protected Dialog onCreateDialog(int id) {
            // TODO Auto-generated method stub
            switch (id) {


             case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                         mTimeSetListener, mHour, mMinute, false);


                        }
            return null;
        }
}

the code am using is given please find a solution thank you

Upvotes: 0

Views: 5809

Answers (2)

Chirag Patel
Chirag Patel

Reputation: 2320

Try this code

package com.openxcell.DemoAnimation;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class **DynamicEditText** extends Activity implements OnClickListener {

    ArrayList<Integer> _ArrListScpoe = new ArrayList<Integer>();
    // List<EditText> allEds = new ArrayList<EditText>();
    List<Button> allbtn = new ArrayList<Button>();
    RelativeLayout.LayoutParams ParaOne;
    EditText EtScopeDYN, EtBidDYN;
    Button btnDynamic, btnDynamicOne, btnOne;
    int _intClick = 0, _intClickBid = 0;
    LinearLayout LLAdd, LLDynamic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edittext);
        LLAdd = (LinearLayout) findViewById(R.id.LLAdd);
        LLAdd.setOrientation(LinearLayout.VERTICAL);

        btnOne = (Button) findViewById(R.id.button1);
        btnOne.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                DynamicEditScope();
            }
        });
    }

    public void DynamicEditScope() {
        ParaOne = new RelativeLayout.LayoutParams(120, 40);
        EtScopeDYN = new EditText(DynamicEditText.this);
        LLDynamic = new LinearLayout(DynamicEditText.this);
        btnDynamic = new Button(DynamicEditText.this);

        EtScopeDYN.setId(_intClick);
        // allEds.add(EtScopeDYN);
        EtScopeDYN.setTextSize(12);
        ParaOne.topMargin = 5;
        ParaOne.leftMargin = 55;

        btnDynamic.setText("Add" + _intClick);
        btnDynamic.setId(_intClick);
        allbtn.add(btnDynamic);
        btnDynamic.setOnClickListener(this);

        LLDynamic.addView(EtScopeDYN, ParaOne);
        LLDynamic.addView(btnDynamic, ParaOne);
        LLDynamic.setOrientation(LinearLayout.HORIZONTAL);

        LLAdd.addView(LLDynamic);
        _ArrListScpoe.add(_intClick);
        _intClick++;
    }

    public void onClick(View v) {
        for (int i = 0; i < _ArrListScpoe.size(); i++) {
            if (v.getId() == i) {
                EtBidDYN = (EditText) findViewById(_ArrListScpoe.get(i));
                EtBidDYN.setText(allbtn.get(i).getText().toString());
            }

        }

    }

}

Upvotes: 1

Shashank Kadne
Shashank Kadne

Reputation: 8101

Create separate arrays for Button and EditText...so that it will be easy for you to identify. Also, I can see that you are setting the same id to both Button and EditText.

Inside array, just initialize your arrays and inside the setOnClickListener for your Button, set the corresponding EditText

Upvotes: 0

Related Questions