Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:44:56+00:00 2026-06-13T19:44:56+00:00

I’ve wrote a small activity that is able to switch between two views. Now

  • 0

I’ve wrote a small activity that is able to switch between two views. Now I am trying to add some animation (fade-in/fade-out effect). Can anybody explain me how to do that right?

My own attempt to do this works kinda buggy (if I will click buttons very fast, my application freezes). I use code listed below:

public class WelcomeActivity extends Activity {
private boolean isLogin = false;
private String KEY_IS_LOGIN = "KEY_IS_LOGIN";
private Animation anim_fadein;
private RelativeLayout welcome, login;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    welcome = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_menu, null);
    login = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_login, null);
    anim_fadein = AnimationUtils.loadAnimation(this, R.anim.anim_fadein);
    if (savedInstanceState != null)
        isLogin = savedInstanceState.getBoolean(KEY_IS_LOGIN, false);
    if (isLogin)
        setContentView(login);
    else
        setContentView(welcome);
}

@Override
public void onBackPressed() {
    if (isLogin) {
        setContentView(welcome);
        welcome.startAnimation(anim_fadein);
        isLogin = false;
    } else {
        super.onBackPressed();
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(KEY_IS_LOGIN, isLogin);
    super.onSaveInstanceState(outState);
}

public void onButton1Click(View v) {
    setContentView(login);
    login.startAnimation(anim_fadein);
}

public void onButtonLoginClick(View v) {
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(i);
}

public void onButtonBackClick(View v) {
    setContentView(welcome);
    welcome.startAnimation(anim_fadein);
}

Animation XML file:

<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="800" />

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:44:57+00:00Added an answer on June 13, 2026 at 7:44 pm

    The way I have done this in the past is by using the ViewFlipper class and utilizing the built-in animation functions that the package provides.

    Here is an example on how to do this; in my experience the transitions have been very smooth:

    The XML File

    <LinearLayout
        //Ommitted...
        <ViewFlipper
            android:id="@+id/[your_id_here]"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
                <RelativeLayout
                    <!--Your first layout XML here...-->
                </RelativeLayout>
                <RelativeLayout
                    <!--Your second layout XML here...-->
                </RelativeLayout>
        </ViewFlipper>
    </LinearLayout>
    

    Please note that you do not have to use relative layouts, I simply used them for the sake of clarity.

    Implementing The Animations

    Get a reference to the ViewFlipper in your activity:

    ViewFlipper v = (ViewFlipper) findViewById(R.id.[your_id]);
    

    Set the animations as necessary:

    v.setInAnimation(AnimationUtils.loadAnimation([your_activity_name].this, R.anim.[your_in_animation here]));
    v.setOutAnimation(AnimationUtils.loadAnimation([your_activity_name].this, R.anim.[your_out_animation here]));
    

    Please note that you can find some really good prebuilt animations in the Android class files located in the following directory:

    [android-sdks]/samples/android-[VERSION_NUMBER_HERE]/ApiDemos/res/anim
    

    I highly recommend using these if you can – it will save you much time.

    Now, if you wish to switch between the views, use the following commands:

    v.showNext();
    v.showPrevious();
    

    You might have to change the animation files slightly to make sure the animations transition properly (i.e. make a fade right and left animation).

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.