Search code examples
androidfor-loopandroid-edittexthorizontalscrollview

Add EditText views dynamically to Horizontal Scroll View


In my app, the user inputs a number (in this case, the number of reactants in a chemical equation, so the number of starting materials) and as a result a number of boxes, the amount matching that of the number given by the user, are produced/spawned/created below after pressing a button.

How do I go about doing this? I thought a for-loop would come into play, so I started it off.

Here's the Java class:

public class EquationBalancer extends Activity {

    final EditText reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
    final int reactantNom = Integer.parseInt(reactantNumberField.getText().toString());

    HorizontalScrollView reactants = (HorizontalScrollView) findViewById(R.id.reactants);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.balancelayout);

        Button setReactantsNo = (Button) findViewById(R.id.reactantsNumberOk);
        setReactantsNo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 1; i < reactantNom; i++) {
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.balancelayout, menu);
        return true;
    }

}

and the XML layout:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/reactantsHowMany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:text="@string/reactantsHowMany"
        android:textSize="18dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/reactantsNumber"
        android:paddingTop="5dp"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/reactantsHowMany"
        android:layout_marginLeft="60dp"
        android:inputType="number"
        android:hint="e.g. 4" />

    <Button 
        android:id="@+id/reactantsNumberOk"
        android:paddingTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/reactantsHowMany"
        android:layout_alignParentRight="true"
        android:layout_marginRight="60dp"
        android:layout_alignBottom="@id/reactantsNumber"
        android:text="Set" />

    <HorizontalScrollView
        android:id="@+id/reactants"
        android:paddingTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/reactantsNumber" >

    </HorizontalScrollView>

</RelativeLayout>

Solution

  • for (int i = 1; i < reactantNom; i++) {
        EditText editText=new EditText(this);
        editText.setText("some text if you want else remove this line");
        reactants.addView(editText);
    }
    

    and if you want to get data from these edittext some where else in your code then maintain an array of edittext and save these edittext objects in that array..,.

    Try this..,.