tags:

views:

369

answers:

1

Hi,

I never designed UIs (more of a middleware guy) before so I apologize if the question is stupid. I am designing a UI to look something like the following:


ImageView ImageView

TabHost

Tab 0 ------ Tab 1 ------ Tab 2

-----INSIDE EACH TAB-----

TextView

ListView

- Consists of ImageView TextView


The problem is I think I am following a very inefficient way of doing the whole stuff. The onCreate method is as follows:

CODE:

public void onCreate(Bundle savedInstanceState) { 

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

          TabHost mTabHost = getTabHost(); 

          Drawable Tab1Icon = getResources().getDrawable(R.drawable.tab1_icon); 
          Drawable Tab2Icon = getResources().getDrawable(R.drawable.tab2_icon); 
          Drawable Tab3Icon = getResources().getDrawable(R.drawable.tab3_icon); 

          mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Tab 1", Tab1Icon).setContent(new Intent(this, Tab1.class))); 
          mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Tab 2", Tab1Icon).setContent(new Intent(this, Tab2.class))); 
          mTabHost.addTab(mTabHost.newTabSpec("Tab3").setIndicator("Tab 3", Tab1Icon).setContent(new Intent(this, Tab3.class))); 

          mTabHost.setCurrentTab(0); 
     }

This program crashes on CupCake (v1.5) complaining about StackOverflowException but runs well on Donut (v1.6). This is my main.xml:

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

    <TableLayout 
               android:id="@+id/TableLayout01" 
               android:layout_width="fill_parent" 
               android:layout_height="fill_parent" 
               android:stretchColumns="0" 
               android:background="@color/application_background"> 

          <TableRow> 
               <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              android:orientation="horizontal" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"> 
               <ImageView id="@+id/picview1" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:src="@drawable/leftlogo" 
                  android:paddingRight="105sp" 
                /> 
                <Button 
                    android:id="@+id/picview2" 
                   android:layout_width="wrap_content" 
                   android:layout_height="wrap_content" 
                   android:background="@drawable/rightlogo" 
                   /> 
          </LinearLayout> 
          </TableRow> 

          <TableRow> 
               <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"> 

                    </FrameLayout> 
                   </LinearLayout> 
               </TabHost> 
          </TableRow> 

     </TableLayout> 
</LinearLayout>

Any suggestions regarding the design please?

Thanks

+2  A: 
  1. If you are getting StackOverflowExceptions, your UI is too complex. Fire up hierarchyviewer and find ways to remove some layers.
  2. One way to simplify everything and remove layers is to supply the tab contents as Views, not Intents pointing to Activities.
  3. You can just pass the R.drawable ID of the icon to setIndicator().
  4. You should not have the TableLayout inside a LinearLayout (why bother with the LinearLayout?) or a LinearLayout inside of a TableRow (TableRow is a LinearLayout, for all intents and purposes). Please consider dumping the entire TableLayout/TableRow/LinearLayout stuff and just use a single RelativeLayout.
CommonsWare
1. The hierarchyviewer scared me like crazy so I thought of asking this question :)2. If you don't mind, could you give me a small example on what you mean?3. Will do that...4. Sure. I will remove that right away...Thanks a lot...
Legend
1. Aw, now you went and hurt hierarchyviewer's feelings... ;-) 2. Uh, you call setContent() and pass a View (e.g., a RelativeLayout), perhaps one you inflated from a layout file.
CommonsWare
Haha... Actually I came across that after watching the optimization video from Google I/O conference. It looked like a small tool but it managed to successfully scare me lol.. Thanks for the input on the setContent call...
Legend
So as I keep optimizing (removing more and more layers of layouts), I see that the numbers shown in measure, layout, and draw inside the hierarchy view tool are changing. Sometimes the measure number is going down with some removals and its going up with some other... Do you have idea why this happening? Thanks
Legend
That's going to be dependent on what else is going on with your emulator/device at the time that widget (tree) was drawn. I would use those figures as only a rough guide.
CommonsWare
I'm sure I ran the test three times and it gave me the same numbers... I was just paranoid because of one of those Google I/O videos where (I think) Romain Guy pointed out this tool in the first place saying that it will keep me on my tracks... :) But it seems to be pushing me more and more towards optimization but I seem to be ruining it somehow... I converted 4 textviews into one textview with a drawableLeft icon and still the timing goes up... Not really sure why this is happening though...
Legend
Slightly off topic: In your code sample, you allocate 3 TabIcons but use only the first one... I know this has nothing to do with Stack depth (and probably it's just a typo anyway), but it's a waste of resources.
Henning
Oops... curse of the copy/paste daemon :) Thanks for that... I just fixed it...
Legend