0

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:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="#000000"
        android:text="0"
        android:textColor="#000000" />
</RelativeLayout>

Java:

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();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        r.run();
    }
    private Runnable r = new Runnable() {
        @Override
        public void run() {
            if(findViewById(R.id.textView).getX()>0) {
                findViewById(R.id.textView).setTranslationX(ind);
                ind--;
                h.postDelayed(r,5);
            }
        }
    };
}
Nick
  • 45
  • 8
  • 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. – Nick Feb 03 '20 at 21:07

3 Answers3

0

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 {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nick);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        findViewById(R.id.textView).animate().translationXBy(displayMetrics.widthPixels * -1).setDuration(displayMetrics.widthPixels * 5).setStartDelay(5L);
    }
}
David
  • 5,203
  • 2
  • 16
  • 17
  • 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! – Nick Feb 03 '20 at 22:23
  • What should your answer do? – Nick Feb 03 '20 at 22:23
0

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...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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() {
        @Override
        public void run() {
            yourTextView.setVisibility(View.INVISIBLE);
        }
    },2500);}
GusCorreia
  • 75
  • 7
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

Leonidos
  • 10,482
  • 2
  • 28
  • 37