skip to Main Content

I want create N edit text with pressing a button only once. for example I enter quantity of edit texts=20[enter image description here][1], when I press button all of 20 edit texts created in several rows and columns.

Any solutions??

sample picture for better solution [1]: https://i.stack.imgur.com/wevZN.png

my code is in below

 String Type,number,tedad;
TextView pazireshnum;
EditText tedadbattri;
String id=ID;
private LinearLayout mLayout;
private EditText mEditText;
private ImageButton mButton;
int k = -1;
int flag,i;
int hint=1;
ArrayList<String> applnserverinstnos = new ArrayList<String>();
public static EditText textView[] = new EditText[100];
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_list);

    mLayout = (LinearLayout) findViewById(R.id.ln1);
   // mEditText = (EditText) findViewById(R.id.editText);
    mButton = (ImageButton) findViewById(R.id.imgbtn);
    tedadbattri=(EditText) findViewById(R.id.ed7);
    tedadbattri.setText("0");
    pazireshnum=(TextView)findViewById(R.id.tv4);
    tedad= tedadbattri.getText().toString();
    int Tedad = Integer.parseInt(tedad);
    mButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

                try {

                        k++;
                        flag = k;
                        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lparams.setMargins(5, 10, 5, 10);
                        textView[flag] = new EditText(CheckList.this);
                        textView[flag].setLayoutParams(lparams);
                        textView[flag].setId(flag);
                        textView[flag].setPadding(5, 5, 5, 5);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                    mLayout.addView(textView[flag]);
                    textView[flag].setHint("باتری"+hint);
                    hint++;
                    textView[flag].setId(hint);
                    textView[flag].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    textView[flag].setBackgroundResource(R.drawable.border4);
            }
    });

2

Answers


  1. You can use a for loop and create EditTexts in that loop and add them to a FlowLayout container; Maybe this implementation or ConstraintLayout's Flow

    Login or Signup to reply.
  2. You may check this procedure , suppose 20 can be divided 4 columns with 5 rows . So run for loop on columns and rows to add editText view to TableRow with TableLayout .

    Example main_activity.xml where used TableLayout to add EditText .

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
    
        <ImageButton
            android:id="@+id/imgbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            tools:layout_editor_absoluteX="137dp"
            tools:layout_editor_absoluteY="283dp" />
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/table">
        </TableLayout>
    </LinearLayout>
    

    Now try the code –

       mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                int total = 20;  // quantity of edit texts 20
                final int columns = 4;
                final int rows = total / columns;  // in this case rows would be 5
    
    
                TableLayout myTable = findViewById(R.id.table);
    
                //Params for TableRow and TableLayout
                final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
                final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
               //apply to clear
                myTable.removeAllViews();
                for (int i = 0; i < rows; i++) {
                    TableRow tableRows = new TableRow(getApplicationContext());
    
    
                    for (int j = 0; j < columns; j++) {
                        EditText editText = new EditText(getApplicationContext());
                        editText.setText("voltage" + total--);
                        tableRows.addView(editText, rowParams);
                    }
                    myTable.addView(tableRows, tableParams);
    
                }
            }
        });
    

    Output-
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search