-6

I am new to Android.

Please change the code accordingly.

Want to display the text messages dynamically, all of them are async calls, as i get response from the async call i need to display the text message on screen. They may not be in the same order as shown below.

Should not wait until all the Async call finishes and display all together using StringBuilder. This is not the solution i am looking for.

As and when i receive response i need to display the message line by line (with a small tick mark, if possible). Number of lines are not fixed, could be 20 or 50.

Thanks.

main.xml

    <RelativeLayout
        android:id="@+id/llMessages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:visibility="visible">

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="40dp"
            android:layout_marginTop="65dp"
            android:inputType="textMultiLine"
            android:singleLine="false"
            android:textIsSelectable="false"/>

    </RelativeLayout>

MainActivity.java

TextView msgs = findViewById(R.id.message);
msgs.setText("First\n"); 
msgs.setText("Second\n"); //<-- Text comes 3 seconds later - Async calls
msgs.setText("Third\n"); //<-- Text comes 3 seconds later - Async calls
msgs.setText("Fourth\n"); //<-- Text comes 3 seconds later - Async calls
msgs.setText("Fifth\n"); //<-- Text comes 3 seconds later - Async calls
Molay
  • 1,154
  • 2
  • 19
  • 42

2 Answers2

0

Use this method to append text append() whenever new text is available. put \n for new line

Mayank Panchal
  • 355
  • 1
  • 10
-1

use LinearLayout with vertical orientation instead of TextView in xml. Create TextView in Java.

each time you get the string from server create new TextView in java and add this into LinearLayout. Here is example code.

LinearLayout linearLayout =  (LinearLayout) findViewById(R.id.linear_layout_id);

TextView tv = new TextView(this);
tv.setText("FirstText");
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

linearLayout.addView(tv);
Prags
  • 2,457
  • 2
  • 21
  • 38
asim
  • 310
  • 1
  • 8