9

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);
  }

7 Answers 7

33

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());
Sign up to request clarification or add additional context in comments.

1 Comment

This seems like the best solution to me, StringBuilder is way better than String for appending text.
2
<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" />

Comments

0

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" />

Comments

0

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>

Comments

0

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);

Comments

0
@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);
}

2 Comments

Its crashing app can i know why??
Use StringBuilder instead of String to see the difference between please refer to this link.
0

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)
    }

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.