• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Working With Tabs

doeiqts

Lurker
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:
package doeiqts.unicorn;

import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;

public class Unicorn extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        
        //tab1
        spec = tabHost.newTabSpec("one").setIndicator("One",
                res.getDrawable(R.drawable.tab_mycorns))
            .setContent(R.layout.corn_changer);

        tabHost.addTab(spec);
        
        //tab2
        spec = tabHost.newTabSpec("two").setIndicator("Two",
                res.getDrawable(R.drawable.tab_horn))
            .setContent(R.layout.corn_changer);

        tabHost.addTab(spec);
        
        tabHost.setCurrentTab(2);
        
        
            
        // Capture our button from layout
        Button button = (Button)findViewById(R.id.corn_changer_button);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(myListener);
    }
    
    
    private OnClickListener myListener = new OnClickListener() {
    	int counter = 1;
    	
    	public void onClick(View v) {        	        	
        	if(counter == 1)
        	{
        		ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
        		img.setImageResource(R.drawable.unicorn001);
        		counter++;
        	}
        	else if(counter == 2)
        	{
        		ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
        		img.setImageResource(R.drawable.unicorn002);
        		counter++;
        	}
        	else if(counter == 3)
        	{
        		ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
        		img.setImageResource(R.drawable.unicorn003);
        		counter++;
        	}
        	else
        	{
        		ImageView img = (ImageView) findViewById(R.id.corn_changer_image);
        		img.setImageResource(R.drawable.unicorn);
        		counter = 1;
        	}        	
        }
    };
    
}

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.
 
Back
Top Bottom