views:

2357

answers:

4

Hi

I am following this tutorial http://developer.android.com/intl/de/guide/tutorials/views/hello-tabwidget.html and have completed it. Now I would actually like to add you know some controls to these tabs like textboxes(text edit).

How do I do this? I go to my mail.xml using eclipse as my ide and go to layout view and I now get a NullPointerException so I can't even drag stuff onto the layout anymore.

Thanks

Edit

This is what I have

<?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">
        <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">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">            
                <TextView 
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a tab" />
                <EditText android:text="" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:password="true"></EditText>

            </LinearLayout>

            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is another tab" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>
+1  A: 

The layout view in Eclipse can be a bit flaky, particularly with complex layouts. A bit of trial and error might find the View node it is choking on.

As regards developing the tab-based layout further, you have two options, the 'quick' way or the 'right' way. First is to adapt the existing layout xml by replacing one of the TextViews with a LinearLayout (or some other layout) which contains the content you want.

http://google.com/codesearch/p?hl=en#HQNWZ1u2Pig/trunk/HelloLayoutAndroid/res/layout/tab_widget.xml

However Tabs are generally used where there is complex content. For scalability it may be better to locate the TabHost in the layout, call newTabSpec() and then use setContent() to supply an Intent that identifies an internal Activity, which supplies its own Layout.

Jim Blackler
Hi. Thanks I probaby will do the quick way as I don't have much time and this is just a learning exericse to get more familiar to android but I would like to know the right way however what you just said with finding the TabHost in the layout just went over my head as I am been doing android for only like a few hours. Could I see an example? Thanks
chobo2
Hmm I still can't even get the quick way to work.
chobo2
+10  A: 

Tabs are a bit funny to get working initially since there's a lot of code overhead, but once you've worked your way through that they aren't too bad. To get tabs to work, let's start by improving your XML file and then we can make sure your code to actually load them is correct.

First off, your XML file. Instead of including everything directly in your main.xml, you should use the include feature. As the name would suggest, this lets you work on a separate xml file and then include it in your main with one line. This makes the main.xml file much easier to read. So we'd modify your file above to make it look like this:

//No need to change anything above this
<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <include layout="@layout/tab1"/>
    <include layout="@layout/tab2"/>
    //and however many other tabs you want to include

</FrameLayout>

You then need to create tab1.xml, tab2.xml and so forth. These are normal xml files in that they start with a ViewGroup (i.e. LinearLayout, RelativeLayout) which contains any number of other widgets. These widgets can be things like EditTexts, buttons, custom views, whatever you want. The only rule is that the parent ViewGroup (the one at the top) must have a unique ID in it, in the manner of android:id="@+id/someUniqueName". You will use that to refer to that specific layout/tab in your code. So for example, this would be:

tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tab1Layout"
    android:orientation="vertical">

    <TextView ... />
    <EditText ... />
</LinearLayout>

With that done, we can look at your code. I assume you've probably already got this, but just in case here's what you want:

public class YourProject extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources();
        TabHost tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1 title",
                res.getDrawable(R.drawable.logo1)).setContent(R.id.tab1Layout));

        (...)

        //You can also fill tabs with a separate activity like so:
        Intent intent = new Intent(this, YourClass.class);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Another title",
                res.getDrawable(R.drawable.logo2)).setContent(intent));

        tabHost.setCurrentTab(0);
    }
}

As shown above you can set the content of one of the tabs to be a separate activity. In that case, the activity is defined just as any other one with its own Class, layout, etc. Usually you shouldn't do this and instead just use a different View (with setContent(R.id.tabXLayout), but sometimes it's needed. For example if you want one of your tabs to have a list, then you need to start an activity in there that extends ListView, and include all the boilerplate code for ListViews.

I hope that helps!

Steve H
I copied what you had except the Intent part as I don't understand what (YourClass.class) is for. It does not run I am not sure where to even find what the error is as I try to walk through with the debugger and I get all these source not found errors.
chobo2
Ok, nevermind about the part with intents. It's just to let you use another activity inside the main tab-hosting activity. Debugging in Android is a bit different to normal debugging in Eclipse because you're not running a stand-alone program; as far as Eclipse sees, it's running the emulator and the emulator isn't crashing so Eclipse doesn't report anything easy to read. What you need to do is use LogCat. See this answer for an explanation of how to do that: http://stackoverflow.com/questions/2552568/whats-wrong-with-debugging-in-eclipse-on-android/2552801#2552801
Steve H
Hmm still having problems with it. Would it be possible to be sent the whole solution that works so I can run it and try it?
chobo2
That pretty much *is* the whole solution. When you copy/pasted, you did remove the "tab1.xml" right? That was just meant to tell you that that XML belonged in a file called that. What did the LogCat messages say?
Steve H
Hey. Finally got it to work. I just misread one of your steps. I still not sure about what the indent still does and how to set it up.
chobo2
A: 

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1 title", res.getDrawable(R.drawable.logo1)).setContent(R.id.tab1Layout));

regarding "Tab1 title" was wondering would it be able use the string from xml file as well, because i wanted to make the title available for different language as well anyhelp would be appreciated. Thanks in advance.logo1)).setContent(R.id.tab1Layout));Blockquote

Charlie