Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

// 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);
                    }
                }
            }
        });
    }
}
 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);}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top