1

How would I display a certain number of EditTexts on an Android layout based on user input? For example, I am creating a simple GPA Calculator app, and I need the multiple EditTexts based on however many classes the user is taking. I want to make the range from 1 to 6 classes. Would the easiest way be to create 6 EditText fields and only display however many the user needs when he or she specifies, or is there a better way to do this?

Thank you!

Jason John
  • 934
  • 2
  • 12
  • 23

3 Answers3

3

You can create the EditText programatically.

btnClick.setOnClickListener(new OnClickListener(){
  //loop based on classes needed
  EditText myEditText = new EditText(context); // Pass it an Activity or Context
  myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
  myLayout.addView(myEditText);
}); 

Check this out.

Community
  • 1
  • 1
SteD
  • 13,909
  • 12
  • 65
  • 76
  • Thanks, it makes sense to do this. Would the positioning, sizing, etc., be fulfilled in the .setLayoutParams method? If not, how is this done? – Jason John May 16 '14 at 00:49
0
// Try this way,hope this will help you...

**activity_main.xml**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edtNoCreate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Enter no EditText wan create"
            android:inputType="number"/>

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create"/>
    </LinearLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="5dp">
        <LinearLayout
            android:id="@+id/lnrDynamicEditTextHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>
</LinearLayout>

**MainActivity.java**

public class MainActivity extends Activity{

    private LinearLayout lnrDynamicEditTextHolder;
    private EditText edtNoCreate;
    private Button btnCreate;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lnrDynamicEditTextHolder = (LinearLayout) findViewById(R.id.lnrDynamicEditTextHolder);
        edtNoCreate = (EditText) findViewById(R.id.edtNoCreate);
        btnCreate = (Button) findViewById(R.id.btnCreate);



        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(edtNoCreate.getText().toString().length()>0) {
                    try {
                        lnrDynamicEditTextHolder.removeAllViews();
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }

                    int length = Integer.parseInt(edtNoCreate.getText().toString());

                    for (int i=0;i<length;i++){
                        EditText editText = new EditText(MainActivity.this);
                        editText.setId(i+1);
                        editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                        editText.setHint("EditText "+(i+1));
                        lnrDynamicEditTextHolder.addView(editText);
                    }
                }
            }
        });
    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0
 for(int i=0;i<3;++i)
    {    LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
            android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

    EditText edttext= new EditText(this);

       edttext.setId(i);
    edttext.setLayoutParams(params);

    layout.addView(edttext);}
}
Sivagami Nambi
  • 332
  • 4
  • 9
  • @Aleksandar Sorry it was me by mistake. Am a newbie. – Sivagami Nambi Nov 02 '16 at 01:43
  • @AnttiHaapala This is to add a number say 'n' (here 3) Edit Text dynamically(programatically) – Sivagami Nambi Nov 02 '16 at 01:45
  • @AnttiHaapala R.id.LinearLayout in findViewById is the Id given to the Layout in .xml file, params are parameters you want to add to the editText, create a editText , set Id to it (so that you access it by this id later) ,add the parameters and add it to the layout. This method can be used when you are not aware of the number EditText (or any fields for that matter) while creating it initially , but it depends on the program dynamically. For Example mine was a BFS algorithm for a word game and the number blanks changes depending on user input. Hope it helps! P.S sorry for the late response! – Sivagami Nambi Nov 02 '16 at 01:52