3

I am trying to make a very simple App, but there's an error I can't get rid of it. Maybe someone can help me.

I'm making an Activity with an ActionBar and 3 Tabs. Beneath the Tabs is a FrameLayout, where I put Fragments with a TextView in it. So when clicking on a tab the content of the TextView shoud change. This works fine until I change the orientation. After the change there is a duplicate TextView and I have no idea where it cames from. Here's a picture for better understanding: Overlapping TextViews

Here's my Activity:

package com.test;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.widget.Toast;

public class ProblemActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      ActionBar bar = getActionBar();
      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

      String tab1 = "First Tab";
    bar.addTab(bar
            .newTab()
            .setText(tab1)
            .setTabListener(
                    new TabListener(new First())));

      String tab2 = "Second Tab";
    bar.addTab(bar
            .newTab()
            .setText(tab2)
            .setTabListener(
                    new TabListener(new Second())));

      String tab3 = "Third Tab";
    bar.addTab(bar
            .newTab()
            .setText(tab3)
            .setTabListener(
                    new TabListener(new Third())));
  }

private class TabListener implements ActionBar.TabListener {
    private MyFragment mFragment;

    public TabListener(MyFragment fragment) {
        this.mFragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(R.id.fragment_content, mFragment, null);
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(mFragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(ProblemActivity.this, "Reselected!", Toast.LENGTH_SHORT)
                .show();
    }

}

}

The Fragment's:

public class First extends MyFragment {

public First() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragView = inflater.inflate(R.layout.tab1, container, false);

    TextView tv = (TextView) fragView.findViewById(R.id.textView1);
    tv.setText("First Tab");

    return fragView;
}

}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<FrameLayout
    android:id="@+id/fragment_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

and the content of the Fragment's.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="TextView"
    android:textSize="35dp" />

</LinearLayout>

It would be amazing if some could show me what I'm making wrong. Thank's in advance!

Edit: I tried already this proposed solution but I would like to work with class Objects, so that I can use their Methods.

Edit2: Solved the problem now. It was sufficient to add android:configChanges="keyboardHidden|orientation|screenSize" to my Activity.

Community
  • 1
  • 1
user1173367
  • 51
  • 1
  • 4

2 Answers2

3

Since the OP already solved his problem, but hasn't been active on SO for almost a year, here is an answer containing the solution:


Adding the following to the Activity solved the problem:

android:configChanges="keyboardHidden|orientation|screenSize"
Baz
  • 36,440
  • 11
  • 68
  • 94
0

You can get overlapping views if there is something wrong with one of your layouts. This seems to happen in particular if you have a fragment. For example, a View in the Fragment can overlap a View in the containing layout.

Sometimes this can be fixed by Cleaning your project (if you are using Eclipse).

Martin Ellison
  • 1,043
  • 1
  • 13
  • 25