Open top menu


Example:- Create Multiple EditText And Get editText String programmatically.


In this tutorial explain how to implement EditText in loop and Set Empty Validation, get EditText String programmatical.



public class ServiceTypeDetailActivity extends Activity implements View.OnClickListener{

private List editTexts = new ArrayList();
ArrayList enterItemArray = new ArrayList<>();

public static ArrayList ServiceEditParameters= ArrayList();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_servicetypedetail);

LinearLayout Create_ServiceDeatils_layout = 
(LinearLayout) findViewById(R.id.Create_ServiceDeatils_layout);


lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
ViewGroup.LayoutParams.WRAP_CONTENT);
lparams.setMargins(5, 7, 5, 7);

for (int ed_txt = 0; ed_txt < ServiceTypeActivity.ServiceParameters.size(); ed_txt++) {
    ServiceParametersDetail item = ServiceTypeActivity.ServiceParameters.get(ed_txt);
    TextInputLayout inputLayout = new TextInputLayout(this);
    EditText ed = new EditText(this);
    ed.setId(ed_txt);
    ed.setHint(item.getPName());
    ed.setLayoutParams(lparams);
    ed.setPadding(7, 0, 2, 0);
    if (item.getPDataType().equalsIgnoreCase("String")) {
        ed.setInputType(InputType.TYPE_CLASS_TEXT);
    } else {
        ed.setInputType(InputType.TYPE_CLASS_NUMBER);
    }
    inputLayout.addView(ed);
    editTexts.add(ed);
    Create_ServiceDeatils_layout.addView(inputLayout);
}
}
@Overridepublic void onClick(View v) {
    switch (v.getId()) {
       
        case R.id.btn_calculate:
            try {
                enterItemArray.clear();
                String[] items = new String[editTexts.size()];
                for (int get = 0; get < editTexts.size(); get++) {
                    if (editTexts.get(get).getText().toString().equals("")) {
                        ServiceParametersDetail item = ServiceTypeActivity
.ServiceParameters.get(get);
                        AlertDialog("PLease Enter Item .");
                        enterItemArray.clear();
                        break;
                    }
                    enterItemArray.add(editTexts.get(get).getText().toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
}






Tagged

0 comments