1

setTranslationY() does not execute translation on a layout in onCreate(), while it's got no problem when used in a setonTouchListener or setOnClickListener.

I've also tried animate().translationY(). However the same exact problem appears. I've tried to present the parts of my code in need to make the issue more clear. A layout is included in the activity. This layout contains another layout which it's height is used in translation measures.

Java codes:

View mainMenuIncluded, mainMenu ;
mainMenuIncluded = findViewById(R.id.main_menu_included);
mainMenu = mainMenuIncluded.findViewById(R.id.main_menu);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_singer_list);
        mainMenuIncluded.setTranslationY(mainMenuIncluded.getY()-mainMenu.getHeight());
    }

The related xml part of the activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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=".activity">
<include
            layout="@layout/layout_pick_main_menu"
            android:id="@+id/main_menu_included"/>

    </android.support.constraint.ConstraintLayout>

The xml of the included layout (containig mainMenu):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/main_menu_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp">
<android.support.constraint.ConstraintLayout
        android:id="@+id/main_menu_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:background="@drawable/menu_gradient_background"
        app:layout_constraintBottom_toTopOf="@id/main_menu_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/main_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:background="@drawable/menu_gradient_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            >
        </android.support.constraint.ConstraintLayout>

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
elyar abad
  • 771
  • 1
  • 8
  • 27

2 Answers2

2

You can animate and move view only when it already drawn and measured. Use:

Kotlin version:

yourView.post { 
    yourView.animate().translationY(/*value*/)            
}

Java version:

yourView.post(new Runnable() {
    @Override
    public void run() {
        yourView.animate().translationY(/*value*/);
    }
};

for this

Peter Staranchuk
  • 1,343
  • 3
  • 14
  • 29
1

Just replace this part:

mainMenuIncluded.setTranslationY(mainMenuIncluded.getY()-mainMenu.getHeight());

with:

mainMenuIncluded.post(new Runnable() {
    @Override
    public void run() {
        mainMenuIncluded.setTranslationY(mainMenuIncluded.getY() - mainMenu.getHeight());
    }
};