Why wont my regular Tabhost work w. ActionBarSherlock? - android

I am pulling my hair out trying to get a simple tabhost to work with ABS, it seems Android only complicates things more when attempting to "improve" them. I was considering converting my project to use fragemnts for tabs , but that proved to be wayyyyyyyyy to much work for the complex layouts im using in each of my tabs. Id settle now for just a plain ol working tabhost w. working abs bar.
package com.abs.tabs.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.buhz.feeds.Buhdz;
import com.buhz.login.R;
import com.buhzhyve.localz.Localz;
import com.buhzhyve.trails.Trails;
import com.buhzhyve.trendz.Trendz;
public class FragmentTabs extends SherlockActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
actionBar=getSupportActionBar();
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost =(TabHost) findViewById(android.R.id.tabhost); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Do the same for the other tabs
intent = new Intent().setClass(this, Buhdz.class);
spec = tabHost.newTabSpec("Buhdz").setIndicator("Feeds",
res.getDrawable(R.drawable.buhdz_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Localz.class);
spec = tabHost.newTabSpec("Localz").setIndicator("Locals",
res.getDrawable(R.drawable.locals_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Trendz.class);
spec = tabHost.newTabSpec("Trendz").setIndicator("Trends",
res.getDrawable(R.drawable.trends_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Localz.class);
spec = tabHost.newTabSpec("convos").setIndicator("Convos",
res.getDrawable(R.drawable.convos_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Trails.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Trails").setIndicator("Trails",
res.getDrawable(R.drawable.trails_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(4);
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
return true;
case R.id.subItem1:
Toast.makeText(this, "Sub Menu item 1 tapped", Toast.LENGTH_SHORT).show();
return true;
case R.id.subItem2:
Toast.makeText(this, "Sub Menu item 2 tapped", Toast.LENGTH_SHORT).show();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}

Use this library: https://github.com/JakeWharton/Android-ViewPagerIndicator, you can find inside this library some samples with tabs like you want and works with sherlockactionbar.
Visit this question: Actionbarsherlock + tabs + multi fragments?.
Ask me if you got in trouble.

Related

onClick event in android tabwidget

i am working with tabview. i have 3 tabs in my tabwidget view. Below is my MainActivity.java
public class MainActivity extends Activity {
TabHost host;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
host = (TabHost) findViewById (R.id.tabhost);
host.setup();
TabSpec tspecMovies = host.newTabSpec("tag1");
// tspecMovies.setContent(R.id.tab1);
tspecMovies.setIndicator("Movies", res.getDrawable(R.drawable.movie_icon));
host.addTab(tspecMovies);
TabSpec tspecTv = host.newTabSpec("tag2");
// tspecTv.setContent(R.id.tab2);
tspecTv.setIndicator("TV", res.getDrawable(R.drawable.tv_icon));
host.addTab(tspecTv);
TabSpec tspecEvents = host.newTabSpec("tag3");
// tspecEvents.setContent(R.id.tab3);
tspecEvents.setIndicator("Events", res.getDrawable(R.drawable.event_icon));
host.addTab(tspecEvents);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have 3 different Activities for each tabs which contains the tab contents. I want to know how do i call these activities on onClick event when the tab is clicked.
find below code for that.
host.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if(getString(R.string.tab1).equals(tabId)){
//your activity 1
// R.string.tab1 is the Id given to tab using values/string.xml file
}else if(getString(R.string.tab2).equals(tabId)){
// your activity 2
}
}
}
You have to set intent to each tab..
use this code
// Android tab //activity which you want to call
Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
For more info Complete Tutorial
You have to pass tabspect with intent
TabSpec tspecMovies = host.newTabSpec("tag1");
// tspecMovies.setContent(R.id.tab1);
tspecMovies.setIndicator("Movies", res.getDrawable(R.drawable.movie_icon));
host.addTab(tspecMovies)
Intent moviesintent=new Intent(this, MoviesActivity.class);
moviesintent.setContent(videosIntent);
You can refer this tutorial.
I hope You can learn very well.

display a context menu when clicking on tabBar

I have a weird requirment to accomplish. Let say I have a tabHost with 3 tabs. And let say the second tab is curently displayed. What I need to do is - when clicking on the third tab to display a context menu. The context menu needs to be displayed in the activity from tab 2.
IMPORTANT So, when I click on the third tab I must not go to another activity, I must stay at the current activity(from tab 2) and display a context menu also.
Hope I made myself clear. Thank you!
simple demo example as given at below check it make some changes according to ur requirement
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class Tab_exActivity 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
// this.myhost = (TabHost)this.findViewById(R.);
// this.myhost.setup();
final 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, tabactivity1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Home",
res.getDrawable(R.drawable.act1))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, tabactivity2.class);
spec = tabHost.newTabSpec("albums").setIndicator("Refresh",
res.getDrawable(R.drawable.act2))
.setContent(intent);
tabHost.addTab(spec);
// intent = new Intent().setClass(this, tabactivity3.class);
spec = tabHost.newTabSpec("songs").setIndicator("Search",
res.getDrawable(R.drawable.act3))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
if (tabId.equals("songs")){
System.out.println("44444");
View v = tabHost.getCurrentView();
registerForContextMenu(v);
v.showContextMenu();
}
}});
// openContextMenu(spec);
// tabHost.setCurrentTab(2);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
}

android tabhost tab and view not matching

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);
}

How to change title of a Tab in TabActivity

I have TabActivity with tabs on bottom which is composed of several different activities like this:
public class HomeScreen extends TabActivity {
private TabHost mTabHost;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try{
setContentView(R.layout.hometabs);
Resources res = getResources(); // Resource object to get Drawables
mTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String strName;
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Books);
intent = new Intent().setClass(this, BookList.class);
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_database))
.setContent(intent);
mTabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Questions);
intent = new Intent().setClass(this, QuestionsList.class);
intent.putExtra("NoteType", UserDataSQLHelper.NOTE_TYPE_QUEST );
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_dialog))
.setContent(intent);
mTabHost.addTab(spec);
My problem is that I have the same window title for every tab. This window title is set to application name.
I need to be able to change this title to the name of the tab.
I am trying to do it by calling setTitle("MyTitle"); in onCreate() of corresponding activity and by overriding TabActivity onChildTitleChanged:
public void onChildTitleChanged(Activity childActivity, CharSequence title)
{
View tabView = mTabHost.getCurrentTabView();
int idTitle = android.R.id.title;
TextView tvTitle = (TextView) tabView.findViewById(idTitle);
tvTitle.setText(title);
}
But the result I get isn't what I need - it sets text to tab under the tab icon.
Could you please help me?
try with following code,
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
Log.i("Tab id", ""+tabId);
setTitle(tabId);
}
});
setIndicator (CharSequence label, Drawable icon).
Use this function and the label will be shown as a title.

Tab Background change on selection

I need to set different Images as tab background on different states. I have set one image as background for default but how to switch to other one when tab is selected. Below is my code.
public class HelloTabWidget extends TabActivity {
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
Intent intent; // Reusable Intent for each tab
TabWidget tw = getTabWidget();
for (int i = 0; i < tw.getChildCount(); i++) {
View v = tw.getChildAt(i);
v.setBackgroundDrawable(getResources().getDrawable
(R.drawable.tab_artist));
}
//First tab
intent = new Intent().setClass(this, FirstActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("First").setIndicator("First")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected);
//Second tab
intent = new Intent().setClass(this, SecondActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Second").setIndicator("Second")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected);
//third
intent = new Intent().setClass(this, ThirdActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Third").setIndicator("Third")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected);
}
}
/*tab_artist.xml*/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:background="#drawable/tabselected" android:state_selected="true" />
<!-- When not selected, use white-->
<item android:background="#drawable/tabunselected" />
</selector>
public class HelloTabWidget extends TabActivity implements OnTabChangeListener{
.....
mTabHost. setOnTabChangedListener(this);
#Override
public void onTabChanged(String tabId) {
// Here in tabId you will get the name of the Tab from that you can check and set the background
// of the requirement tab according to need.
}
}
Implement onTabChangeListener() and there modify their backgrounds. Cheers
http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Edit:
Use the tabHost to implement the method. You can implemented where ever you want. Let's say do it after you set all the TabWidgets. Its good practice to use ids of the tab like you've set them "First", "Second" etc etc.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(tabId.equals("First"){
//do something
}else if(tabId.equals("Second"))
{
//do something
}// etc etc etc
}});
this may help you
Tabhost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tb.getTabWidget().getChildCount();i++)
{
tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected
}
tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected);
}});

Categories

Resources