probability
probability

Reputation: 17

How to add multiple TextViews programmatically to a RelativeLayout

How does one programmatically, via one button click at a time, add multiple TextViews to an existing RelativeLayout without the TextViews overlapping onto one another.


I am trying something like this - The following code exists inside the onCreate() method:

TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1);

RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
                    (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

        TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this);
        newTextView.setText("text you want");
        rLayout1.addView(newTextView, lparams);

    }

The TextViews are being added to the RelativeLayout, but they are all on top of one another, how does one fix this?

Upvotes: 0

Views: 2985

Answers (3)

proy31
proy31

Reputation: 123

You can use Linear Layout with orientation as vertical instead of Relative Layout. It will align all the textviews vertically one below the other. I dont consider adding large number of textviews to the xml file a valid solution, as the number of times the user will click a button is unknown.

Upvotes: 0

probability
probability

Reputation: 17

The goal is to programmatically, via one button click at a time, add multiple TextViews to an existing RelativeLayout, and without the TextViews overlapping onto one another.

Here is what I finally came to, this works but I am unsure if it is the best way to go (or even a good way).


The following code exists inside the onCreate() method:

// Creates a variable to keep track of the amount of TextViews, the 
// first TextView, an array, and then stores the TextView in the Array 
int numberOfTextViews = 1;
TextView[] textViewArray = new TextView[20];
TextView textViewToSeeFirst = (TextView) findViewById(R.id.textView1);
textViewArray[numberOfTextViews - 1] = textViewToSeeFirst;

// Also need to reference the RelativeLayout we are putting TextViews in
RelativeLayout rLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams
                    (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
        lparams.addRule(RelativeLayout.RIGHT_OF, textViewArray[numberOfTextViews - 1].getId());
        lparams.addRule(RelativeLayout.ALIGN_TOP, textViewArray[numberOfTextViews - 1].getId());

        TextView newTextView = new TextView(TheActivityYouAreUsingActivity.this);
        newTextView.setText("text you want");
        newTextView.setId(numberOfTextViews);
        textViewArray[numberOfTextViews] = newTextView;
        rLayout1.addView(textViewArray[numberOfTextViews], lparams);
        numberOfTextViews = numberOfTextViews + 1;

    }

Some things to keep in mind:

  • the parameters for RelativeLayout.LayoutParams are important, see developer material on these parameters. WRAP_CONTENT was chosen for my needs because it causes the TextViews to only take up the size of their text, rather than their entire parent... Overlapping was occurring before I changed this.

  • the id of each new TextView must be set if it is to be referenced later on for new layout parameters

  • the id must be a positive number, and zero is not positive

  • the RelativeLayout is holding the TextViews and handling them, the textViewArray is just so the ids of each TextView can be stored and referenced if need be

The corresponding XML has this going for it inside the main parent:

     <RelativeLayout 
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight=".2" >

        <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                 
                android:text="@string/die_one" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name_a_button" />
    </RelativeLayout>

Notice that the first RelativeLayout, and the TextView both have an id, this is so the addRule() method in the activity can reference them.

This code allows a user to click a button and add new TextViews to a RelativeLayout without them overlapping.

Upvotes: 1

Abhinav Raja
Abhinav Raja

Reputation: 351

Why dont you add all your text views in your xml file (as much as you want) before running you app. Just set the visibilities to the textviews which you dont want to show initially to "GONE" and then in button click listener just keep changing the visibility of textview to "View.Visible" . If you want a new textview to appear each time you press a button then set a counter to a button and each time a counter increments you change the desire textview's visibility to View.Visible. If you understood what i am saying you will be able to make the code on your own.

Upvotes: 0

Related Questions