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" />
Related
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">
I am having a problem with a list view that will only show one row at a time even though there are many rows.
The list view is included within a tab so the problem could be there rather than in the list view.
I first tried defining the list view in a separate layout file and using that when creating the tab. When that didn't work I put it into the FrameLayout with the tab definition.
Same result.
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_height="fill_parent" android:layout_width="wrap_content"></TabWidget>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ListView android:id="#+id/list1" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView></FrameLayout>
</LinearLayout>
</TabHost>
This is the code I use to create and add the tab:
protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
TabSpec tabSpec = tabHost.newTabSpec(tabName);
tabSpec.setIndicator(tabName);
tabSpec.setContent(tabFactory);
tabHost.addTab(tabSpec);
tabs.add(new TabDetails(tabName,tabFactory));
}
I call it with:
addTab("Medical Center Tests",this);
and include this code for creating the tab contents:
#Override
public View createTabContent(String tag) {
// Get the data returned from the servelet and display it in the ListView
data = getDataArray(this.getIntent().getExtras());
ListView lv = (ListView) contentView.findViewById(R.id.list1);
lv.setAdapter(new MyMedicalCentersTestsScreenAdapter(this,lv,data));
return lv;
}
The data is all there. It's just the the height of the list view is equal to the height of one row.
EDIT:
I added some debug commands to see what position the getView requests. It only requests the first row and then requests them one at a time as I scroll.
So, it is definitely seeing it as a small listview and scrolling them accordingly.
Edit 2:
Decided to try an experiment.
Returned a simple list for the list view.
ListView lv = (ListView) contentView.findViewById(R.id.list1);
// create some dummy strings to add to the list
List<String> list1Strings = new ArrayList<String>();
list1Strings.add("Item 1");
list1Strings.add("Item 2");
list1Strings.add("Item 3");
list1Strings.add("Item 4");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
Still the same. Only one row at a time.
you define height of layout rather than using fill_parent...
code:
layout_height ="150dp"
try doing this
<TabWidget android:id="#android:id/tabs"
android:layout_height="fill_parent" android:layout_width="fill_parent"></TabWidget>
instead of
<TabWidget android:id="#android:id/tabs"
android:layout_height="fill_parent" android:layout_width="wrap_content"></TabWidget>
android:layout_height="wrap_content" or android:layout_height="100dp"
instead of this
android:layout_height="fill_parent"
Your layout is a little weird cause the tabWidget (it displays a list of tab labels representing each page in the parent's tab collection) height is set to fill_parent. How about something like this?
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost">
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="84dp" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
For tabs:
tabhost = (TabHost) findViewById(android.R.id.tabhost);
tabhost.setup();
TabSpec ts = tabhost.newTabSpec("tag1");
ts.setContent(R.id.tab1);
ts.setIndicator("TAB1");
tabhost.addTab(ts);
ts = tabhost.newTabSpec("tag2");
ts.setContent(R.id.tab2);
ts.setIndicator("TAB2");
tabhost.addTab(ts);
You add tabs in that way 'cause you add them dynamically?
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));
I am working on a Android project. In my project I am using tab layout. It works fine but all tabs background is black and when I am selecting any tab ,that tabs background changes to light brown.Is it possible to give custom color?
my xml file is
<?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:id="#+id/LinearLayout01"
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>
my java code is
Resources res = getResources();
TabHost tb=getTabHost();
Intent i;
TabHost.TabSpec spec;
spec = tb.newTabSpec("mee").setIndicator ("home").setContent(i);
tb.addTab(spec);
spec=tb.newTabSpec("contacts").setIndicator("search").setContent(i);
tb.addTab(spec);
Sure :)
You should define and use styles.
I suggest you to read here - http://developer.android.com/guide/topics/ui/themes.html
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.