Working With Tabs


Last Updated:

  1. doeiqts

    doeiqts New Member This Topic's Starter

    Joined:
    Sep 18, 2010
    Messages:
    1
    Likes Received:
    0
    New and trying to figure out the basics. Not sure what all code needs to be quoted to make sure it's right, but here's the main .java file that I'm working with...

    Code (Text):
    1. package doeiqts.unicorn;
    2.  
    3. import android.app.TabActivity;
    4. import android.content.res.Resources;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.view.View.OnClickListener;
    8. import android.widget.Button;
    9. import android.widget.ImageView;
    10. import android.widget.TabHost;
    11.  
    12. public class Unicorn extends TabActivity {
    13.     /** Called when the activity is first created. */
    14.     @Override
    15.     public void onCreate(Bundle savedInstanceState) {
    16.         super.onCreate(savedInstanceState);
    17.         setContentView(R.layout.main);
    18.        
    19.        
    20.         Resources res = getResources(); // Resource object to get Drawables
    21.         TabHost tabHost = getTabHost();  // The activity TabHost
    22.         TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    23.        
    24.         //tab1
    25.         spec = tabHost.newTabSpec("one").setIndicator("One",
    26.                 res.getDrawable(R.drawable.tab_mycorns))
    27.             .setContent(R.layout.corn_changer);
    28.  
    29.         tabHost.addTab(spec);
    30.        
    31.         //tab2
    32.         spec = tabHost.newTabSpec("two").setIndicator("Two",
    33.                 res.getDrawable(R.drawable.tab_horn))
    34.             .setContent(R.layout.corn_changer);
    35.  
    36.         tabHost.addTab(spec);
    37.        
    38.         tabHost.setCurrentTab(2);
    39.        
    40.        
    41.            
    42.         // Capture our button from layout
    43.         Button button = (Button)findViewById(R.id.corn_changer_button);
    44.         // Register the onClick listener with the implementation above
    45.         button.setOnClickListener(myListener);
    46.     }
    47.    
    48.    
    49.     private OnClickListener myListener = new OnClickListener() {
    50.         int counter = 1;
    51.        
    52.         public void onClick(View v) {                      
    53.             if(counter == 1)
    54.             {
    55.                 ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
    56.                 img.setImageResource(R.drawable.unicorn001);
    57.                 counter++;
    58.             }
    59.             else if(counter == 2)
    60.             {
    61.                 ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
    62.                 img.setImageResource(R.drawable.unicorn002);
    63.                 counter++;
    64.             }
    65.             else if(counter == 3)
    66.             {
    67.                 ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
    68.                 img.setImageResource(R.drawable.unicorn003);
    69.                 counter++;
    70.             }
    71.             else
    72.             {
    73.                 ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
    74.                 img.setImageResource(R.drawable.unicorn);
    75.                 counter = 1;
    76.             }          
    77.         }
    78.     };
    79.    
    80. }
    When I run it to test it it just Force Closes. As you may recognize, this is a slightly modified version of the Android Developer Tab Layout guide. I wanted to use just an xml view rather than a whole new activity for each tab.

    I realize both tabs will have the same content, that's fine, I just want to get them to work at all right now. If you need the tab_[name] xml files or the main or the corn_changer let me know.

    I'm getting no compile errors in Eclipse but when running it will just FC. To me everything looks right but I'm probably just trying to do something I can't actually do.

    Worth noting that the corn_changer.xml works just fine if that's called instead of main.xml and it's switched back to Activity rather than TabActivity. So, to me, means that the content inside the tabs should be working fine.
     

    Advertisement

Share This Page

Loading...