Home > Mobile >  Does append create new textView?
Does append create new textView?

Time:08-13

MainActivity.java file

package com.example.sunshineexample_2;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    TextView toyListText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         toyListText = findViewById(R.id.toys_list);

         String[] toyNames = ToyBox.getToyNames();
         for(String Toyname : toyNames){
             toyListText.append(Toyname   "\n");
         }

    }
}

In for each loop, the line toyListText.append(Toyname "\n"); , does it create a new textview for every ToyName(There are about 30 toys in String[] toysName) or just extends the size of existing textview. In xml file I have created a framelayout container view and a single textView.

Xml file

<TextView
            android:id="@ id/toys_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="30dp"
            android:textSize="25sp" />

CodePudding user response:

No. It creates a new String, but not a new TextView. Nothing but the call to inflate creates a TextView. However, nothing in your code is updating the textview so you'll never see the changes- to see the text you'd need to call textView.setText(toyListText) after you're done generating it.

  • Related