Dynamically update android ui for an animation - android

I have implemented a layout update animation by referring this article. It is working fine when I click a button ui gets updated. Now I want to automate this process. That means automatically update the ui. I have used handler within a for loop to update the ui periodically like follows
for(int i = 0; i < 100; i++){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
final ViewGroup newView = (ViewGroup) LayoutInflater.from(getApplicationContext()).inflate(
R.layout.list_item, containerGroup, false);
TextView tv = (TextView) newView.findViewById(android.R.id.text1);
tv.setText(RAVEMSGS[(int) (Math.random()*RAVEMSGS.length)]);
containerGroup.addView(newView,0);
}
},2000);
}
but this is not working. How can I update the ui dynamically for the animation ?
Note : I posted only a part of the code which I have used to update the list view this piece of code is working if I call it withing a onclick listner of a button.
Thanks.

You can try to update Ui from runOnUiThread........
try {
// code runs in a thread
runOnUiThread(new Runnable() {
#Override
public void run() {
// your code for animation
}
});
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}

did you write this code in a thread? try this:
// main thread
final Handler handler = new Handler();
new Thread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 100; i++){
handler.post(new Runnable() {
#Override
public void run() {
final ViewGroup newView = (ViewGroup) LayoutInflater.from(getApplicationContext()).inflate(
R.layout.list_item, containerGroup, false);
TextView tv = (TextView) newView.findViewById(android.R.id.text1);
tv.setText(RAVEMSGS[(int) (Math.random()*RAVEMSGS.length)]);
containerGroup.addView(newView,0);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();

Related

Android dev't using thread with textview: why this is crashing?

My goal is when the user tap start button, letters "o" "n" "o" "m" and so forth will appear at the center of the screen. "o" will appear first then after a few seconds will be replaced by "n" then "o" and so forth.
note: for brevity, i just make the guessword = onomatopoeia, first. In reality, guessword will changes every time i tap the start bottom.
this is the code:
private String guessword = "onomatopoeia";
private TextView showchar;
private int n = guessword.length();
private char letArray[]= guessword.toCharArray();;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
addStartListener();
}
public void addStartListener(){
Button start = (Button) findViewById(R.id.start);
showchar = (TextView) findViewById (R.id.charView);
start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Thread thread = new Thread()
{
#Override
public void run() {
try {
for(int i = 0 ; i < n ; i++) {
sleep(1000);
showchar.setText(letArray[i]);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
}
thanks for the help
I decided to implement runonuithread but still it crashes:
this is the updated version:
private String guessword = "onomatopoeia";
private TextView showchar;
private int n = guessword.length();
private char letArray[]= guessword.toCharArray();
private Handler handler;
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
handler = new Handler();
showchar = (TextView) findViewById (R.id.charView);
}
public void startGame(View view){
new Thread() {
public void run() {
while(i++ < n) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
showchar.setText(letArray[i]);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
use this code for setting the text in your textview
runOnUiThread(new Runnable() {
#Override
public void run() {
showchar.setText(letArray[i]);
}
});
You are updating ui from a thread which is not possible.
showchar.setText(letArray[i]);
UI must be updated ui thread.
All you are doing is repeatedly setting value to TextView you can use Handler with a delay for this purpose.
You could use runOnUiThread also but i don't see the need for a thread for what you are doing.
Use a Handler. You can find an example #
Android Thread for a timer

Display Values Dynamically in TextView - Android

I have a simple program with one button. Once I clicked on the button loop started and should display the current value in TextView dynamically. But this display the value when loop is completed.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView2);
}
public void btnClick(View v) throws Exception {
for (int i=0;i<10;i++){
tv.setText("Value of I "+i);
Thread.sleep(500);
}
}
My expected output is displaying values from 0, 1,.... dynamically but displayed 9 at last. if I use append I have to wait until loop terminated. Please help me.
Try this:
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);
}
Make sure you declare int i = 0 globally
tv.append("text here") is your answer.
don't use Thread.sleep() in your main UI thread this should give an exception instead use a new Thread like this
Thread myThread = new Thread(new Runnable() {
#Override
public void run() {
for (int i=0;i<10;i++){
tv.setText("Value of I "+i);
Thread.sleep(500);
}
}
});
myThread.start();
or if there is more complex operations you make you will have to use AsyncTask calss

Why do I need a Handler object in this example?

public class ProgressBarTest extends Activity {
private int progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_bar);
final Handler handler = new Handler();
progress = 0;
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
new Thread(new Runnable() {
public void run() {
while (progress < 10) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
}
handler.post(new Runnable() {
#Override
public void run() {
pb.setVisibility(View.GONE);
}
});
}
}).start();
}
}
Why can't I just put the pb.setVisibility(View.GONE) in the first Runnable inner class? Like this: The program crashes if I write it this way.
new Thread(new Runnable() {
public void run() {
while (progress < 10) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
pb.setVisibility(View.GONE);
}
}
}
The program crashes when the setVisibility statement is executed.
You cannot update ui from a thread. Ui should be updated on the ui thread.
In the second one you are setting the visibility of progressbar inside the threads runs method. Hence it crashes. So you use handler to set the visibility of progress bar in the the first
To know more about handlers.
http://developer.android.com/reference/android/os/Handler.html

Bitmap wallpaper change every 10 seconds android

Hello I'm trying to create bitmap wallpaper. But this bitmap changes every 10 seconds. How can I accomplish this?
This is what I have tried:
// I have declared
int[] images = {R.drawable.donna, R.drawable.donna1, R.drawable.marian,
R.drawable.marian1, R.drawable.marian};
Handler mHandler = new Handler();
ImageView imgView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView) findViewById(R.id.imageView1);
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
Random ran = new Random();
imgView.setImageResource(images[ran.nextInt(images.length)]);
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
But my question is how do I integrate the function/method for setting this as wallpaper?
Any help is truly appreciated. Thanks.
You can use postDelayed() to change your image wthin a specified timeframe:
Handler mHandler = new Handler();
Runnable __runnable = new Runnable()
{
#Override
public void run()
{
Random ran = new Random();
imgView.setImageResource(images[ran.nextInt(images.length)]);
mHandler.postDelayed(this, 10000);
}
};
new Thread(__runnable).start();
For your second question, see the link below:
how to set image as wallpaper from the ImageViev

I am trying to make a simple thread that changes a textview after a time

The problem is that when i click the button for the second my application crashes and i don't understand why. When i clink the button for the second time is suppose to the same all over again.
Any help is appreciated.
public class main extends Activity {
Boolean grabar = false;
TextView texto;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
texto = (TextView) findViewById(R.id.texto);
Button startbtn = (Button) findViewById(R.id.button);
startbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
background.start();
}
});
}
Thread background = new Thread (new Runnable() {
public void run() {
try {
Thread.sleep(3000);
cambiarHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Handler cambiarHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
texto.setText("ok");
}
};
}
The problem I see is that threads are designed to run once. You need to create a new instance of the thread each time you wish to run it.
This may well have something to do with Android's policy of only allowing the UI thread to modify the UI.
You should be using ASyncTask instead if you want to modify the UI from a separate thread.
See here:
Update UI from Thread
I don't know which exception is thrown. I guess it is NPE thrown. When you try to set text to the TextView. Try instantiating the TextView within the handleMessage() method:
Handler cambiarHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
texto = (TextView) findViewById(R.id.texto);
texto.setText("ok");
}
};
And in the onClick method do this to have new instance of the thread each time button is clicked.
public void onClick(View v){
new Thread (new Runnable() {
public void run() {
try {
Thread.sleep(3000);
cambiarHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}

Categories

Resources