Wednesday, 21 August 2013

Can't get textview and \n to work

Can't get textview and \n to work

Posting this on behalf of a friend, I don't have the android SDK to test
this so apologies if it's super obvious:
Trying to update a TextView with a timestamp on a new line:
viewText = viewText + "\n"+ String.format("%d:%02d:%02d", minutes,
seconds,millis);
But no matter what seems to always end up on the same line.
Code below:
package com.example.polyrhythm;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView showResults, countDown;
long start, stop;
String sResults = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bStart = (Button) findViewById(R.id.startButton);
Button bStop = (Button) findViewById(R.id.rhythmOne);
showResults = (TextView) findViewById(R.id.resultTextView);
// countDown = (TextView) findViewById(R.id.countDownTextView);
showResults.setSingleLine(false);
bStart.setOnClickListener(this);
bStop.setOnClickListener(this);
start = 0;
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.startButton:
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
// countDown.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
// countDown.setVisibility(View.GONE);
start = System.currentTimeMillis();
}
}.start();
break;
case R.id.rhythmOne:
stop = System.currentTimeMillis();
if (start != 0) {
String viewText = "";
long result = stop - start;
int millis = (int) result;
int seconds = (int) result / 1000;
int minutes = seconds / 60;
millis = millis % 100;
seconds = seconds % 60;
viewText = viewText
+ "\n"
+ String.format("%d:%02d:%02d", minutes, seconds,
millis);
showResults.setText(viewText);
}
break;
}
}
}

No comments:

Post a Comment