Skip to content Skip to sidebar Skip to footer

Trying To Generate Tabs And Their Views Dynamically

Update: The compile error is resolved and I found some things I didn't do right orgianlly based off the example I was following and corrected them. However when I try to run the co

Solution 1:

I figured out the issue after much trial and error and googling an extensive ammount of examples.

First I gave up attempting to create the tab widget and the frame layout through java instead of xml for the activity..

In my activities layout xml file I added a tab control and in the xml view deleted the tabs leaving the xml code looking like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

        <LinearLayout
        android:id="@+id/MainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

Then in the java file for the activity I updated the oncreate routine to be this.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Create the tabs
TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " +
    dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
    " = d." + dbHelper.Attribute_Device_ID + ";";
Cursor c = db.rawQuery(dbQuery, null);
int Count = 0;
if (c.getCount() > 0)
{
    while (c.moveToNext())
    {
        Count = Count + 1;
        int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        final String DeviceType = c.getString(iColumnDeviceType);
        final String DeviceName = c.getString(iColumnDeviceName);
        final String DeviceNameLabel = DeviceName.replaceAll(" ", "");
        Log.d("Creating Tab", "TabSpec: tab" + DeviceNameLabel);
        Log.d("Creating Tab", "Indicator: " + DeviceName);
        final TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
        NewTab.setIndicator(DeviceName);
        NewTab.setContent(new TabHost.TabContentFactory()
        {
            @Override
            public View createTabContent(String tag)
            {
                LinearLayout ll = new LinearLayout(MainActivity.this);
                LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                ll.setLayoutParams(params);
                ll.setOrientation(LinearLayout.VERTICAL);
                if (DeviceType.equals("Receiver"))
                {
                    final Button btnVolumeUp = new Button(MainActivity.this);
                    btnVolumeUp.setText("Volume Up");
                    btnVolumeUp.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnVolumeUp.setGravity(Gravity.LEFT);
                    ll.addView(btnVolumeUp);
                }
                else if (DeviceType.equals("Video Player"))
                {
                    final Button btnPlay = new Button(MainActivity.this);
                    btnPlay.setText("Play");
                    btnPlay.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnPlay.setGravity(Gravity.LEFT);
                    ll.addView(btnPlay);
                }
                return ll;
            }
        });

        Tabs.addTab(NewTab);

    }
}

Post a Comment for "Trying To Generate Tabs And Their Views Dynamically"