Páginas

sexta-feira, 18 de fevereiro de 2011

Android applicaiton with tabs

Hi my first post :D last night I was trying to make a simple application on android with tabs just to learn how to use them. When i search on Google I found something pretty from Google android development site but even following all the steps the application was not ok. I've decided to build these blog not only because I love mobile but because I want to work on these field so I want to show what I can do.
 So let's start.

Installation

I'm not gonna guide you from eclipse installation and configuration if you are a new bee just go here and get stated :) http://developer.android.com/sdk/index.html

Create a project

Create a project named whatever you like :)

in there create three classes ArtistsActivity, AlbumsActivity, and SongsActivity

AlbumsActivity

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AlbumsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView textview = new TextView(this);
textview.setText("This is the Albums tab");
setContentView(textview);
}
}

ArtistsActivity

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ArtistsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView textview = new TextView(this);
        textview.setText("This is the Artits tab");
        setContentView(textview);
    }
}

SongsActivity

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SongsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView textview = new TextView(this);
        textview.setText("This is the Songs tab");
        setContentView(textview);
    }
}







Now that we have our three nice classes let's start with the graphic's. Our tabs can have icon's
my project is using same picture for all tabs but you can use other if you want to know more obout size 
IMPORTANT: I save my images in /res/drawable-hdpi/

Now on same folder /res/drawable-hdpi/ create a xml file containing these:

    
    
    
    


Open the res/layout/main.xml file and insert the following:

    
        
        
    





Now on your project you should have some class that was there from the beginning and it should have same name as the project if you open that class it extends from activity you need to change that to:

extends TabActivity 

now inside you place these code:
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
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ArtistsActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, AlbumsActivity.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                          res.getDrawable(R.drawable.ic_tab_albums))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SongsActivity.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                          res.getDrawable(R.drawable.ic_tab_songs))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTabByTag("albums");

The imports are these:
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;


I think the code is self explanatory if you have any questions just ask.
Now we have to change the AndroidManifest.xml file we need to add the following code before the </application> tag:


        
  
        
          
        

That's all I hope you guys like it :)