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
You can use a
for
loop and createEditText
s in that loop and add them to aFlowLayout
container; Maybe this implementation orConstraintLayout's Flow
You may check this procedure , suppose 20 can be divided 4 columns with 5 rows . So run
for loop
on columns and rows to addeditText
view toTableRow
withTableLayout
.Example
main_activity.xml
where usedTableLayout
to add EditText .Now try the code –
Output-