0

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?

2
  • Stack Overflow is for programming questions. What is your question? If you are attempting to write a FAQ-style entry, everything in your "question" should be in an answer, and the question should be an actual Stack Overflow-appropriate question, explaining the problem. Commented Jun 23, 2014 at 22:36
  • btw - CommonsWare's comment is valid. My original post didn't ask the question directly enough, I have since edited the post... Commented Jun 28, 2014 at 19:45

3 Answers 3

1

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.

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

Comments

0

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.

4 Comments

I like this method, but was concerned that it might slow the app down if there were too many textViews in the activity. Would it? Also, I don't know how many textViews the user will make.
well.. one thing is for sure. it wont slow the down the app. But when the number of textviews is unknown, then it is a problem (for me as i am also new). Sorry couldn't help you bro !
Your answer did help (I can't up vote because I am new), thanks.
you can accept the answer though. more you accept more are the chances of people answering your question next time.
0

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.

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.