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
and more go here http://developer.android.com/guide/practices/ui_guidelines/icon_design.html#tabstructure
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 :)
Nenhum comentário:
Postar um comentário