Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i'm developing my first app, so far so good but stucked with tabs layout change when screen orientation is changed. I've read several posts and questions with similar problem but could not find good explanation and solution. Let me describe my case:
After entering credentials in login activity i enter into tabbed activity...
public class EntryActivity extends FragmentActivity implements
ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3" };
.....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
actionBar.setDisplayOptions(Window.FEATURE_NO_TITLE);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Bundle bundle = getIntent().getExtras();
username = bundle.getString("username");
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
....
}
Activity "entry" is defined with activity_entry.xml layout
<android.support.v4.view.viewpager>
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

Each Tab is defined with own class (Example Tab1)
public class Tab1Fragment extends Fragment {
private TextView twUsername;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_frag1_layout, container, false);
return rootView;
}
}
and corresponding xml file (tab_frag1_layout, tab_frag2_layout, tab_frag3_layout)

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:removed="#fa6a6a" >
<textview>
android:id="@+id/tvFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Top Rated Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>

I have also customized default application style and used "CustomActionBarTheme" settings to create my own tabs
<activity>
android:name=".EntryActivity"
android:label="@string/title_activity_entry"
android:theme="@style/CustomActionBarTheme" >

When i run application in landscape orientation everything is OK, but when i change orientation, the tabs do not preserve their customized style, but take the default one. I have read that with orientation change you destroy and recreate all activities and fragments but how can i solve my problem.
Thanks in advance.


Igor
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900