Search code examples
androidandroid-activityoverlapviewgroup

Android: ViewGroup Overlapping When Adding Children within Activity


My situation is as follows: In my layout xml, I have a general LinearLayout serving as a master container and I want to populate the contents based on a paging system all contained within the single activity.

My Java Code is as follows:

private void goToPage(int pageNum){
    LinearLayout layout = (LinearLayout)this.findViewById(R.id.layoutRadioGroups);

    //Clear Views
    layout.removeAllViews();

    //Figure out array positions based on page number
    if (pageNum < this.PAGE_NUM)
        this.LAST_QUESTION_START -= this.NUM_PER_PAGE[pageNum];
    else{
        if (pageNum > 0)
            this.LAST_QUESTION_START += this.NUM_PER_PAGE[pageNum - 1];
        else
            this.LAST_QUESTION_START = 0;
    }
    this.PAGE_NUM = pageNum;

    //Grab my array values, create a TextView, and add it to the layout
    for (int i=this.LAST_QUESTION_START; i < this.LAST_QUESTION_START + this.NUM_PER_PAGE[pageNum]; i++){
        TextView tv = new TextView(layout.getContext());
        tv.setText(this.QUESTIONS[i]);
        tv.setId(i);
        layout.addView(tv);
    }
}

So it clears all views (which would be only TextView's), figures out the correct array values that will formulate the TextView objects, then adds them to my layout.

Now Page 0 displays correct, when I click my next button, page 1 displays correctly as well. But when I go to page 2, the contents from page 1 are still there, and the contents from page 2 are overlayed on top.

I'm fairly new to Android Development so I'm not sure I'm not refreshing something or invalidating something, but I'm puzzled. Any ideas folks?

Thanks!


Solution

  • Figured out the problem.

    This overlap issue only happens if you have the android:windowBackground set to @null for the application.

    This overlap issue would even happen if you're just setting the text of a textview to the array values. You don't have to actually add views to a layout or anything.

    Moral of the story, set things up properly at the beginning.

    That being said, does anyone have any idea why this would be happening?