TrinityTuts Tips

Animation between login and forgot screen android

Last updated on March 13th, 2020 at 12:02 pm

In this tip i am going to share a simple and use full code which help you to save extra screen and also a simple animation between layout in android

Layout Code

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/activity_horizontal_margin">

            <ImageView
                android:layout_width="@dimen/logoSizeNormal"
                android:layout_height="@dimen/logoSizeNormal"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/appName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/app_name"
                android:textColor="@color/white"
                android:textSize="@dimen/logoTextNormal"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/loginForm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/top_margin"
                android:background="@drawable/loginbg"
                android:elevation="5dp"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/loginEmail"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/logoSizeNormal"
                    android:background="@android:color/transparent"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:singleLine="true"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="@dimen/normalText" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:background="#cccccc" />

                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/logoSizeNormal"
                    android:background="@android:color/transparent"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:singleLine="true"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="@dimen/normalText" />

            </LinearLayout>

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin10"
                android:layout_marginRight="@dimen/margin10"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/forgot"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:background="@null"
                    android:text="@string/forgot"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:textSize="@dimen/smallText" />

                <Button
                    android:id="@+id/loginButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@drawable/bottom_round_button"
                    android:elevation="2dp"
                    android:text="@string/login"
                    android:textAllCaps="false"
                    android:textColor="@color/white" />
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/forgotForm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/top_margin"
                android:background="@drawable/loginbg"
                android:elevation="5dp"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/recoverEmail"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/logoSizeNormal"
                    android:background="@android:color/transparent"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:singleLine="true"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="@dimen/normalText" />

            </LinearLayout>

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin10"
                android:layout_marginRight="@dimen/margin10"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/login"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:background="@null"
                    android:text="@string/login_shape_stick"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:textSize="@dimen/smallText" />

                <Button
                    android:id="@+id/forgotButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@drawable/forgot_button"
                    android:elevation="2dp"
                    android:text="@string/recover"
                    android:textAllCaps="false"
                    android:textColor="@color/white" />
            </FrameLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="@dimen/margin10"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/top_margin">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|center_vertical"
                android:background="@null"
                android:text="@string/sin_up"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="@dimen/smallText" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:background="@drawable/round_normal"
                android:paddingBottom="@dimen/pixel5"
                android:paddingLeft="@dimen/margin10"
                android:paddingRight="@dimen/margin10"
                android:paddingTop="@dimen/pixel5"
                android:text="@string/ques"
                android:textColor="@color/colorPrimaryDark"
                android:textStyle="bold" />
        </FrameLayout>
    </LinearLayout>
</FrameLayout>

Java Code

package com.animationview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    Button forget, login, loginButton, forgotButton;
    LinearLayout loginForm, forgotForm;
    AlphaAnimation fadeIn, fadeOut;
    EditText loginEmail, password, recoverEmail;

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

        loginEmail = (EditText) findViewById(R.id.loginEmail);
        password = (EditText) findViewById(R.id.password);
        recoverEmail = (EditText) findViewById(R.id.recoverEmail);

        forget = (Button) findViewById(R.id.forgot);
        login = (Button) findViewById(R.id.login);
        loginButton = (Button) findViewById(R.id.loginButton);
        forgotButton = (Button) findViewById(R.id.forgotButton);

        loginForm = (LinearLayout) findViewById(R.id.loginForm);
        forgotForm = (LinearLayout) findViewById(R.id.forgotForm);

        fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setStartOffset(100);
        fadeOut.setDuration(500);

        calAction();
    }

    public void calAction() {

        forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fadeOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        loginForm.setVisibility(View.GONE);
                        showForget();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                loginForm.startAnimation(fadeOut);
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fadeOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        forgotForm.setVisibility(View.GONE);
                        showLogin();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                forgotForm.startAnimation(fadeOut);
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        forgotButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void showForget() {
        fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setDuration(500);
        fadeIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                clearForms();
                forgotForm.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        forgotForm.setAnimation(fadeIn);
    }

    public void showLogin() {
        fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setDuration(500);
        fadeIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                clearForms();
                loginForm.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        loginForm.setAnimation(fadeIn);
    }

    public void clearForms() {
        loginEmail.setText("");
        password.setText("");
        recoverEmail.setText("");
    }
}

That all, you can download complete code from above download link.