I've implemented tab layout it works, but when I add tab swipe. it freeze I can't use the other tabs in the tab layout. Any suggestions.
Here's my code for the tab layout
public class TabLayoutAdmin extends TabActivity {
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayoutadmin);
Resources res= getResources();
TabHost tabHost = getTabHost();
Intent confirmedList = new Intent().setClass(this, Reservations.class);
TabHost.TabSpec tabSpecConfirmedList= tabHost
.newTabSpec("Android")
.setIndicator("", res.getDrawable(R.drawable.stadimumspage))
.setContent(confirmedList);
Intent stadiumsPage = new Intent().setClass(this, StadiumsPage.class);
TabHost.TabSpec tabSpecStadiumsPage = tabHost
.newTabSpec("Apple")
.setIndicator("", res.getDrawable(R.drawable.newreservation))
.setContent(stadiumsPage);
Intent newReservation = new Intent().setClass(this, NewReservation.class);
TabHost.TabSpec tabSpecNewReservation= tabHost
.newTabSpec("Windows")
.setIndicator("", res.getDrawable(R.drawable.confirmedlist))
.setContent(newReservation);
Intent blockTeam = new Intent().setClass(this, Block_Team.class);
TabHost.TabSpec tabSpecBlockTeam = tabHost
.newTabSpec("Berry")
.setIndicator("", res.getDrawable(R.drawable.blockteam))
.setContent(blockTeam);
// add all tabs
tabHost.addTab(tabSpecConfirmedList);
tabHost.addTab(tabSpecStadiumsPage);
tabHost.addTab(tabSpecNewReservation);
tabHost.addTab(tabSpecBlockTeam);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
}
As for tab swipe I used this code
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
Thanks
You should use TabLayout to hold the fragments and ViewPager for swipe, here's how to do it step by step: tablayout with viewpager
Related
I want to add background images for Tabhost which i already added in selector xml. But not sure how to add resource while initiating Tab. Below is tab :
Code to add :
getResources().getDrawable(R.drawable.tabicon)
/* Tabs */
Bundle bundle = getIntent().getExtras();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// First Activity
intent = new Intent().setClass(this, InfoListView.class);
spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
tabHost.addTab(spec);
// Second Activity
intent = new Intent().setClass(this, LogListView.class);
spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;
/* Tabs ends */
Create a TextView , set text to that textView, set background drawable to it, and set this textview as indicator
Somewhat like this:
TextView mTv = new TextView(mContext);
mTv.setText(/*Your-text*/);
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/));
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);
tabHost.addTab(spec);
After your tabhost is intilize with indicator and other things add background to it like this
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab
I have not used the activityGroup in my project. Now I'm not is a position to implement the whole project using Activity group.
is it nesssory that I must implement the activityGroup class in my project to do so?
If yes then please give links for the basic tutorial of activityGroup implementation.
Here is my MainActvity.java which loads 4 other actvities in 4 tabs.
public class MainActivity extends TabActivity {
TabHost tabHost;
Context context = MainActivity.this;
Button btnGo;
TabSpec spec;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btn_GO);
tabHost = getTabHost();
// Android tab
Intent intentHome = new Intent();
intentHome.setClass(this, Home.class);
TabSpec tabSpecHome = tabHost
.newTabSpec("Home")
.setIndicator("Home",
getResources().getDrawable(R.drawable.home))
.setContent(intentHome);
tabHost.addTab(tabSpecHome);
Intent intentNowReading = new Intent().setClass(this, NowReading.class);
TabSpec tabSpecNowReading = tabHost
.newTabSpec("Now Reading")
.setIndicator("Now Reading",
getResources().getDrawable(R.drawable.now_reading))
.setContent(intentNowReading);
tabHost.addTab(tabSpecNowReading);
Intent intentFavourite = new Intent().setClass(this, Favorites.class);
TabSpec tabSpecFavourite = tabHost
.newTabSpec("Favourite")
.setIndicator("Favorites",
getResources().getDrawable(R.drawable.favorites))
.setContent(intentFavourite);
tabHost.addTab(tabSpecFavourite);
Intent intentProfile = new Intent().setClass(this, Profile.class);
TabSpec tabSpecProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile",
getResources().getDrawable(R.drawable.profile))
.setContent(intentProfile);
tabHost.addTab(tabSpecProfile);
tabHost.setCurrentTabByTag("Home");
...}
now I want to start the new activity in the Home tab area on the click event of Go button.(See the picture).
please note that I do not want to impelement the ActivityGroup class, How can I do that without this.
New Actvity must load in the HomeTab's area, not on the full screen.
ActvityGroup is a bad idea, this is old, deprecated API, don't use it.
You have to use Fragments API, just create a Fragment and add it to layout using FragmentTransaction, thats all what you need.
I have a tabhost created by
this.tabHost = getTabHost();
// Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch the first Activity for the tab (to be reused)
intent = new Intent().setClass(this, FirstGroup.class);
// Initialize a TabSpec for the first tab and add it to the TabHost
spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec1);
And I want to change label of tabhost programmatically: "Regionlar" to "newMenuTabbar". I couldn't find any example. thanks for attention.
Edit:
I want to change second tabitem's label from "Mənzərələr"=> "secondTabitem"
intent = new Intent().setClass(this, FirstGroup.class);
// Initialize a TabSpec for the first tab and add it to the TabHost
spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec1);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, SecondActivityGroup.class);
spec2 = tabHost.newTabSpec("SecondActivityGroup").setIndicator("Mənzərələr",
getResources().getDrawable(R.drawable.img_gallery_icon)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec2);
Try this:
final TextView label = (TextView) tabHost.getTabWidget().findViewById(android.R.id.title);
label .setText(YOUR NEW LABEL);
Hope it will help.
b.i 's code can only change the first tab's title.
I think this is a more straight way to get the thing done:
((TextView) mTabHost.getTabWidget().getChildTabViewAt(position)
.findViewById(android.R.id.title))
.setText(yourTitle);
where position is the position of the tab and yourTitle is the title you wish to set for the tab. If you want to change the text for the current tab, then instead of replacing position by getCurrentTab, you can just replace getTabWidget().getChildTabViewAt(position)
by getCurrentTabView()
Whoever wrote this should have defined a setTabText(int position, String text) method, otherwise who would know they have a text view id'ed android.R.id.title? Or if they already had, please enlighten me.
Try this code
public class SlideMainActivity extends TabActivity {
public static RelativeLayout headerLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main_silde_tab);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.hd_header);
setTabs();
}
private void setTabs() {
addTab("FirstGroup", R.drawable.tab_home, FirstGroup.class);
addTab("Regionlar", R.drawable.tab_search, Regionlar.class);
}
private void addTab(String labelId, int drawableId, Class<?> c) {
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
I have two tabs in my tabwidget and each tab display a list view.
ay
Initially my first tab is displayed as selected but below list correspond to second tab.
Once i click the tabs, i am getting correct display.
private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
.setIndicator(LIST_TAB_TAG1)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
.setIndicator(LIST_TAB_TAG2)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
LIST_TAB_TAG1 is highlighted when this sctivity is launched but the list displayed is listview2. This problem is only when activity starts. Upon clicking tabs its working fine
Can please help me in fixing this. Thanks for your time
I generally use the code given in docs which works fine for me ,try this one .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, UpcomingEvents.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this,PastEvents.class);
spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
getResources().getDrawable(R.drawable.bank_transaction1))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
I want to design a page in which i just want to show only one tab. But when i add only one tab, it takes the full width available but i want that tab only takes small space and remaining space is left empty. No other tabs i want to add. Only one tab is displayed all time. Code is following
public class TabViewActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, FirstTab.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("firstTab").setIndicator("First Tab")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
Use tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30; also set the layout_width="wrap_content"
In your xml hide the tab widget by setting the visibility to gone. and instead use a button. And in the button handler you can use code like
public void tabHandler(View target){
artistButton.setSelected(false);
albumButton.setSelected(false);
if(target.getId() == R.id.artist_id){
tabHost.setCurrentTab(0);
artistButton.setSelected(true);
} else if(target.getId() == R.id.album_id){
tabHost.setCurrentTab(1);
albumButton.setSelected(true);
}
}