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.

<RelativeLayoutxmlns: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" ><TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true" ><LinearLayoutandroid:id="@+id/MainLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content" ></TabWidget><FrameLayoutandroid: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 tabsTabHostTabs= (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
DatabaseHelperdbHelper=newDatabaseHelper(this);
SQLiteDatabasedb= dbHelper.getWritableDatabase();
StringdbQuery="SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " +
    dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
    " = d." + dbHelper.Attribute_Device_ID + ";";
Cursorc= db.rawQuery(dbQuery, null);
intCount=0;
if (c.getCount() > 0)
{
    while (c.moveToNext())
    {
        Count = Count + 1;
        intiColumnDeviceType= c.getColumnIndex(dbHelper.Attribute_Device_Type);
        intiColumnDeviceName= c.getColumnIndex(dbHelper.Attribute_Device_Name);
        finalStringDeviceType= c.getString(iColumnDeviceType);
        finalStringDeviceName= c.getString(iColumnDeviceName);
        finalStringDeviceNameLabel= DeviceName.replaceAll(" ", "");
        Log.d("Creating Tab", "TabSpec: tab" + DeviceNameLabel);
        Log.d("Creating Tab", "Indicator: " + DeviceName);
        finalTabSpecNewTab= Tabs.newTabSpec("tab" + DeviceNameLabel);
        NewTab.setIndicator(DeviceName);
        NewTab.setContent(newTabHost.TabContentFactory()
        {
            @Overridepublic View createTabContent(String tag)
            {
                LinearLayoutll=newLinearLayout(MainActivity.this);
                LayoutParamsparams=newLinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                ll.setLayoutParams(params);
                ll.setOrientation(LinearLayout.VERTICAL);
                if (DeviceType.equals("Receiver"))
                {
                    finalButtonbtnVolumeUp=newButton(MainActivity.this);
                    btnVolumeUp.setText("Volume Up");
                    btnVolumeUp.setLayoutParams(newLayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnVolumeUp.setGravity(Gravity.LEFT);
                    ll.addView(btnVolumeUp);
                }
                elseif (DeviceType.equals("Video Player"))
                {
                    finalButtonbtnPlay=newButton(MainActivity.this);
                    btnPlay.setText("Play");
                    btnPlay.setLayoutParams(newLayoutParams(
                        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"