I got some sample code for TabLayout for Android and I modified it to suit my needs.
The thing is that when I tried to re-write it I got some deprecation errors and when i opened the sample no errors appeared.
At first I thought it was the minSdkversion so i changed it in the sample but still nothing happened.
I can’t get the icons appear correctly in the tab so it might have something to do with this deprecation.
Can anyone enlight me? Here is my code:
package com.example.myapp;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost.TabSpec;
//notice that here eclipse won't let me import android.widget.TabHost
//but i use it when i create the tabhost variable
public class TabHost extends TabActivity {
//the 'TabActivity' part above gets crossed-over although that doesn't happen
//in the sample
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host);
android.widget.TabHost tabHost = getTabHost();
TabSpec absSpec = tabHost.newTabSpec("Absences");
absSpec.setIndicator("Absences", getResources().getDrawable(R.drawable.abs));
Intent int1 = new Intent(this, Absences.class);
absSpec.setContent(int1);
tabHost.addTab(absSpec);
}
}
and this is the sample :
package com.example.androidtablayout;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost.TabSpec;
import android.widget.TabHost;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
tabHost.addTab(photospec);
}
}
TabActivity is deprecated, you should use fragments and the compatibly library to implement tabs now. Google seems to be pushing this deprecation quite forcefully, and they have even replaced the documentation page with a “how to migrate” type page.So http://developer.android.com/reference/android/app/TabActivity.html now explains what needs to be done to port your code 🙂