0

How to add EditText programmatically?

Here's what I wrote:

View RV = inflater.inflate(R.layout.fragment__dummy,container, false);

LinearLayout LL = new LinearLayout(getActivity());
EditText ET = new EditText(getActivity());
ET.setId(5);
ET.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LL.addView(ET);

return RV;

Thank You!

1
  • can you post your logcat error Commented Jun 28, 2014 at 11:41

2 Answers 2

2
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);
Sign up to request clarification or add additional context in comments.

Comments

0

You are creating the LinearLayout LL dynamically too.

You need to add it to RV like

((ViewGroup)RV).addView(LL);

Also you should set the LayoutParams for LL too

2 Comments

Something like? View RV = inflater.inflate(R.layout.fragment__dummy,container, false); LinearLayout LL = (LinearLayout) RV .findViewById(R.id.LL1); LinearLayout(getActivity()); LL.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); EditText ET = new EditText(getActivity()); ET.setId(5); ET.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ((ViewGroup)RV).addView(LL); return RV;
Like ((ViewGroup)RV).addView(LL);

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.