Hi,
Here I want to show you how to make different screen orientation for TabActivity children activities.

You may say “Just specify this in Android Manifest file. That’s All”. But, it didn’t works. All child activities have same orientation with parent (TabActivity).

Below you can download source code of Android project

I will show you how I fix this for me.

  1. Specify the portrait or some else orientation for TabActivity in Manifest file. This will be default orientation for all child activities.
  2. Save reference to TabActivity as static field in TabActivity class
  3. Download the source code below. Pay attantion in onPause and onResume methods in SecondTabActivity class.
The code is so simple 🙂

TabActivitySampleActivity.java

package com.hrupin;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabActivitySampleActivity extends TabActivity {
	
	private static TabActivitySampleActivity INSTANCE;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        INSTANCE = this;
        TabHost host = getTabHost();
        
        TabSpec tab1 = host.newTabSpec("tab1");
        tab1.setIndicator("TAB 1");
        tab1.setContent(new Intent(this, FirstTabActivity.class));
        
        TabSpec tab2 = host.newTabSpec("tab2");
        tab2.setIndicator("TAB 2");
        tab2.setContent(new Intent(this, SecondTabActivity.class));
        
        host.addTab(tab1);
        host.addTab(tab2);
    }
    
    public static TabActivitySampleActivity getInstance(){
    	return INSTANCE;
    }
}

FirstTabActivity.java

package com.hrupin;

import android.app.Activity;
import android.os.Bundle;

public class FirstTabActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);
	}
}

SecondTabActivity.java

package com.hrupin;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class SecondTabActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		TabActivitySampleActivity.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		TabActivitySampleActivity.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
}

Here  you can download the source code of Android project

Download it from github

2 Comments

matej smitala · 18 June, 2012 at 19:18

This is not clean solution. Furthemore, it does not even work…

Igor · 26 June, 2012 at 15:16

Hi, matej smitala

I’ve just check my code and check application on device. It works fine.
What kind of problem do you have?
I can help you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.