0

I want to add text view dynamically when clicking a button . I have implemented this but when adding it over writing the previous one.Anything wrong on my code ?

imGbtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                int id =0;
                final String data   =   textView.getText().toString();
                //childHolder.title.setText(data);
                 LinearLayout ll = new LinearLayout(mContext);
                 ll.setOrientation(LinearLayout.VERTICAL);
                 ll.setId(id);
                 tvll.addView(ll);
                // TextView tv1 = new TextView(mContext);
                // tv1.setText("Dynamic layouts ftw!");
                // ll.addView(tv1);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(25, 20, 25, 10);
                TextView tv= new TextView(mContext);
                tv.setId(id);
                tv.setText(data);
                ll.addView(tv,layoutParams);
                textView.setText("");

            }
        });

2 Answers 2

3

following code add Edittext in linearlayout onclick

public class ViewOnClick extends Activity {
    LinearLayout.LayoutParams layoutParams;
    LinearLayout ll;
    static int i;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.Button01);

        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {


     LinearLayout ll = new LinearLayout(mContext);
                 ll.setOrientation(LinearLayout.VERTICAL);
                 ll.setId(id);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(25, 20, 25, 10);
                EditText view = new EditText(ViewOnClick.this);             
                view.setText(++i+" view");
                ll.addView(view, layoutParams); 

            }});
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest define LinearLayout inside layout xml.Create object in Java and then add textview on button click in the LinearLayout.

 <LinearLayout
            android:id="@+id/relatedChannels"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>

Java

 LinearLayout cat_linear=(LinearLayout) findViewById(R.id.list_Category);
    TextView tv = new TextView(context);
    tv.setText(category.get(i).getNAME());
    tv[i][i].setLayoutParams(new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    cat_linear.addView(tv);

Comments

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.