I have a problem involving Handle.postDelay function in Android

Related searches

Basically i have a TextView which needs to move from right side to the other (I am testing something for a different project).Instead, everything is still. Nothing is executed.

Another thing to point out: If I remove the if statement, which is necessary in my case, everything works just fine. I thought the condition is false, so I put an else statement. Same thing happened - nothing. I tested whether or not the condition is false - it was true. So the code inside the if statement should've been executed.

Everything seems logical to me, but I could be wrong. Could you help me? What could I change? Do you have a different approach to my problem? Thanks!

P.S: The problem is not animating the TextView. The problem is the time delay.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >

        android:textColor="#000000" />


package com.nicksoft.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    int ind = -1;
    private Handler h = new Handler();
    protected void onCreate(Bundle savedInstanceState) {
    private Runnable r = new Runnable() {
        public void run() {
            if(findViewById(R.id.textView).getX()>0) {

Nothing works because getX returns 0. Inside onCreate stage View doesn't have real coordinates because layout haven't been built yet and getX returns default 0 and postDelayed isn't called. You call r.run(); too early.

You should wait until layout stage is finished. All views will get their coordinates. Then getX will return you something meaningful and your code will work.

If you don't 100% required to use such custom approach for "animation" use default platform ways e.g. PropertyAnimator

Android, What am I doing wrong? Is it my new Handler() creating problem? What is the best way to handle this? I want one sendData() call per second� When all requests have been handled, the IntentService stops itself, so you should not call stopSelf. So based on the result I get, I feel like I cannot use postDelayed in "worker thread". But can anyone explain this a bit more, like why this is not working in worker thread?

You can try to do it with the daimajia AndroidViewAnimations library https://github.com/daimajia/AndroidViewAnimations ... you could simply slice it to the left, and then make it invisible...

protected void onCreate(Bundle savedInstanceState) {

yourTextView = (TextView) findViewById(R.id.textView);
YoYo.with(Techniques.SlideOutLeft).duration(2500).playOn(yourTextView); //take 2500ms to slide
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {

Handler, the Handler's handleMessage(Message) method (requiring that you implement If this thread does not have a looper, this handler won't be able to receive Callback : The callback interface in which to handle messages, or null. queue, cause ordering problems, or have other unexpected side-effects. If the Runnable is NOT annonymous it means I have a reference to it (r in this case) which means I can use myHandler.removeCallbacks(r);.If the Runnable is anonymous then the flag will be a member in the enclosing class, which means I need a reference to that object to change the flag, which means I again will have to have r anyways, which means I can do myHandler.removeCallbacks(r);.

Another way could could make doing simple animations cleaner is using the animate builder on the view itself. You may need to fetch the screen width as I have done if you wish to move across the whole screen.

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        findViewById(R.id.textView).animate().translationXBy(displayMetrics.widthPixels * -1).setDuration(displayMetrics.widthPixels * 5).setStartDelay(5L);

Mastering Android Handler. What is Handler, internals and Best , If you are an Android developer, I can say with surety you have used The question arises do you understand it in depth? But how Handler is related to a Thread and how does we can use it to Every message it receives has a target attached to it which handles the Following code in your run method: If you have are having a problem connecting to Bluetooth, Wi-Fi, or your cellular network, enable Airplane mode for 30 seconds, toggle it off, and try connecting again.

Stop Using Post/PostDelayed in Your Android Views, Be wary of Handler API in Android when delaying actions in views. They're with an inner Handler and exposes both post() and postDelayed() functions. Have a closer look at its implementation (at the time I'm writing the article): I've seen so many crashes due to this API because developers failed to handle lifecycles. The Roku app for iOS and Android is a must-have add-on for your phone. It can act as a replacement remote control, plus you can add/remove channels, use the private listening function, and cast

Android Threading: All You Need to Know, Every Android developer, at one point or another, needs to deal with threads in their application. Loaders are the solution for the problem mentioned above. Since this involves making a request over the network, you should do it from a thread other The overall functionality will be difficult to get right and will require a� Problem: This is one of the most often Android problems. Unfortunately, we have to face the fact that our Android device will never run for as long as old simple mobile phones used to some years ago. This is the price we have to pay for having the smartest devices in the world.

Find the latest and greatest on the world’s most powerful mobile platform. Browse devices, explore resources and learn about the latest updates.

  • As you can see in the XML file, i have a TextView on the right side which needs to every 5ms move 1px to the left until it reaches the left side of the screen.
  • So, for some reason nothing is happening. Maybe there is an error in your answer? Perhaps I haven't used it correctly. Could you check again, please? Thank you for helping!
  • What should your answer do?