Lets say i have an array of char (mCharArray), and one textView (tvChar) that is the size of the whole screen. i want that, when the user clicks the screen (the tvChar), the textView shows each char inside the charArray, however it should have like 1 second delay in each iteration. How to accomplish this?
this is the current solution that i had. This code snippet is from the onViewCreated part of a fragment.the problem is, the text doesn't update, it only works for the last element of the char array.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvChar = (TextView) view.findViewById(R.id.tv_char);
tvChar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// not sure how to pause after each iteration...
v.postDelayed(new Runnable() {
@Override
public void run() {
for (char element : mCharArray) {
synchronized (tvChar) {
tvChar.setText(String.valueOf(element));
tvChar.notify();
try {
tvChar.wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}, 1000);
}
});
}
UPDATE: Thanks for all the answers, although none really works when i tried them, you guys help me give some intuition.
I end up adding thread handler on onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCharArray = getArguments().getCharArray(SENTENCE_KEY);
}
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
this.obtainMessage();
tvChar.setText(Character.toString((char)msg.arg1));
}
};
}
create a new thread in the setOnclickListener
tvChar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
thread = new Thread(new ChangeCharThread());
thread.start();
}
});
creating an inner class that implements runnable.
class ChangeCharThread implements Runnable{
@Override
public void run() {
Message msg = Message.obtain();
for (char ch : mCharArray){
msg.arg1 = ch;
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}