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

  • Home
  • SEARCH
  • 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 8687975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:14:53+00:00 2026-06-12T23:14:53+00:00

I get a number of artifacts and incorrect behavior when trying to switch views

  • 0

I get a number of artifacts and incorrect behavior when trying to switch views in a ViewFlipper. I want to add and remove views on demand, so, instead of using XML, I must call ViewGroup.addView(). At certain times I want to clean up the container by removing all children but the last. Here is a demo:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);

        final ViewFlipper flipper = new ViewFlipper(this);

        Animation in = new TranslateAnimation(-200, 0, 0, 0);
        in.setDuration(300);
        Animation out = new TranslateAnimation(0, 200, 0, 0);
        out.setDuration(300);
        flipper.setInAnimation(in);
        flipper.setOutAnimation(out);

        // clean it up
        out.setAnimationListener(new AnimationListener(){

            @Override
            public void onAnimationEnd(Animation animation) {
                flipper.post(new Runnable(){
                    public void run() {
                        flipper.removeViews(0, flipper.getChildCount() - 1);
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationStart(Animation animation) {}
        });

        Button button = new Button(this);
        button.setText("Click me");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                View a = makeView();
                flipper.addView(a);
                flipper.showNext();
            }
        });

        root.addView(button);
        root.addView(flipper);
        setContentView(root);
    }

    int i = 0;

    public View makeView() {
        TextView tv = new TextView(MainActivity.this);
        tv.setText("TextView#" + i++);
        tv.setTextSize(30);
        return tv;
    }
}

Sometimes I want to remove all children but the last added, to save memory and because those children will never be used again (Maybe I can recycle them, but that’s another story). I use a simple runnable scheduled via View.post() in the animation listener, and exactly once every three times there are artifacts.

Using View.post(Runnable) is required because if you remove children directly in the animation listener, NullPointerException are thrown because (at least on Honeycomb+, which uses display lists to draw hierarchies).

Note: I’m developing for 2.1+, so the Honeycomb animation packages doesn’t suit.

  • 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-12T23:14:54+00:00Added an answer on June 12, 2026 at 11:14 pm

    Those “artifacts” appear because in your case you try to remove all the views except the one at the top leaving the index of the current displayed child out of order. The ViewFlipper will try to compensate this and from the looks of it doesn’t succeed. But you could still do what you want with no visual problems like this:

    flipper.post(new Runnable() {
        public void run() {
    
            if (flipper.getChildCount() < 4) // simulate a condition
                return;
    
            flipper.setInAnimation(null);
            flipper.setOutAnimation(null);
    
            while (flipper.getChildCount() > 1)
                flipper.removeViewAt(0);
    
            flipper.setInAnimation(in);
            flipper.setOutAnimation(out);
    
            assert  flipper.getChildCount() == 1;
        }
    });
    

    This should leave only the visible View in the ViewFlipper. See if the code solves the problem.

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

Sidebar

Related Questions

Im trying to get random number between -5 and 5. Im using this code:
I am trying to get number of day differences using Floor function between a
How to get number of revision by using repo command?
I'm messing with core.time.Duration s - in particular, I'm trying to properly get number
How can I get the number of followers of any Twitter account? I want
I easily get Week Number using MySql Week function like this WEEK(SYSDATE()) I just
I'm trying to get the mercurial revision number in the manifest. I've read following
I am trying to get number of files in a directory through c++ for
I have several field that just get number, I want if user typed be
I'm trying to add the svn.revision to project version as a build number and

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.