"goClicked" function is a onClick function to the button "Go" but it is not executing when I click on the button "Go"( I am able to say this because the toast is not appearing ) until I comment the while loop in "goClicked" function. I am pasting the code for only 2 functions "goClicked" and "countdown" because they are the only functions that alter the variable "counterRunning".
public void goClicked(View view) {
afterGoPressed();
countDown();
correctCount = 0;
totalCount = 0;
TextView time = (TextView) findViewById(R.id.time);
while (counterRunning) {
int sum = generateQuestion();
pickOption = generateOptions(sum);
}
}
public void countDown() {
counterRunning = true;
final TextView time = (TextView) findViewById(R.id.time);
final Button tryAgain = (Button) findViewById(R.id.tryAgain);
final TextView result = (TextView) findViewById(R.id.result);
final TextView score = (TextView) findViewById(R.id.score);
int secondsLeft = 30;
time.setText(secondsLeft+"");
CountDownTimer countDownTimer = new CountDownTimer(30000,1000) {
#Override
public void onTick(long millisUntilFinished) {
time.setText(millisUntilFinished/1000 + "");
}
#Override
public void onFinish() {
tryAgain.setVisibility(View.VISIBLE);
result.setText("Your score: " + score.getText());
result.setVisibility(View.VISIBLE);
counterRunning = false;
}
}.start();
}
It is because you're using an infinite loop with the following code:
while (counterRunning) {
int sum = generateQuestion();
pickOption = generateOptions(sum);
}
the code will blocking all other instruction until it found the counterRunning is changed to false inside the while block. But it never happened. Hence the infinite loop.
Related
I'm working on a tank-game and I have a TextView which represents the shot. Now I want to display the TextView at the specific point and remove it after a second that it looks like the shot goes further step by step. But when I add a countdown or a Thread.sleep the program stops for a second but the TextView doesn't disappear. i want to move the TextView over the screen and after every iteration of my for loop i want to wait a second and then rearrange it again?
Here is the code :
public void shot(float power, float winkel, Button button) {
if(winkel>90) {
winkel = winkel - 10;
}else if(winkel<90){
winkel = winkel +10;
}
for (double i = 0; i<100;i = i+ 1) {
final TextView textView = new TextView(context);
textView.setText(".");
double x = tanks.get(currentTank).getxPos()+(i*power*Math.cos(winkel *(Math.PI/180)));
double y = tanks.get(currentTank).getyPos()+(-1*(i*power*Math.sin(winkel *(Math.PI/180))));
double gravity = (-1*((9.81/2)*Math.pow(i,2)));
y = (y-gravity);
textView.setX((float) x);
textView.setY((float) y);
layout.addView(textView);
for (int j = 0;j<tanks.size();j++){
if(textView.getX()>tanks.get(j).getxPos()&&textView.getX()<tanks.get(j).getxPos()+100){
if(textView.getY()>tanks.get(j).getyPos()&&textView.getY()<tanks.get(j).getyPos()+100){
float k = tanks.get(j).getxPos()-textView.getX();
if(k<0){
k = k*-1;
}
makeDamage(k,tanks.get(j));
}
}
}
new CountDownTimer(2000,1000){
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
layout.removeView(textView);
}
}.start();
}
newTurn();
}
I want to pause the program after adding the TextView for one second and the remove it. The program stops but the TextView doesn't disappear till the for-loop finished. Then all TextViews disappear.
Problem solved:
i've added all positions in a array and then this
public void drawShot(final Button firework, final ArrayList<TextView> toDraw){
final int[] i = {0};
final Handler mHandler = new Handler();
firework.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View v) {
firework(firework,toDraw.get(i[0]).getX(),toDraw.get(i[0]).getY());
}
});
Runnable runnable = new Runnable() {
#Override
public void run() {
layout.addView(toDraw.get(i[0]));
if(!check(toDraw.get(i[0]))) {
mHandler.postDelayed(this, (long) 1);
}
i[0]++;
}
};
// start it with:
mHandler.post(runnable);
}
probably need to run the remove command on main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
#Override
public void run() {
layout.removeView(textView);
}
};
mainHandler.post(myRunnable);
The issue I'm having is that my score counter variable 'score' cannot go higher than 1 in my program. If the user inputs a value equal to the random number displayed (LoadG1), they are awarded a point. This is then outputted at the end, as shown in the longer timer. In the shorter timer, the point is added. The int variable is declared at the beginning. All is shown below. Now I'm aware that it's final, and that this is most likely the cause of my issue, but the IDE requires it to be final otherwise I can only call the variable 'score' in one method (CountDown Timer in my case). This is causing me problems. I intend to have a point added each time the 4 second timer repeats if the user has the correct input each time, though it can't go more than 1 at the moment. I would like the final score to be outputted at the end, as shown below.
The code:
final int[] score = {0};
final Random generateG1 = new Random();
final int loadG1 = generateG1.nextInt(1000000)+10000;
final TextView number = (TextView) findViewById(R.id.number);
number.setText(" "+loadG1);
final CountDownTimer loop = new CountDownTimer(4000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.VISIBLE);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(loadG1));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score[0]++;
}
number.setVisibility(View.VISIBLE);
final int loadG1 = generateG1.nextInt(1000000) + 10000;
number.setText(" " + loadG1);
input.getText().clear();
start();
return true;
default:
}
}
return false;
}
});
}
}.start();
new CountDownTimer(24000, 1000) {
#Override
public void onTick (long millisUntilFinished) {
}
#Override
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score[0]);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.GONE);
loop.cancel();
}
}.start();
I'd greatly appreciate it if someone can provide me with a fix to my issue, thanks in advance.
Because you are not reading the random value that you have generated, just the first one. So the first time the answer is correct, but next time you answer different, but the if compares to the firstly generated random number, so it is not counting as valid answer (if you enter same first number all times it will count). You need to read the number from the TextEdit number every time since there is the current number.
So it could be:
final int[] score = {0};
final Random generateG1 = new Random();
final int loadG1 = generateG1.nextInt(1000000)+10000;
final TextView number = (TextView) findViewById(R.id.number);
number.setText(" "+loadG1);
final CountDownTimer loop = new CountDownTimer(4000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.VISIBLE);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
// here we get from text field the current correct value
int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score[0]++;
}
number.setVisibility(View.VISIBLE);
final int loadG1 = generateG1.nextInt(1000000) + 10000;
number.setText(" " + loadG1);
input.getText().clear();
start();
return true;
default:
}
}
return false;
}
});
}
}.start();
new CountDownTimer(24000, 1000) {
#Override
public void onTick (long millisUntilFinished) {
}
#Override
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score[0]);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.GONE);
loop.cancel();
}
}.start();
Also, you could (and should) use AtomicInteger instead of int[], the methods you'll be interested are:
int AtomicInteger#get();
int AtomicInteger#incrementAndGet()
So declare the score as AtomicInteger like:
final AtomicInteger score = new AtomicInteger();
then instead of score[0]++; do score.incrementAndGet();
then when you read the results do: score.get();
Did you write final int[] score = {0}; in the onCreate?
Try to write int[] score = {0}; out of onCreate, as a global variable.
It was working fine when it was just one timer but when I added inserted a switch and added two more timers, the stop timer button seized working. All the code is pasted below:
public class timer extends Activity implements OnClickListener {
private Button buttonStartTime, buttonStopTime, buttonStartTime2, buttonStopTime2, buttonStartTime3, buttonStopTime3;
private EditText edtTimerValue, edtTimerValue2, edtTimerValue3;
private TextView textViewShowTime, textViewShowTime2, textViewShowTime3; // will show the time
private CountDownTimer countDownTimer, countDownTimer2, countDownTimer3; // built in android class
// CountDownTimer
private long totalTimeCountInMilliseconds, totalTimeCountInMilliseconds2, totalTimeCountInMilliseconds3; // total count down time in
// milliseconds
private long timeBlinkInMilliseconds, timeBlinkInMilliseconds2, timeBlinkInMilliseconds3; // start time of start blinking
private boolean blink, blink2, blink3; // controls the blinking .. on and off
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
buttonStartTime = (Button) findViewById(R.id.btnStartTime);
buttonStopTime = (Button) findViewById(R.id.btnStopTime);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);
buttonStartTime2 = (Button) findViewById(R.id.btnStartTime2);
buttonStopTime2 = (Button) findViewById(R.id.btnStopTime2);
textViewShowTime2 = (TextView) findViewById(R.id.tvTimeCount2);
edtTimerValue2 = (EditText) findViewById(R.id.edtTimerValue2);
buttonStartTime3 = (Button) findViewById(R.id.btnStartTime3);
buttonStopTime3 = (Button) findViewById(R.id.btnStopTime3);
textViewShowTime3 = (TextView) findViewById(R.id.tvTimeCount3);
edtTimerValue3 = (EditText) findViewById(R.id.edtTimerValue3);
buttonStartTime.setOnClickListener(this);
buttonStopTime.setOnClickListener(this);
buttonStartTime2.setOnClickListener(this);
buttonStopTime2.setOnClickListener(this);
buttonStartTime3.setOnClickListener(this);
buttonStopTime3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartTime:
if (v.getId() == R.id.btnStartTime) {
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer();
buttonStopTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.GONE);
edtTimerValue.setText("");
startTimer();
} else if (v.getId() == R.id.btnStopTime) {
countDownTimer.cancel();
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
break;
case R.id.btnStartTime2:
if (v.getId() == R.id.btnStartTime2) {
textViewShowTime2.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer2();
buttonStopTime2.setVisibility(View.VISIBLE);
buttonStartTime2.setVisibility(View.GONE);
edtTimerValue2.setVisibility(View.GONE);
edtTimerValue2.setText("");
startTimer2();
} else if (v.getId() == R.id.btnStopTime2) {
countDownTimer2.cancel();
buttonStartTime2.setVisibility(View.VISIBLE);
buttonStopTime2.setVisibility(View.GONE);
edtTimerValue2.setVisibility(View.VISIBLE);
}
break;
case R.id.btnStartTime3:
if (v.getId() == R.id.btnStartTime3) {
textViewShowTime3.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer3();
buttonStopTime3.setVisibility(View.VISIBLE);
buttonStartTime3.setVisibility(View.GONE);
edtTimerValue3.setVisibility(View.GONE);
edtTimerValue3.setText("");
startTimer3();
} else if (v.getId() == R.id.btnStopTime) {
countDownTimer3.cancel();
buttonStartTime3.setVisibility(View.VISIBLE);
buttonStopTime3.setVisibility(View.GONE);
edtTimerValue3.setVisibility(View.VISIBLE);
}
break;
}
}
private void setTimer() {
int time = 0;
if (!edtTimerValue.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue.getText().toString());
} else
Toast.makeText(timer.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds = 60 * time * 1000;
timeBlinkInMilliseconds = 30 * 1000;
}
private void startTimer() {
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink) {
textViewShowTime.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime.setVisibility(View.INVISIBLE);
}
blink = !blink; // toggle the value of blink
}
textViewShowTime.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
#Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime.setText("Time up!");
textViewShowTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
}.start();
}
private void setTimer2() {
int time = 0;
if (!edtTimerValue2.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue2.getText().toString());
} else
Toast.makeText(timer.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds2 = 60 * time * 1000;
timeBlinkInMilliseconds2 = 30 * 1000;
}
private void startTimer2() {
countDownTimer2 = new CountDownTimer(totalTimeCountInMilliseconds2, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
if (leftTimeInMilliseconds < timeBlinkInMilliseconds2) {
textViewShowTime2.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink2) {
textViewShowTime2.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime2.setVisibility(View.INVISIBLE);
}
blink2 = !blink; // toggle the value of blink
}
textViewShowTime2.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
#Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime2.setText("Time up!");
textViewShowTime2.setVisibility(View.VISIBLE);
buttonStartTime2.setVisibility(View.VISIBLE);
buttonStopTime2.setVisibility(View.GONE);
edtTimerValue2.setVisibility(View.VISIBLE);
}
}.start();
}
private void setTimer3() {
int time = 0;
if (!edtTimerValue3.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue3.getText().toString());
} else
Toast.makeText(timer.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds3 = 60 * time * 1000;
timeBlinkInMilliseconds3 = 30 * 1000;
}
private void startTimer3() {
countDownTimer3 = new CountDownTimer(totalTimeCountInMilliseconds3, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds3) {
long seconds = leftTimeInMilliseconds3 / 1000;
if (leftTimeInMilliseconds3 < timeBlinkInMilliseconds3) {
textViewShowTime3.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink3) {
textViewShowTime3.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime3.setVisibility(View.INVISIBLE);
}
blink3 = !blink; // toggle the value of blink
}
textViewShowTime3.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
#Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime3.setText("Time up!");
textViewShowTime3.setVisibility(View.VISIBLE);
buttonStartTime3.setVisibility(View.VISIBLE);
buttonStopTime3.setVisibility(View.GONE);
edtTimerValue3.setVisibility(View.VISIBLE);
}
}.start();
}
}
You need to switch on stopping buttons as well.
switch (v.getId()) {
case R.id.btnStartTime:
means v.getId() == R.id.btnStartTime inside this case so checking the value of v.getId() there is useless. The first if (v.getId() == R.id.btnStartTime) will always be true, the second one (v.getId() == R.id.btnStopTime) always false.
I got a problem with CountDownTimer's StartButton: the timer doesn't start after pressing the button. How do I fix that?
I want to start the timer by pressing button buttonCount.
Can someone help me please?
int clicks = 0;
TextView textCount;
ImageButton buttonCount;
int guessCount =0;
boolean started = false;
boolean timerProcessing = false;
private CountDownTimer count;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newgame);
count = new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("Time's Up!");
buttonCount.setEnabled(false);
if (clicks > oldscore)
getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
}
};
final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
final TextView textView = (TextView) findViewById(R.id.applesEaten);
buttonCount = (ImageButton) findViewById(R.id.button);
buttonCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
clicks++;
textView.setText("Clicks: " + clicks);
TextView textView = (TextView) findViewById(R.id.topScoreView);
textView.setText("Best: " + oldscore);
if(!started){
count.start();
started = true;
timerProcessing = true;
}
}
});
final TextView textic = (TextView) findViewById(R.id.textView2);
}
It seems to me this is what you really want to do:
private int clicks = 0;
private TextView textCount;
private ImageButton buttonCount;
private int guessCount = 0;
private CountDownTimer count; // RENAMED
private boolean started = false; // FALSE.
private boolean timerProcessing = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newgame);
count = new CountDownTimer(15000, 1000) { // MOVED UP
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("Time's Up!");
buttonCount.setEnabled(false);
if (clicks > oldscore)
getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
}
};
final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
final TextView textView = (TextView) findViewById(R.id.applesEaten);
buttonCount = (ImageButton) findViewById(R.id.button);
buttonCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
clicks++;
textView.setText("Clicks: " + clicks);
TextView textView = (TextView) findViewById(R.id.topScoreView);
textView.setText("Best: " + oldscore);
if(!started){
count.start(); // START COUNTDOWN TIMER
started = true;
timerProcessing = true;
}
}
});
final TextView textic = (TextView) findViewById(R.id.textView2);
}
And if you really want to start another CountDownTimer than the one you create at the bottom (named count). Then you need to instantiate it and set its behaviour, just like you do for the other CountownTimer.
Also, all the variables you use need to be created before (textic, oldscore)
i'm new to android programming. I have the following code happening on a button click
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.morse_btn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loopCode();
}
});
}
which calls this:
public void loopCode()
{
String code = "Hello There";
TextView view = (TextView) findViewById(R.id.code_txt);
String s = "";
for(int i = 0; i < code.length(); i++)
{
s+=code.charAt(i);
view.setText(s);
try {
TimeUnit.SECONDS.sleep(1);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
but when i run it on my phone, the text does not get appended until after the for loop has gone through, i.e i press the button, and after a few seconds, the whole string "Hello There" appears.
How can I make it write the text one character at a time, like a typewriter style.
Thanks
You need to use view.append("") which will append new text to the existing one.
Try this code:
int i = 0; //declare this globally
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != 10) {
text.append(" " + i);
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
This code will append a new number to the TextView every one second until it has reached the count 10. You can apply the same logic.
I had provided this solution to a question here -
[EDIT]
Try this:
String code = "Hello There"; //declare variable globally
int i = 0; //declare globally
TextView view; //declare globally
public void loopCode()
{
view = (TextView) findViewById(R.id.code_txt);
//String s = "";
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i != code.length()) {
view.append(" " + code.charAt(i));
i++;
handler.postDelayed(this, 1000);
}
}
}, 1000);
}
}
Don't forget to declare int i = 0 and String code = "Hello There" globally.
Exist 2 different method setText in TextView.
public final void setText (int resid)
public final void setText (CharSequence text)
when you put variable int in setText, the android try find String in classe R variable in same code.
To resolve this you then cast int to string using String.valueOF(...)
see more in;
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf(int)
try
public void loopCode()
{
String code = "Hello There";
TextView view = (TextView) findViewById(R.id.code_txt);
String s = "";
for(int i = 0; i < code.length(); i++)
{
view.setText(String.valueOf(i));
try {
TimeUnit.SECONDS.sleep(1);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}