Reputation:
I want my text object to show string in TextView after the complete execution of for loop in startGame() function, but it is displaying the TextView as soon as it enters the startGame() function. I don't know may be this is out of order execution or what, but can anyone please help me?
Here is my code: ` import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random; import java.util.Timer; import java.util.TimerTask;
public class EasyGameActivity extends AppCompatActivity
{
private int counter;
private TextView text;
private boolean flag = false;
private Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_game);
startGame();
}
public void startGame()
{
counter = 3;
int temp;
int i;
int level = 1;
long nanoSec = 0;
final Random rand = new Random();
Timer timer = new Timer();
while(true)
{
BinaryTree binaryTree = new BinaryTree(counter);
nanoSec = 2000;
for(i = 0; i<counter; ++i)
{
temp = rand.nextInt(4);
switch(temp)
{
case 0:
button = (Button) findViewById(R.id.button5); break;
case 1:
button = (Button) findViewById(R.id.button6); break;
case 2:
button = (Button) findViewById(R.id.button7); break;
case 3:
button = (Button) findViewById(R.id.button8); break;
}
timer.schedule(new MyTask(button, temp), nanoSec);
timer.schedule(new MyTask(button, -1), nanoSec + 500);
nanoSec += 2000;
binaryTree.insert(temp);
}
text = (TextView) findViewById(R.id.levelText);
text.setText("Level: " + level);
break;
}
}
class MyTask extends TimerTask
{
private Button buttonId;
private int buttonNumb;
public MyTask(Button buttonId, int temp)
{
super();
this.buttonId = buttonId;
buttonNumb = temp;
}
@Override
public void run()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
if(buttonNumb == -1)
{
buttonId.setBackgroundColor(Color.BLACK);
}
else if(buttonNumb == 0)
{
buttonId.setBackgroundColor(Color.YELLOW);
}
else if(buttonNumb == 1)
{
buttonId.setBackgroundColor(Color.BLUE);
}
else if(buttonNumb == 2)
{
buttonId.setBackgroundColor(Color.RED);
}
else if(buttonNumb == 3)
{
buttonId.setBackgroundColor(Color.GREEN);
}
}
});
}
}
}
`
here is my xml File: `
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/levelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="50dp"
android:textColor="#FFFFFF"
android:text = "" />
<TextView
android:id="@+id/countDnText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="100dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text=""
/>
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button6"
android:layout_alignTop="@+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button7"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="@+id/button8"
android:layout_alignTop="@+id/button7"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
`
Upvotes: 1
Views: 263
Reputation: 801
Your code will not wait for the timer to complete before setting the text of the textview, so it is working something like this (arbitrary ms values to make my point):
You can simply set the edit text to happen on a timer like you have each task (@nanoSecond) and it will enqueue after the other tasks. Something like this should work (replacing your current TextView code).
final TextView textView = (TextView) findViewById(R.id.levelText);
Handler handler = new Handler();
handler.postDelayed(new Runnable{
@Override
public void run(){
textView.setText("Level: " + level);
}
}, nanoSec);
Upvotes: 3