Pam Ix
Pam Ix

Reputation: 468

Android not animating the Relative Layout

I am creating a Relative layout which I want should come from above sliding into the layout so here's what I did

  1. made the layout invisible
  2. In oncreate animated the layout above the screen
  3. and in onWindowsFocusChanged() I called animation , made layout visible and want layout to slide into the screen

BUT

when view is created the layout is at its right location with out showing any sliding effect from coming from top of screen

public class OverlayActivity extends Activity implements View.OnClickListener {
  RelativeLayout question_box;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overlay);
// Slide Up the INVISIBLE layout so that I can call it by animation back to its original position

        question_box = findViewById(R.id.question_box);
        question_box.animate().translationY(-question_box.getHeight());


        final Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
      
    }

    public void animateInObjects() {
        question_box.setVisibility(View.VISIBLE);
        question_box.animate().setDuration(1000).translationY(0);
    }

    @Override
    public void onClick(View v) {
//Some Code
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        animateInObjects();
        super.onWindowFocusChanged(hasFocus);
    }

}

Layout


    <RelativeLayout
        android:id="@+id/question_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/ad_view_container"
        android:visibility="invisible">

        <TextView
            android:id="@+id/question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/questiontext"
            android:paddingStart="20dp"
            android:paddingTop="7dp"
            android:paddingEnd="20dp"
            android:paddingBottom="20dp"
            android:text="@string/sample_question"
            android:textAlignment="center"
            android:textColor="@color/text_quest"
            android:textSize="23sp" />

        <View
            android:id="@+id/center_vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_centerVertical="true" />

        <LinearLayout
            android:id="@+id/cover_opt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/question"
            android:layout_marginStart="15dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/main_layout">

            <Button
                android:id="@+id/opt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginVertical="1dp"
                android:layout_marginHorizontal="2dp"
                android:background="@android:color/transparent"
                android:text="@string/sample_number"
                android:textAlignment="center"
                android:textColor="@color/text_quest"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/cover_opt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/question"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:background="@drawable/main_layout">

            <Button
                android:id="@+id/opt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginVertical="1dp"
                android:layout_marginHorizontal="2dp"
                android:background="@android:color/transparent"
                android:text="@string/sample_number"
                android:textAlignment="center"
                android:textColor="@color/text_quest"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/cover_opt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/question"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="15dp"
            android:background="@drawable/main_layout">

            <Button
                android:id="@+id/opt3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginVertical="1dp"
                android:layout_marginHorizontal="2dp"
                android:background="@android:color/transparent"
                android:text="@string/sample_number"
                android:textAlignment="center"
                android:textColor="@color/text_quest"
                android:textSize="18sp" />
        </LinearLayout>

    </RelativeLayout>

this is the theme of the activity

<style name="Theme.Lockscreen" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">#33000000</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

Upvotes: 1

Views: 108

Answers (2)

user12757262
user12757262

Reputation:

Try this as your xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
      android:fromYScale="0"
      android:toYScale="50%"
      android:duration="1000"></scale>
</set>

To have it till half of your screen and arrange layout respectively

Upvotes: 1

Shay Kin
Shay Kin

Reputation: 2657

well you try to animate the view in onCreate method. at first the view is not drawn yet and you get a getHeight = 0 .so you must wait when the view is drawn by using view.getViewTreeObserver().addOnGlobalLayoutListener to be able to animate it

you need to add this on your onCreate() :

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
// Slide Up the INVISIBLE layout so that I can call it by animation back to its original position

        question_box = findViewById(R.id.question_box);

        question_box.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (question_box.getHeight() != 0)
                    question_box.animate().translationY(-question_box.getHeight());
            }
        });

        final Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    }

Upvotes: 2

Related Questions