6

I am new to android programming and I have a problem of creating text fields dynamically;

I want to create a view in which there is a button named "Create text fields" and two Edit Text one Edit Text name is ROW and the second edit text name is COLUMN. As a user enters numbers in the both Edit Text let say ROW =2 and COLUMN =3 and press the button "Create text fields" it may create 6 Edit Text below the button of "Create text fields". These 6-edit text should be positioned in 2 rows and 3 columns like below

EditText-1 EditText-2 EditText-3

EditText-1 EditText-2 EditText-3

1
  • what you have tried, show us.. Commented Apr 13, 2012 at 6:22

4 Answers 4

4
setContentView(R.layout.main);
        LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
        Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth()/3;
        for(int i=0;i<2;i++){
            LinearLayout l = new LinearLayout(this);
            l.setOrientation(LinearLayout.HORIZONTAL);
            for(int j=0;j<3;j++){
                EditText et = new EditText(this);
                LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
                l.addView(et,lp);
          }
            ll.addView(l);
        }

use the above code in your onCreate() where "main" is xml file. and you can replace i<2 and j<3 with values of edittext 1 & 2.

sample main.xml which i am using is below::

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>


    <EditText
        android:id="@+id/et2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
Sign up to request clarification or add additional context in comments.

Comments

3
public class DynamicEditTextActivity extends Activity {

    private EditText row, column;
    private Button submit;
    private LinearLayout main, matrix;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        main = (LinearLayout) findViewById(R.id.mainlayout);
        row = (EditText) findViewById(R.id.row);
        column = (EditText) findViewById(R.id.column);      
        matrix = (LinearLayout) findViewById(R.id.matrix);
        matrix.setOrientation(LinearLayout.VERTICAL);

        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(generate);
    }

    OnClickListener generate = new OnClickListener() {

        public void onClick(View v) {

            matrix.removeAllViews();

            int rows = Integer.parseInt(row.getText().toString());
            int cols = Integer.parseInt(column.getText().toString());

            for (int i = 0; i < rows; i++) {

                LinearLayout layout = new LinearLayout(
                        DynamicEditTextActivity.this);
                layout.setOrientation(LinearLayout.HORIZONTAL);

                for (int j = 0; j < cols; j++) {

                    TextView text = new TextView(DynamicEditTextActivity.this);
                    text.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    text.setText((j+1) + "    ");
                    text.setTextColor(Color.RED);

                    layout.addView(text);
                }

                matrix.addView(layout);
            }
        }
    };

Comments

1

This is how you create an EditText box dynamically...

Hope this is useful...

This is just an example... You can reference this...

EditText t = new EditText(myContext);
    t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    layout.addView(t);

final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final EditText edit_text = new EditText(this);
edit_text.setLayoutParams(lparams);
edit_text.setText("New text: " + text);

Comments

0

try like this function

public static void display(final Activity activity, Button btn) {
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            LinearLayout scrollViewlinerLayout = activity.findViewById(R.id.linearLayoutForm);
            java.util.ArrayList<String> msg = new ArrayList<String>();

            for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
            {
                LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
                EditText edit = innerLayout.findViewById(R.id.editDescricao);

                msg.add(edit.getText().toString().trim());

                int childcount = scrollViewlinerLayout.getChildCount();
                for( i =0 ; i < childcount; i++){
                    View view = scrollViewlinerLayout.getChildAt(i);
                }
            }

            Toast t = Toast.makeText(activity.getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT);
            t.show();
        }
    }); }

2 Comments

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
can you share xml file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.