Android orientation change removes actionbar tab custom view

164 Views Asked by At

I have created ActionBar tabs with custom views. I am using a custom view because I want to show unread badge counts inside the tabs. When the activity is created, they appear just fine.

enter image description here

When I change the orientation of the device, the selected tab's custom view is removed from the tab.

enter image description here

This does not occur when I am using a tablet, just a phone. Does anyone know what could be causing this?

Here is the custom view I am using

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

    <ImageView
        android:id="@+id/ivTabIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_action_messages" />

    <TextView
        android:id="@+id/tvBadgeCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/ivTabIcon"
        android:layout_alignTop="@id/ivTabIcon"
        android:layout_marginRight="0dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/background_message_notification"
        android:gravity="center"
        android:minWidth="17sp"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:text="@null"
        android:textColor="@color/white"
        android:textSize="12sp" />

</RelativeLayout>

Here is the code when I inflate the custom views

RelativeLayout messageView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_message, null);
ImageView messageIcon = (ImageView) inboxView.findViewById(R.id.ivTabIcon);
messageIcon.setImageResource(R.drawable.ic_action_messages);
TextView tabBadgeInbox = (TextView) inboxView.findViewById(R.id.tvBadgeCount);
actionBar.newTab().setCustomView(messageView);

UPDATE Here is the screen shot of the ActionBar when the activity is created

enter image description here

And when I change orientation here is what the ActionBar looks like

enter image description here

Now if I create the Activity in landscape mode, the ActionBar loads properly

enter image description here

0

There are 0 best solutions below