ASK
ASK

Reputation: 117

Android, Inside a tabhost how to add a button and a listview undernetath?

I want to create something like this

|Button|

item 1

item 2

item 3

. . .Items on a listview

I already have a tabhost with 3 tabs, so i don't want to change my main.xml because the button will appear on every tab! i want my first tab to show a calendar (this one is done, i'm not sure if its ok but it has be done), the second tab will show something diferrent and the last one the button and the item list underneath.

I donnot paste any code because everything i've done comes from android tutorials, so i don't want to ask someone to give me an already written code, just to guide me through what do i have to read and where to look to achieve that!

Thanks in advance!

Well this is what I've done so far this is my main class

`public class HourPayActivity extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an //setContentView(R.layout.emptab); Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MonthsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Months").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_months))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, EmployersActivity.class);
    spec = tabHost.newTabSpec("Employers").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_employers))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, PaymentsActivity.class);
    spec = tabHost.newTabSpec("Payments").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_payments))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

} `

And this is the tab content that i want to show up

public class EmployersActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ListView employersList = getListView();

    String[] employers = getResources().getStringArray(R.array.employers_list);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, employers));
    employersList.setAdapter(getListAdapter());       

    employersList.setTextFilterEnabled(true);



    employersList.setOnItemClickListener(new OnItemClickListener() {
        //@Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_LONG).show();
        }
    });

    setContentView(employersList);

}

Upvotes: 2

Views: 3598

Answers (1)

Joey Roosing
Joey Roosing

Reputation: 2155

What you do is; You follow the android tutorial, which tells you to create an class that extends TabActivity if I remember correctly.

In this activity you load intents via

TabHost.TabSpec spec = tabHost.newTabSpec([title]);
....
Intent content = new Intent(this, activityToLoad.class);
spec.setContent(content);
tabHost.addTab(spec);

What this does is, it loads an activity in a tab. This in turn means that you can use an activity like normal, you can use custom layouts with setContentView(R.layout.yourlayout); in the onCreate() method. You can call your components, attach a custom list adapter to your list and so on. For the 3 tabs create 3 different activities. Based on personal knowledge its the easiest way to maintain your tabs.

What you put in these layout is upto you. But those activities you put in the tabs are "normal" activities like you are (probably) familiar with.

To explain your view:

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
    <Button 
     android:id="@+id/btnId"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/yourtext"
     />
    <ListView 
    android:id="@android:id/android:list"
    android:layout_height="fill_content" 
    android:layout_width="match_parent"
    />
</LinearLayout>

This will place a button at the top with a listview underneath it. The id of the listview is android:list because in my activity i extend listActivity, which expects a listview with that id.

Upvotes: 0

Related Questions