1

Description::

  • On click of button I want to add a textview dynamically on run-time
  • I don't want to use a textview widget(from xml)
  • I want to generate textviews programatically
  • Is it possible ?
  • If so how ?

MainActivity.java

public class MainActivity extends Activity {

    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Container" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:text="Button" />

</RelativeLayout>

5 Answers 5

1

Just

 button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               LinearLayout layout = (LinearLayout) findViewById(R.id.parent_layout);
               TextView textView = new TextView(MainActivity.this);
               layout .addView(textView);
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

1

You can create a textView using the context of your activity.

example:

RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);


button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
TextView tv = new TextView(MainActivity.this);
               relativeLayout.addView(tv,lp);
            }
        });

Comments

1

Simple example, including layout params (width, height, etc):

    // instantiate the new view - int his example, a textview
    TextView label=new TextView(context);
    label.setText(labelText);

    // new LayoutParams, specify the height as WRAP_CONTENT
    // width is 0 in this example as we are using the layout weight (2)
    LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
            0, LayoutParams.WRAP_CONTENT,2
    );

    // margins
    lp.rightMargin=5; 
    lp.leftMargin=5;

    // apply the layout params to this view
    label.setLayoutParams(lp);

    // finally, add the view to the container view
    content.addView(label);

Comments

1

Something like this:

// Create the TextView
TextView textView = new TextView(this);

// Create some parameters
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                                           LayoutParams.WRAP_CONTENT, 
                                           LayoutParams.WRAP_CONTENT);

p.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

// Get your relative layout by its id
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);

// Add the newly created TextView to the layout
relativeLayout.addView(textView, p);

Comments

1

TextView in a LinearLayout:

LinearLayout layout = new LinearLayout(this);
TextView textview = new TextView(this);
textview.setText("Hellow, I'm a TextView!");
layout.addView(textview);
this.setContentView(layout);

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.