nilesh wani
nilesh wani

Reputation: 1291

How to display multiline from array list in single TextView?

I am developing android apps so in that i want display the four lines of text in single text view. my Array list contain data like following

Customer Name : Nilesh
Customer Contact : 23230200
Customer City : Pune

but when I write code like following only last line was displayed from the array List

XML

  <TextView android:id="@+id/kbpViewTvCustNm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:lines="4"
            android:textIsSelectable="false" />

code

for (String details : list2)
  {
     custName.setText(details);
  }

Upvotes: 9

Views: 22391

Answers (7)

haresh
haresh

Reputation: 1420

Simple solution for your entire pojo fields:

for (Post post : posts) {
 String content = "";
 content += "ID: " + post.getId() + "\n";
 content += "User ID: " + post.getUserId() + "\n";
 content += "Title: " + post.getTitle() + "\n";
 content += "Text: " + post.getText() + "\n\n";

 textViewResult.append(content);         
  }
 }

For kotlin :

   posts.forEach {post->
        var content = ""
        content += "ID: " +  post.id + "\n"
        content += "User ID: " + post.userId + "\n"
        content += "Title: " + post.title + "\n"
        content += "Text: " + post.text + "\n\n"
      textViewResult.append(content)
    }

Upvotes: 0

Simant Shrestha
Simant Shrestha

Reputation: 17

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    Bundle bundle = getIntent().getExtras();
    assert bundle != null;
    ArrayList<String> expTit = bundle.getStringArrayList("expTit");
    ArrayList<Integer> expAmt =bundle.getIntegerArrayList("expAmt");
    TextView t= findViewById(R.id.textView2);
    TextView t2= findViewById(R.id.textView3);
    StringBuilder builder = new StringBuilder();
    if (expTit == null) throw new AssertionError();
    String text="";
    for (String details : expTit) {
        text = text + details + "\n";
    }
    t.setText(text);
}

Upvotes: 0

Ostkontentitan
Ostkontentitan

Reputation: 7010

Use a StringBuilder to make your array 1 String and append linebreaks in between the individual Strings like this:

StringBuilder builder = new StringBuilder();
for (String details : list2) {
   builder.append(details + "\n");
}

custName.setText(builder.toString());

Upvotes: 33

Neoh
Neoh

Reputation: 16164

You can combine your details as a single string and separate with a newline character:

String text="";
for (String details : list2) {
    text = text + details + "\n";
}
custName.setText(details);

Upvotes: 0

androidgeek
androidgeek

Reputation: 3480

Hi you can set the Max lines in Text View

  <TextView
android:id="@+id/address1"
android:gravity="left"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:maxLines="4"
android:lines="4"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>

Upvotes: 0

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

Change your layout_width from wrap_content to particular size like below:

<TextView
            android:id="@+id/kbpViewTvCustNm"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:lines="4"
            android:textIsSelectable="false" />

Upvotes: 0

Samadhan Medge
Samadhan Medge

Reputation: 2049

<TextView
            android:id="@+id/kbpViewTvCustNm"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:lines="4"
            android:textIsSelectable="false" />

Upvotes: 2

Related Questions