0

I am new to the Android and I am currently working on my android project.

I want to do this...

On the first Activity, the user will enter the number of strings he/she wants to input. For example, 3. (I am already done with this.)

And then, on the second activity, three edit text will appear for the entering the first, second and third string based on the user input in the first activity. If he/she enters 2, two edit text will appear on the second Java Activity. (How to do this one?)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

In your Activity1, pass the user's entry to the next activity:

int userSelectedVal=somevalue;
Intent mIntent = new Intent(Activity1.this, Activity2.class);
mIntent.putExtra("userSelectedVal", userSelectedVal);
startActivity(mIntent);

In your Activity2, retrieve this value and programmatically add the Edittext's depending on this value:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    int noOfEditTexts = extras.getInt("userSelectedVal");
    LinearLayout mLinearlayout = new LinearLayout(this);
    // specifying vertical orientation
    mLinearlayout.setOrientation(LinearLayout.VERTICAL);
    // creating LayoutParams  
    LayoutParams mLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    // set LinearLayout as a root element of the screen 
    setContentView(mLinearlayout, mLayoutParam);
    for (int i = 0; i < noOfEditTexts; i++) {
        EditText mEditText = new EditText(context); // Pass it an Activity or Context
        myEditText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mLinearlayout.addView(mEditText);
    }
}
Sanket Mendon
  • 403
  • 5
  • 11