Generating Edit Text Programmatically in Android

Generating Edit Text Programmatically in Android

You can create it like so:

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);

This can be implemented anywhere on the UI thread; a click listener, an onCreate method, and everything in between.

There is a more generic example in this question, and a good rundown of these processes in this blog.

Android TextView and EditText programmatically

LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root_layout);
rootLayout.setOrientation(LinearLayout.VERTICAL);
//this layout still needs to be vertical to hold the children.
for (int i = 0; i < 6; i++) {

//make a new horizontal LinearLayout each time to hold the children.
LinearLayout temp = new LinearLayout(this);
temp.setOrientation(LinearLayout.HORIZONTAL);
temp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));

TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
textView.setText("Text");
temp.addView(textView); //add them to this temporary layout.

EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
temp.addView (editText);

TextView addTextView = new TextView(this);
addTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
addTextView.setText("Additional Text");
temp.addView(addTextViewtextView);

rootLayout.addView(temp);

In this way, you can add several linear layouts inside one. So basically, for each set of TextViews, you are making a separate LinearLayout, and then adding each of these layouts to your main LinearLayout which is still vertical in orientation.

How to add EditText programmatically?

LinearLayout layout=(LinearLayout)view.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("edittext");
edttext.setLayoutParams(params);

layout.addView(edttext);

Getting the value of Edittext which created programmatically

I guess you want to get the values of the dynamically created EditTexts. You currently replace the instance of the EditText which each creation. Try the below code:

        List<EditText> editTextList = new ArrayList<>();

private void setChoices() {

layout = (LinearLayout) findViewById(R.id.linearLayout);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
int marginPixel = 1;
int marginPixell = 5;
float densityy =getResources().getDisplayMetrics().density;
int marginDp = (int)(marginPixel * densityy);
int margin = (int)(marginPixell * densityy);
params.setMargins(marginDp, margin, marginDp, margin);

int num= Integer.parseInt(choicesNumberEDT.getText().toString());

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

EditText explanationED= new EditText(this);
explanationED.setLayoutParams(params);
editTextList.add(explanationED); //<-------new line here
layout.addView(explanationED);
explanationED.setGravity(Gravity.TOP);
explanationED.setHint("أكتب هنا.....");
explanationED.setTextSize(14);
explanationED.setId(i);
float density = getResources().getDisplayMetrics().density;
int paddingDp = (int) (Const.paddingPixel * density);
explanationED.setBackgroundColor(Color.parseColor("#B2E3CC"));
explanationED.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);

}

}

private EditText getText(int position) {
return editTextList.get(position).getText().toString();
}

OR if you don't want to keep a list of the EditTexts, you can set a tag for each EditText you create using:

    editText.setTag("tag");

and later retrieve it using:

    layout.findViewWithTag("tag");

Android: Dynamically or Programmatically add two EditText in one line and make them relate

Use a horizontal linear layout and add two edit texts inside.

Try this :

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);

Using layout weight as 1 for both edit texts(1.0f is the weight). You can tweak the width and height of the edit texts.

EditText et1 = new EditText(this);
et1.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1.0f));

EditText et2 = new EditText(this);
et2.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1.0f));

Now add edit texts to the horizontal linearLayout and then add it to your layout.

linearLayout.addView(et1);
linearLayout.addView(et2);
layout.addView(linearLayout);


Related Topics



Leave a reply



Submit