PROBLEM
I have nearly 20 TextView and I need to have do something to make their background color change dynamically.
For example: all TextView have same default background color then after 5 sec first one's will turn red where others still same then after 5 more sec passed first TextView's background color will turn to default and second TextView's background color will be turn to red and it will go on like this.
I tried to put them in for loop in thread and handler but they changed all together. What should I do ?
I have tried this code
Thread color=new Thread() {
public void run() {
for(int i=2131230724;i<=2131230742;i++){
TextView currentText = (TextView) findViewById(i);
currentText.setBackgroundColor(Color.RED);
try {
sleep(2000);
currentText.setBackgroundColor(Color.TRANSPARENT);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
then I used
mHandler.post(color);
SOLUTION
I found a solution for my problem and sharing if someone else needed too.
Runnable myhandler=new Runnable() {
int id=2131230724;//First textView id
#Override
public void run() {
// TODO Auto-generated method stub
if(id==2131230724){
TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime1);
currentText.setBackgroundColor(Color.RED);
id++;
}
else if(id==2131230725){
TextView currentText=(TextView)findViewById(R.id.egzersiz01_kelime2);
currentText.setBackgroundColor(Color.RED);
TextView PreText=(TextView)findViewById(R.id.egzersiz01_kelime1);
PreText.setBackgroundColor(Color.TRANSPARENT);
id++;
}//And all other id's to else if as same as mine.
mHandler.postDelayed(myhandler, delay);
and in onCreate()
myhandler.run();
I have tried to use for loop to make it easy but it crashed and I had to write them all.
Thanks for helping...
You can put the params to AsynTask like this, and then
new changeColor().execute(max);
private class changeColor extends AsyncTask<Integer,Integer,Void>{
int max,cur;
#Override
protected void onPreExecute() {
max=cur=0;
super.onPreExecute();
}
#Override
protected Void doInBackground(Integer... number) {
int max=number[0];
int cur=1;
if(max>0)
while(true ){/*or other condition else*/
try {
Thread.sleep(5000);//Sleep 5000s before make change
} catch (InterruptedException e) {
Log.i("TAG","InterruptedException");
}
publishProgress(cur);
if(cur==max)
cur=1;
else
cur++;
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
changeColorTextView(values[0]);
super.onProgressUpdate(values);
}
private void changeColorTextView(int textViewId){
switch(textViewId){
case textId1:{
//do some thing here like:
//textView2.setBackGroundColor(R.color.RED);
//textView1.setBackGroundColor(default);
break;
}
//....
}
}
}
Related
I am wondering about the post method use into the Thread class.
My program is about to find fixed salary from the basic salary.Basically in this program ,I have one editText where user have to enter the basic salary and when the click on the button called 'show' there is one counter which display on the screen 1,2,3,...10 in textview.Then after the fixed salary will displayed on other editText.So,this about my program
Now,In code I have make one class called Mythread for the counter purpose.In which I have put the loop which count 1,2,...10.But my problem is that the i want to refresh the values of the textView so i want to use post method but how to use in my code that i don't know.
Kindly guide me
package com.example.bs_to_fs_thread;
import android.widget.TextView;
public class MyThread extends Thread{
TextView _tv;
String fs;
public MyThread(TextView tv,String sfs){
_tv=tv;
fs=sfs;
}
public void run()
{
int i=0;
while(i<10)
{
_tv.post(new Runnable()
{
public void run()
{
_tv.setText(i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
MainActivity.printFS(fs);
}
}
`
In this code ,may be I write the _tv.post(Runnable{ });
on wrong place pls give me the view of this post method and how should and where should i write this post method? and why?
Try something like this(I have not tested it dont have editor now):
public MyThread(TextView tv,String sfs,Activity a){
this._tv=tv;
this.fs=sfs;
this.a= a;//this is required to use method runOnUiThread as background thread cannot modify Ui thread
}
Then in the place where you want to modify your UI like change text do something like this:
a.runOnUiThread(new Runnable() {
public void run()
{
Toast.makeText(a, "show some toast", Toast.LENGTH_SHORT).show();
_tv.setText("mytext");
}
});
Try this:
public void run() {
int i = 0;
while(i < 10) {
// We can't use 'i' directly inside the runnable declaration, because all
// external variables must be declared as final
final int number = i;
// Posting this runnable to the UI thread
_tv.post(new Runnable() {
public void run() {
_tv.setText(number);
}
}
// Sleeping current thread (NOT UI thread) for 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Increment 'i'
i++;
}
MainActivity.printFS(fs);
}
I am working on an android app in which i want to hide my image view after some interval. i am using this code but it is not hiding. can anybody tell me how i can hide it ???
showtrue1.setBackgroundResource(R.drawable.ticktrue);
showtrue1.setVisibility(View.VISIBLE);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
showtrue1.setVisibility(View.GONE);
Try CountDownTimer:
new CountDownTimer(1000, 100) {
public void onTick(long millisUntilFinished) {
// implement whatever you want for every tick
}
public void onFinish() {
showtrue1.setVisibility(View.GONE);
}
}.start();
You can use async Task also to solve your problem . In its backgound function make a thread sleep for particular second and in the post method make trhe visibility gone for your image viiew.
Call the execute method in your oncreate
new MyAsyncTask().execute();
and make an inner class as defined below:
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
showtrue1.setBackgroundResource(R.drawable.ticktrue);
showtrue1.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... voids){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void params)
{
showtrue1.setVisibility(View.GONE);
}
}
Create a separate thread that sleeps for 1 seconds then call runOnUiThread to hide the view.
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// Do some stuff
showtrue1.setVisibility(View.GONE);
}
});
}
};
All i want to do is display a text for 1000 milliseconds and get the next thing.. dialogs are slow to pop up so i used a textview..but its not working.. the code snippet is given..
What am I doing wrong?
Thread sleep_for_sometime = new Thread() {
public void run() {
try {
correct.setVisibility(TextView.VISIBLE);
sleep(1000);
correct.setVisibility(TextView.INVISIBLE);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
get_question(questionset[i]);
}
}
};
//sleep_for_sometime.setDaemon(false);
sleep_for_sometime.start();
You should change visibility in UI thread. Read painless threading
AsyncTask is great, but overused in some ways I think. Just use post/postDelayed. You're not really doing background work, you just want to delay some UI-thread work. Definitely read Nikita's link, but I would do something like:
correct.setVisibility(View.VISIBLE);
correct.postDelayed(new Runnable() {
correct.setVisibility(View.INVISIBLE);
get_question(questionset[i]);
}, 1000);
Expanding Nikita's Answer , you could do something like this
private class SleepTask extends AsyncTask<Void,Integer,Void> {
#Override
protected Void doInBackground(Void... params) {
try {
publishProgress(0);
sleep(1000);
publishProgress(1);
}catch(Exception e){
//null
}
}
#Override
protected void onProgressUpdate(Integer... i){
if(i[0]==0){
correct.setVisibility(TextView.VISIBLE);
}else{
correct.setVisibility(TextView.INVISIBLE);
}
}
#Override
protected void onPostExecute(Void res) {
get_question(questionset[i]);
}
#Override
protected void onPreExecute() {
//do something before execution
}
}
to start the thread , do this
new SleepTask().execute();
I want to show images one by one neither in vertical nor horizontal.
I just created one imageview in xml layout and then i called that image by through id and then i just want to show list of images in one imageview like this.
It first show first image after some time the first image was removed and then second image will be visible with out changing position of imageview like ads in android device. I mean Every 10 seconds the imageview has been changed.
So i tried below code
class doInBack extends AsyncTask<URL, Bitmap, Long>
{
#Override
protected Long doInBackground(URL... arg0) {
for(int i=0;i<adimage.length;i++){
Bitmap bitmap=Download(adimage[i]);
publishProgress(bitmap);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
protected void onProgressUpdate(Bitmap bitmap) {
adimg.setImageBitmap(bitmap);
}
protected void onPostExecute(Long result) {
}
}
But it didn't call OnProgressUpdate method. So please tell me how to show number of images one by one in one imageview.
I used below code its working
int i=0;
final Handler handler=new Handler();
final Runnable r=new Runnable()
{
public void run()
{
if(i<adimage.length){
adimage.setImageBitmap(Download(adimage[i]));
i++;
if(i==adimage.length)
i=0;
}
else
i=0;
}
};
if(thread == null){
thread = new Thread()
{
#Override
public void run() {
try {
while(true) {
sleep(10*1000);
handler.post(r);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
I want show dialog after finish Thread.
In thread I am changing TextView's Value like as 0 to 100...
When TextView Value is reach 100 then i want to show dialog..
What i do for it.
Thanks in advance...
Code Snippet:
final Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
synchronized (this)
{
try
{
for(int i=0 ; i<speed; i++)
{
final int value=i+1;
wait(3000/speed);
Test.this.runOnUiThread(new Runnable() {#Override public void run()
{
accText.setText(String.valueOf(value));
}});
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread.start();
this is asyntask code snippet...
class setTextBackgroundTask extends AsyncTask<String , Integer, Void>
{
#Override
protected void onPreExecute()
{
}
#Override
protected Void doInBackground(String... params)
{
Thread th = new Thread();
int value;
for(int i=0 ; i<speed; i++)
{
value=i+1;
publishProgress(value);
try {
th.sleep(3000/speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values)
{
accText.setText(String.valueOf(values[0]));
System.out.println("Value=="+values[0]);
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void result)
{
showShareDialog();
}
}
This document explains how you can create dialogs.
Please note that you will have to do any dialog creation code on the UI thread.
You already have code that runs something on the UI thread, just do that outside the loop,but with the dialog creation code inside.
You should use AsyncTask: subclass AsyncTask, override doInBackground() to execute your time consuming action on another thread, and then override onPostExecute() to show your dialog.
Note that you cannot change UI elements from a non-UI (background) thread. AsyncTask takes care of that for you: it calls doInBackground() on a new thread and then calls onPostExecute() on the UI thread as soon as the background task is complete.