Full width home advertisement

Android Development

CPP & JAVA

Post Page Advertisement [Top]

Activity Slide Animation

In this tutorial, we will learn how to open and close activities with animation. We will use activity open and close animations. There are two ways to achieve this. First, we can use overridePendingTransitions() after starting the activity and before finishing the activity. This will be applied for the activity calling these functions only. If we want to apply it for all activities, we need to create a style and assign it to the app.

YouTube Video



Using Override Pending Transition


Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
// Add this method just after start activity
// first parameter is enter anim id
// second parameter is exit anim id
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

    // override finish method and use the same method here
    @Override
    public void finish() {
        super.finish();
        // Add this method just after start activity
        // first parameter is enter anim id
        // second parameter is exit anim id
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
    }

Using Style


<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.DemoApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        
        <!--     Assign the animation style     -->
        <!--=================      Add This Line      ==========================-->
        <item name="android:windowAnimationStyle">@style/ActivityAnim</item>
        <!--====================================================================-->
    </style>

    <!--    Add New Style   -->
    
    <style name="ActivityAnim" parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/slide_in</item>
        <item name="android:activityCloseEnterAnimation">@anim/slide_in</item>
        <item name="android:activityOpenExitAnimation">@anim/slide_out</item>
        <item name="android:activityCloseExitAnimation">@anim/slide_out</item>
    </style>

</resources>

Animation Code

Slide In


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500">

    <translate
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:fromXDelta="-100%p"
        android:toXDelta="0%"/>

</set>

Slide Out


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500">

    <translate
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:fromXDelta="0%"
        android:toXDelta="100%p"/>

</set>

Activity Codes

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="go"
        android:text="Go"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java


package com.code_vedanam.demoapp;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    }

    public void go(View view) {
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }
}

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib