user2749364
user2749364

Reputation: 23

Not showing tabs:

I'm new to android programing and need an advanced user to check my work. For some reason when I run this app I get a blank screen instead of the two tabs that i want. Thanks a lot!

MainActivity.java

package com.example.storeitemfinder3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.layout.tab1);
        spec1.setIndicator("Tab 1");

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2");
        spec2.setContent(R.layout.tab2);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost"
    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" />

    <include layout="@layout/tab1" />

    <include layout="@layout/tab2" />

</TabHost>

tab1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/Avocados"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Avocados" />

    <CheckBox
        android:id="@+id/Cashew_Nuts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cashew_Nuts"

        />
 ////android:checked="true"

</LinearLayout>

tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:src="@drawable/tjs" />

</LinearLayout>

Upvotes: 0

Views: 98

Answers (2)

Emmanuel
Emmanuel

Reputation: 13223

This is taken from the docs:

Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page. The individual elements are typically controlled using this container object, rather than setting values on the child elements themselves.

You need to have one FrameLayout as the child of the TabHost

Upvotes: 1

Trikaldarshiii
Trikaldarshiii

Reputation: 11314

May be due to this coz both having full screen so may be hiding Tabs just tried check the layout

tab1.xml

android:layout_height="fill_parent"

tab2.xml

android:layout_height="fill_parent"

So change it to this

tab1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/Avocados"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Avocados" />

    <CheckBox
        android:id="@+id/Cashew_Nuts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cashew_Nuts"

        />
 ////android:checked="true"

</LinearLayout>

tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:src="@drawable/tjs" />

</LinearLayout>

Upvotes: 0

Related Questions