James Robert Singleton James Robert Singleton - 3 months ago 35
Android Question

Set TextView text with a for loop so multiple lines show

So I have a for loop that I want to output a new line in a textview every single time it goes through. However, I am not sure if that is possible. I have TableLayout in there but it put the items vertically instead of horizontally. I thought about ListView but not sure how to set the text of it.

Here is my code

private void populateView() {
this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
drives = APIRequestsUtil.getRoutes();

// TableLayout tl;
// tl = (TableLayout) myView.findViewById(R.id.fragment1_tlayout);
driveNumber = (TextView) myView.findViewById(R.id.Drive_Number);
driveDistance = (TextView) myView.findViewById(R.id.Drive_Distance);
driveTime = (TextView) myView.findViewById(R.id.Drive_Time);

driveNumber.setText("Drive Number");
driveDistance.setText("Drive Distance");
driveTime.setText("Drive Time");
int driveNum = 0;
for (Map.Entry drive : drives.entrySet()) {
TableRow tr = new TableRow(getActivity());
Route route = (Route) drive.getValue();
tr.setId(driveNum++);
//tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

DateTime startTime = new DateTime(route.getStart_time());
DateTime endTime = new DateTime(route.getEnd_time());

driveNumList = (TextView) myView.findViewById(R.id.Drive_Number_List);
driveDistList = (TextView) myView.findViewById(R.id.Drive_Distance_List);
driveTimeList = (TextView) myView.findViewById(R.id.Drive_Time_List);

driveNumList.setText(String.valueOf(driveNum));
driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km");
driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s");
}
}
});
}


Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<!--Header aligned to top -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
android:gravity="center"
android:background="#FC9">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Number"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Distance"
android:layout_toRightOf="@+id/Drive_Number"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Time"
android:layout_toRightOf="@+id/Drive_Distance"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#005"
android:layout_below="@+id/header"
android:id="@+id/scrollableContents">

<!--<TableLayout-->
<!--android:id="@+id/fragment1_tlayout"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:stretchColumns="0,1">-->

<!--</TableLayout>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Number_List"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Distance_List"
android:layout_toRightOf="@+id/Drive_Number_List"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Time_List"
android:layout_toRightOf="@+id/Drive_Distance_List"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>

Answer

Add android:inputType="textMultiLine" to the TextView in the XML.

And add data into TextView like this:

driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator"));

And so on.

Edit: you can use "\n" instead of System.getProperty("line.separator") for adding the new line character.