The following code is showing the error of cannot resolve symbol post even if import android.os.Handler; is used. And also invalid method declaration, return type is required for Runnable object.
TextView tv = (TextView) this.findViewById(R.id.txt);
String[] str = new String[] { "72", "71", "70",
"73", "75", "74", "69", "76", "77", "78" };
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
Random generator = new Random();
final int random = generator.nextInt(str.length);
tv.setText(str[random]);
h.postDelayed(this, 1000);
}
});
The import is correct, the problem seem to be inside the runnable. If you want to run a rask with delay the direct method is postDelay. Two arguments, a runnable and the delay.
h.postDelay(new Runnable..., 1000);
This is because you're calling the method post() outside of a method in a class.
For example, if your code inside an Activity class,
This is wrong:
public class MainActivity extends AppCompatActivity {
final Handler h = new Handler();
// This is outside a method. It's wrong.
h.post(new Runnable() {
#Override
public void run() {
Random generator = new Random();
final int random = generator.nextInt(str.length);
tv.setText(str[random]);
h.postDelayed(this, 1000);
}
});
...
#Override
protected void onCreate(Bundle savedInstanceState) {
}
}
This is right:
public class MainActivity extends AppCompatActivity {
private void generateRandom() {
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
Random generator = new Random();
final int random = generator.nextInt(str.length);
tv.setText(str[random]);
h.postDelayed(this, 1000);
}
});
}
...
#Override
protected void onCreate(Bundle savedInstanceState) {
}
}
Related
TextView output;
int i;
Random random=new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output=findViewById(R.id.textView);
new Thread(new mythread()).start();
}
class mythread implements Runnable{
#Override
public void run() {
try {
while(true) {
i = random.nextInt(100);
output.setText(i + "");
Thread.sleep(500);
}
}catch (Exception e){}
}
}
}
it just showing one number in text view
but requirement is ,it should generate random number and keep updating textview after 500ms
Thank You!
The main problem, in your code, is that you can update UI only in the main thread and you are using a custom thread.
The second problem is that you are using Thread.sleep that is a very bad practise.
I suggest you to use Handler
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
Log.d("Handler", "Running Handler");
handler.postDelayed(this, 500);
}
}
handler.postDelayed(runnable, 0);
and here the kotlin version
var handler = Handler()
var runnable = object : Runnable {
override fun run() {
Log.d("Handler", "Running Handler");
handler.postDelayed(this, 500)
}
}
handler.postDelayed(runnable, 500)
Try this, I think it will solved your problem.
public class MainActivity extends AppCompatActivity {
private Random random;
private Handler handler;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tv_number);
displayRandomNumber();
}
/**
* Display random number in text view
*/
private void displayRandomNumber()
{
random = new Random();
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
int value = random.nextInt();
textView.setText(String.valueOf(value));
handler.postDelayed(this,2000);
}
}, 2000);
}
}
I wrote code which can change the background every 3 second, but unfortunately it only change 1 time, I try using the count but it wont work, where did it went wrong?
public class MainActivity extends AppCompatActivity {
public static int count=0;
int images[] = new int[] {R.drawable.main, R.drawable.main2, R.drawable.main3, R.drawable.main4};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
public void run() {
if (count < images.length) {
RelativeLayout background = (RelativeLayout) findViewById(R.id.activity_main);
Random rand = new Random();
int index = rand.nextInt(images.length);
background.setBackgroundResource(images[index]);
count++;
}
else{
count = 0;
}
}
}, 3000);
}
};
Just call handler.postDelayed inside run() method to start it again, and modify your code like this:
public class MainActivity extends AppCompatActivity {
int images[] = new int[] {R.drawable.main, R.drawable.main2, R.drawable.main3, R.drawable.main4};
final Handler animHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout background = (RelativeLayout) findViewById(R.id.activity_main);
animHandler.post(new Runnable() {
#Override
public void run() {
Random rand = new Random();
int index = rand.nextInt(images.length);
background.setBackgroundResource(images[index]);
animHandler.postDelayed(this, 3000);
}
});
}
};
}
I have created this class, but there is an error last part saying
"Syntax error on token "(", delete this token" on the "timers.schedule part"
public class refrend
{
Timer timers = new Timer();
final Handler handler = new Handler();
int initial = 1000;
int looper = 6000;
TimerTask task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
new activityIns().execute();
}
});
}
};
timers.schedule(task, initial, looper);
}
Thanks for your help :)
what you can do is to create a function
public class refrend {
Timer timers = new Timer();
final Handler handler = new Handler();
int initial = 1000;
int looper = 6000;
public void your_Function(){
TimerTask task = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
// new activityIns().execute();
}
});
}
};
timers.schedule(task, initial, looper);
}
}
now you can call that function in you class by your_Functino();
and if you want to use that function out side of class create it object and call
it
refrend object=new refrend();
object.your_Function();
i am creating a sample app in which i want to change a TextVeiw value every second.
TextView value should change continue every second.
to perform this task i tried this below code:
but this code is not changing text values continue, it only change on app start or screen rotate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Boolean b = true;
final TextView tv = (TextView) this.findViewById(R.id.textView1);
final String[] str = new String[] { "897451", "34232", "33432",
"46867", "54554", "6756", "56r7", "2345u8", "9654", "987650", };
Random generator = new Random();
final int random = generator.nextInt(str.length);
Thread t = new Thread() {
#Override
public void run() {
try {
while (b != false) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// update TextView here!
tv.setText(str[random]);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
and i also tried this code:
but this code crash application
private Timer timer = new Timer();
private TimerTask timerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
final TextView tv = (TextView) this.findViewById(R.id.textView1);
final String[] str = new String[] { "897451", "34232", "33432",
"46867", "54554", "6756", "56r7", "2345u8", "9654", "987650", };
Random generator = new Random();
final int random = generator.nextInt(str.length);
timerTask = new TimerTask() {
#Override
public void run() {
// refresh your textview
tv.setText(str[random]);
}
};
timer.schedule(timerTask, 0, 1000);
}
Try the following code snippet
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
long millis =(long)currentTime();
dateAndTime.setText(getDate(millis, "dd/MM/yyyy hh:mm:ss.SSS"));
h.postDelayed(this, 1000);
}
});
final TextView tv = (TextView) this.findViewById(R.id.textView1);
final String[] str = new String[] { "897451", "34232", "33432",
"46867", "54554", "6756", "56r7", "23458", "9654", "987650", };
// //
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
Random generator = new Random();
final int random = generator.nextInt(str.length);
tv.setText(str[random]);
h.postDelayed(this, 1000);
}
});
// //
i'm working on an Android application in which i want the background image to be changed after every 5 seconds. i have all the images in my drawable folder.
i am giving the code which i am using but i am not getting the output. As an output i am getting a still image which is not changing.
Please help
Thanks
[CODE]
public class Home extends Activity {
public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
new Handler().postDelayed(new Runnable() {
public void run() {
if(count<drawablearray.length){
Home.this.getWindow().
setBackgroundDrawableResource(drawablearray[count]);
count++; //<<< increment counter here
}
else{
// reset counter here
count=0;
}
}
}, 5000);
}
}
You can achieve this using Timer
public class Home extends Activity {
public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
Timer _t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
lnMain = (LinearLayout) findViewById(R.id.lnMain);
_t = new Timer();
_t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() // run on ui thread
{
public void run() {
if (count < drawablearray.length) {
lnMain.setBackgroundDrawable(drawablearray[count]);
count = (count + 1) % drawablearray.length;
}
}
});
}
}, 5000, 5000);
}
}
Why don't you have a look at the ViewFlipper class
http://developer.android.com/reference/android/widget/ViewFlipper.html
final Handler h = new Handler();
Runnable r = new Runnable() {
public void run() {
Home.this.getWindow().setBackgroundDrawableResource(drawablearray[count]);
count += (count+1)%drawablearray.length; //<<< increment counter here
h.postDelayed(this, 5000);
}
};
now call like
h.postDelayed(r, 5000);
I think that would be easier to work with the xmls. You can change the background of the main layout of the activity.
Try something like this:
public class Home extends Activity {
public static int count=0;
int[] drawablearray=new int[]{R.drawable.slider_1,R.drawable.slider_2,R.drawable.slider_3,R.drawable.slider_4,R.drawable.slider_5};
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ll = (LinearLayout) findViewByID(R.id.mainlayout) //It depends of the name that you gave to it
new Handler().postDelayed(new Runnable() {
public void run() {
ll.setBackgroundDrawable(drawablearray[count]);
// or ll.setBackgroundResource(resid) if you want.
count += (count+1)%drawablearray.length;
}
}, 5000);
}
}