ActionBarActivity with TabHost - android

I am developing an app, with minimum sdk level 7 and that's why I use support library and ActionBarActivity to have a proper ActionBar, but also the layout of my page contains four tabs on the bottom of the activity, and as i already extend ActionBarActivity, I cannot extend TabActivity.
The problem is when I write
public class HomePageActivity extends ActionBarActivity {
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_homepage);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab1 = mTabHost.newTabSpec("TAB1");
tab1.setIndicator("TAB 1");
Intent popularContentIntent = new Intent().setClass(this, GuideActivity.class);
tab1.setContent(popularContentIntent);
mTabHost.addTab(tab1);
I get the error "Did you forget to call 'public void setup(LocalActivityManager activityGroup)?'" at the line when I trying to add tab to the tabhost.
If I try to set the id of the view in the tab.setContent(..) like tab.setContent(R.id.tab1) and have a view in the xml with the id tab1, I get the error that the view with such a number cannot be found.
My layout:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"
android:layout_alignParentTop="true" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</Button>
</TabWidget>
</RelativeLayout>
Is it possible somehow to have layout with tabs and actionbar at the same time? What I do wrong? What could be other alternatives?
Thanks a lot!

TabActivity was deprecated in API level 13. The updated documentation recommends using android.support.v4.app.FragmentTabHost instead.
An even better method would be to use the ViewPager sample provided here.

Related

tabHost in android gives Unfortunately,app has stopped error

I want two tabs in my application which are in other activity which I access from my main activity from a button.
My Activity which will control the tabs has the code as below
public class Myclass extends TabActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myclass);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstSpec = tabHost.newTabSpec("First");
firstSpec.setIndicator("first Tab",null);
Intent firstintent = new Intent(this,Tab1.class);
firstSpec.setContent(firstintent);
TabSpec secondSpec = tabHost.newTabSpec("Second");
secondSpec.setIndicator("first Tab",null);
Intent secondintent = new Intent(this,Tab2.class);
secondSpec.setContent(secondintent);
tabHost.addTab(firstSpec);
tabHost.addTab(secondSpec);
}
}
And the XML which has the tabHost content has the code
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
So the app is compiling and loading up fine in my device and able to access other activites. But when I try to access the button which should display the activity above mentioned "Myclass" activity. The application exits and I get a screen displaying "Unfortunately,app has stopped".
I have entered the activities in the AndroidManifest.xml also.
The TabActivity shows a cancel mark on the word and says 'android.app.TabActivity' is deprecated.
What does this mean exactly?
Can any one suggest me what is going wrong with the program.
I changed the code as given in the link http://developer.android.com/reference/android/app/TabActivity.html to the code below Code
public class MyClass extends FragmentActivity
{
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myclass);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
Tab1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
Tab2.class, null);
}
}
And the XML CODE to
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
But still no effect, the same problem of application exiting upon clicking of the button to start the activity Myclass continues.
Can anyone suggest a solution?
you should change
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
with
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
try this:
make changes in your XML
Change Tab host id
android:id="#+id/tabHost" to android:id="#android:id/tabhost"
it should be like this :
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">

Facebook slide Menu With MapView on Android

so I am aware of a few workarounds to get the facebook slide menu on my android app which can be through either:
1- FrameLayouts (http://stackoverflow.com/a/8673805/1010114)
2- screenshots (http://stackoverflow.com/a/9768498/1010114)
However, what I want to do, and so far clueless on how to achieve, is to have the facebook slide menu in a MapActivity. This way, user can see and interact with the MapView and has the ability to press the menu button to look at the menu (when the menu appears, it's fine if the user can't interact with the visible part of the map)
Using option number 2 (screenshots) did not work because it seems I can't not take screen shots of a mapview (or at least I can't using his way)
Any hints/ideas on how to do that ?
I had the same issue and solved it by adding a TabHost on the Activity. You can set the MapActivity as the tab content and hide the button for it.
Example code:
map.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="menu" />
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</TabHost>
</LinearLayout>
MyMapActivity.java:
public class MyMapActivity extends MapActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
}
}
MyActivity.java:
public class MyActivity extends TabActivity
{
private LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
inflater = LayoutInflater.from(context);
// Inflate menu here
// Get the TabHost
TabHost tabHost = getTabHost();
// Add the button and content
TabHost.TabSpec spec = tabHost.newTabSpec("myMapTab")
.setIndicator("Map")
.setContent(new Intent(this, MyMapActivity.class));
tabHost.addTab(spec);
// Hide the button
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
MapView mapView = (MapView) tabHost.getTabContentView().getChildAt(0).findViewById(R.id.mapview)
}
}
I hope this solves your problem.

Using Tabs with Views

I have a little problem using Tabs with Views.
First I just copied the sample code where Tabs are used with activitys:
My LayoutFile looks like this:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And this is my Java-code:
public class MyActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.main);
TabHost tH = getTabHost();
Indent intent = new Intent().setClass(this, AnotherActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
//TextView Test = new TextView(this);
//Test.setText("test");
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(intent));
tH.setCurrentTab(0);
}
}
And this works as expected.
But when I uncomment the TextView-lines and call setContent(Test.getId()) instead of setContent(intent), the app crashes.
I also tried to create a textview in the layoutfile, and call setContent(R.id.test),
that also makes it crash.
So this is one problem.
The seccond point is. I do not want to use activitys, because i want to be able to call methods on those classes, which shall represent the Tab-content.
So my original idea is, to derive some classes from view. 1 for each tab, and pass their ids. But therefor the codesample above needs to work first.
greetings Uzaku
I know you said you tried a TextView in the layout file but this should work...
Change the FrameLayout section as follows...
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TEST" />
</FrameLayout>
Then in your code do the following...
tH.addTab(tH.newTabSpec("t1").setIndicator("Tab1").setContent(R.id.test));

TabHost android isn't working correctly

I am trying to get this TabHost to work. When I add the TabHost to the xml layout and run the program I can see the different content for each tab stacked on top of each other. This tells me that the layout is working properly. I then add my spec code to the activity and then if closes the program on the emulator when I navigate to this screen.
So here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/bNewTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Task" />
<TabHost
android:id="#+id/myTabs"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Here now is my Activity code: (The only part that matters...)
welcomeName = (TextView) findViewById(R.id.tWelcome);
Test = (TextView) findViewById(R.id.tTest);
newTask = (Button) findViewById(R.id.bNewTask);
myTask = (ListView) findViewById(R.id.listView);
TabHost th = (TabHost) findViewById(R.id.myTabs);
welcomeName.setText("Welcome " + ToDoActivity.myUser.getName() + "!");
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("All");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Personal");
th.addTab(specs);
This is the Android tutorial page for tabs:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
It has information and requirements about Tabs and it gives lots of sample code. You should be able to copy-paste from there and then edit.
Specifically, I'd change the ids for all the Tab elements to be the suggested Android ids:
android:id/tabhost
android:id/tabs
android:id/tabcontent
A couple possibilities come to mind.
If you're using a TabActivity (not a requirement, but makes things easier), the tabhost must have android:id="#android:id/tabhost" as the id.
I've also seen it said that the tabhost must be the root node. I cannot confirm that using the android docs. I'd probably make this change after all other avenues have failed (or someone confirms that it is true).
If you are using a TabActivity, you don't need to reference the tabhost by id, or use the setup() method. I see you are using the setup() method, but I can;t tell if your activity is a TabActivity. If it is, maybe that's causing your issue.
Following is the method I use for a tab layout setup and it works for me. Note that this assumes the tabhost as the root node of the layout and it has android:id/tabhost as the id and it is a TabActivity.
tabhost code:
public class Tabs extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("tag1").setIndicator("All").setContent(R.id.tab1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tag2").setIndicator("Personal").setContent(R.id.tab2);
tabHost.addTab(spec);
}
}
Hope this helps! (Be sure to click the check mark to accept the answer if it solves your problem).

Android TabHost content not showing

I've created a TabHost and assigned 4 activity intents with tabs and these seems to work just fine. My only problem is that the activity content is not showing within the framelayout #tabcontent in my tabhost view.
I've followed the official reference and scanned the internet for a solution but I can't see what the problem is.
The Log.v("Activity", "Reports") is logged in ant, so it does execute the activity. Therefore I'm guessing its the setContentView() in my ReportsActivity that is causing the problem. But I'm new to this so I can't really tell. (There are no generated errors)
This is my tabhost
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
This is how I add tabs in my TabActivity
// Glob
Intent intent;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Resources res = getResources();
// Reports tab
intent = new Intent().setClass(this, ReportsActivity.class);
spec = tabHost.newTabSpec("reports")
.setIndicator(
res.getText(R.string.reports),
res.getDrawable(R.drawable.reports))
.setContent(intent);
tabHost.addTab(spec);
And this is my content activity (R.layout.reports = linearlayout with a textview)
public class ReportsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reports);
Log.v("Activity", "Reports");
}
}
Try implementing your TabSpec like this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec = tabHost.newTabSpec("reports")
.setIndicator(
res.getText(R.string.reports),
res.getDrawable(R.drawable.reports))
.setContent(intent);
This is because you have chosen a LinearLayout which defaults to Horizontal layout.
If you set android:orientation="vertical" inside the LinearLayout tag that should fix your problem

Categories

Resources