tags:

views:

75

answers:

4

I would like to define each tab's content in a different XML file so the visual editor in Eclipse can be used. That is, without all of the tabs being laid over each other. The visual editor doesn't even seem to work with the XML example provided here: [link text][1] [1]: http://developer.android.com/guide/tutorials/views/hello-tabwidget.html "Here", It just has a Null pointer exception.

I have tried to define each tap in it's own file, for example:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>

And when adding the tab I use:

mTabHost.addTab(mTabHost.newTabSpec("tab_in").setIndicator("Input").setContent(R.layout.input));

But this causes the application to crash on launch. Is there any way to do this?

Thanks

A: 

Hi,

It's much more convenient if you write a TabContentFactory, or have a separate Activity for each tab.

ognian
I may be misunderstanding TabContentFactory's description, but it looks like it generates content from code, not an XML file. I am trying to avoid making each tab a separate activity because they need to "talk" with each other quite a bit. Right now it looks like my best bet is to edit the layout in a separate file and copy it to the tab's XML file to test.
Matt
TabContentFactory is returning a View, so you can (and it's best to) use View.inflate() inside it to load an XML layout.
ognian
A: 

I think what you're doing would work if you had valid XML.

The TextView is giving you those errors because of the android:text="@+id/TextView01" part. Change that to android:text="Some literal string" and it should work (or android:text="@strings/tabtextview01" assuming a strings.xml file with that string).

Your XML should be:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView android:id="@+id/TextView01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@strings/tabTextView01" /><!--
or android:text="The string literal to display." -->
</RelativeLayout>
kiswa
android:text="@+id/TextView01" is the default text when you add a TextView via the visual editor. It displays as "false" when you run the application if you don't set it with the code. I did try modifying the string anyway and it still crashes.
Matt
I don't have access to the SDK at the moment. I'll try to get something working when I get home.
kiswa
Nevermind, I think Francesco's answer is probably what you're looking for. If not, I'll come back and get something working for you.
kiswa
A: 

you might also look at using the TabHost, which allows you to have each tab be an 'activity'

Ben
+1  A: 

Just my 2cents. I had lot of problems using an Intent based tabhost. I ran into really strange behaviours expecially with ListView widget. I was able to rid all of my problems when I started to rewrite my project into a View based tabhost instead of using separated Intent/activities

You can always have your separated XML per Tab

In short this is what I did:

public class TabOneView implements TabContentFactory {

    private LayoutInflater mInflater;

    public TabOneView(Context context){
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public View createTabContent(String arg0) {

        View v = mInflater.inflate(R.layout.tab_one, null);

        /**
         * Add your code here for buttons/list etc
         * An object lookup for your Button: 
         */
        Button b = (Button)v.findViewById(R.id.btn01);

        b.setOnClickListener(mListener);
        //...more code
        return v;
    }

}
Francesco
Thanks, that worked perfectly! Just to be clear, you are saying that you get strange behavior when you use a separate activity for each tab? Interesting...
Matt
I get best results when using separate Activity for each tab when tabs are the only thing on screen, no matter of the complexity. Activities are well designed and implemented and pretty much take care of their lifecycle.
ognian