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">
Related
I am trying to create an app that starts with an activity A with a "custom" layout. From that activity it is possible to enter activity B with 3 tabs.
I succeed at creating activity with tabs. The problem is that tabs shows up only if that activity is also launcher activity. If it is not, the tabs do not shows up.
At first time i tried to create tabs with TabLayout. Now i tried to create tabs with TabHost.
Can someone pleas help?
onCreate method in main activity where tabs are "created".
public class GlavnaStran extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavna_stran);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("Search");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Lyric");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Playlist");
//tab1.setIndicator("Tab1");
tab1.setIndicator("Search");
tab1.setContent(new Intent(this, SearchTab.class));
//tab2.setIndicator("Tab2");
tab2.setIndicator("Lyric");
tab2.setContent(new Intent(this, LyricTab.class));
//tab3.setIndicator("Tab3");
tab3.setIndicator("Playlist");
tab3.setContent(new Intent(this, PlaylistTab.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
For each tab i was created separate activity which looks like that:
public class SearchTab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_tab);
}
}
The xml part of the project is created by importing TabHost container in design view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.admin.besedilatakt_v5.GlavnaStran">
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<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"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
In the XML files of tab's activity is nothing special except TextView widget to display some random text.
i'm using Listview on Tab Layout in my android app to load data from network. Tha tabs are working properly and i can switch between my tabs easily without any error but when the listview is populated with data from server the tabs go away and i've a simple listview with data. I can't see my other tabs anymore.
PS The data is loading just fine without any problem.
Here is the Home java code for home_layout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
TabHost tabHost = getTabHost();
TabHost.TabSpec myprofiletab = tabHost.newTabSpec("Profile");
myprofiletab.setIndicator("Profile",getResources().getDrawable(R.drawable.myprofile_icon));
Intent myprofileintent = new Intent(this,MyProfile.class);
myprofiletab.setContent(myprofileintent);
TabHost.TabSpec newsfeedtab = tabHost.newTabSpec("NewsFeed");
newsfeedtab.setIndicator("Feed",getResources().getDrawable(R.drawable.newsfeed_icon));
Intent feed = new Intent(this,NewsFeed.class);
newsfeedtab.setContent(feed);
tabHost.addTab(newsfeedtab);
tabHost.addTab(myprofiletab);
}
Here is the home_layout
<?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_alignParentBottom="true"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
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"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
Solved my problem by adding one line to my FrameLayout
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs" />
I have developed a simple Android application where in i have implemented tabs using Fragments... but i am unable to display the images for the tabs.. i have attached the source code below.. please let me know what is the error...
My Activity class TabDemoFragmentActivity.java
public class TabDemoFragmentActivity extends FragmentActivity {
/**
* Instance variable of type {#link FragmentTabHost}
*/
private FragmentTabHost fragmentTabHost;
/**
* This is a call back method, which gets invoked
* when the instance of this class is created.
*
* <p>
* This method is used to set the tab host and
* add the tab host fragments to the screen,
* which acts as a UI.
* </P>
*/
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_fragment_main);
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("DropCall Details").setIndicator("DropCall Details",
getResources().getDrawable(R.drawable.drop_call_image)).setContent(intent), QoSDropCallDetailsFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Network Details").setIndicator("Network Details",
getResources().getDrawable(R.drawable.network_details)), QoSNetworkDetailsFragment.class, null);
}
}
My XMl file
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Image is not getting displayed here.. please let me know how to display images in the tabs.. Thanks
I realize it is an old question but hoping someone might land on this page. First, let me point out that FragmentTabHost is deprecated approach to adding tabs in your app. Google wants you to move to ActionTabBar to add tabs.
I had done a very similar implementation to yours and I don't see much difference. However, 2 things I can point out from your code is first, you need to make sure you copy your image to all 3 drawable folders. Second, remove the extra Framelayout(tabcontent) you have in your xml because I only had 1 Framelayout and I don't see you using the other one in your Activity class.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
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"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Here is a snippet of my Activity Class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1",
getResources().getDrawable(R.drawable.user_card)),
TabActivity.ListFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(R.drawable.menu)),
TabActivity.ListFragments.class, null);
}
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));
Hey... i'm trying to create an activity with the layout structure like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost android:id="#+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"
>
<TabWidget android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
<some code here>
</LinearLayout>
What is wrong here? I'm getting nullPointerException in my activity
public class TabsActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
// Resources res = getResources();
// TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
}
}
The problem is with nesting. There is no problem with the TabHost as the main XML node.
Thx!
Error:
You are misinterpreting your stack trace.
The exception is occurring inside Intent. The Intent you are using to start the activity is invalid. Fix your Intent, and your problem will go away.
I had a similar problem and the solution was to get the TabHost and call setup(). Hard to say if that's your problem here.