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
Related
How to make a picture that would be updated every 30 seconds ?
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
You are going to want to use something like a thread to do this.
For example, below your image view:
Runnable imageUpdater = new Runnable() {
#Override
public void run() {
while (true) {
try {
sleep(30000); // 30 seconds in milliseconds
} catch(InterruptedException e) {
// Someone called thread.interrupt() and tried
// to stop the thread executing
return;
}
// Here load the image in the same way as above:
// But you will need to go onto the UI thread first.
image.post(new Runnable() {
public void run() {
Picasso.with(YourActivity.this).load( "http://i.imgur.com/DvpvklR.png").memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
}
});
}
}
};
Then you just start the runnable:
Handler handler = new Handler();
handler.post(imageUpdater);
String[] imageLink = {http://i.imgur.com/DvpvklR.png,
http://i.imgur.com/DvpvklR.png, http://i.imgur.com/DvpvklR.png};
int position = 0;
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(runnable);
}
}, 30 * 1000, 30*1000);
Runnable runnable = new Runnable() {
#Override
public void run() {
Picasso.with(this).load(imageLink[position%imageLink.length]).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
position++;
}
};
Hope the above code will help you :)
I want to implement ProgressBar in Android and when I execute the program, Progressbar should show for up to 2 seconds. I can't get it to work properly but I can't figure out why.
public void myThread(){
Thread th=new Thread(){
#Override
public void run(){
try
{
while(mRunning)
{
Thread.sleep(10L);//10s wait
YourCurrentActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
//DISMISS PROGRESS BAR HERE
mRunning=false;
}
});
}
}catch (InterruptedException e) {
// TODO: handle exception
}
}
};
th.start();
}
I have tried this but it does not giving me output as i want.
That what handlers are for in Android.
Example:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// cancel or dismiss your progressbar
}
}, 2000);
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
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();
When i using this timer to change the image, its not working. I am trying to change the image when the timer run.I put this timer inside onCreate is that okay?
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
int i=0;
#Override
public void run() {
img1.setImageResource(Images[i]);
i++;
}
};
timer.scheduleAtFixedRate(tone, 500, 500);
try something like this:
private Handler handler = new Handler();
handler.postDelayed(runnable, 100);
private Runnable runnable = new Runnable() {
#Override
public void run() {
/* do what you need to do */
changeImage();
/* and here comes the "trick" */
handler.postDelayed(this, 100);
}
};
changeImage can be something like
void changeImage()
{
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
img1.setImageResource(Images[i]);
i++;
}
});
}