I am working with the tabhost with 2 tabs i am unable nothing in the tab activity,
This is the main class where i implemented the tab host
tabHost = getTabHost();
//* TabHost Implementations*//*
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec tab1 = tabHost.newTabSpec("FirstTab");
TabSpec tab2 = tabHost.newTabSpec("SecondTab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Friends");
Intent tab1intent = new Intent().setClass(this, Friends_list_add.class);
tab1intent.putExtra("userid", userid);
System.out.println("tabHost------" + tab1intent);
tab1.setContent(tab1intent);
tab2.setIndicator("Invite Friends");
Intent tab2intent = new Intent().setClass(this, Friends_Invite_list.class);
tab2intent.putExtra("response", response);
System.out.println("tab2intent------" + tab2intent);
tab2.setContent(tab2intent);
//** Add the tabs to the TabHost to display. *//*
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tab = 1;
//set Windows tab as default (zero based)
tabHost.setCurrentTab(tab);
Through intent I am switching to another activity in that activity in onCreate
I have to view a layout as follows textview but it's not displaying anything
no_friends = (TextView) findViewById (R.id.nodata_found_friends);
I have referred all the website but still didn't get anything,
Please help me, Thanks in advance
Try this way to make tabhost i m sure its work.
public class MainActivity extends TabActivity implements OnTabChangeListener,
OnClickListener {
private ImageView iv_Home;
private ImageView iv_Chat;
private ImageView iv_Post;
private ImageView iv_Alert;
private ImageView iv_More;
private TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Casting();
}
private void Casting() {
// TODO Auto-generated method stub
iv_Home = (ImageView) findViewById(R.id.iv_Home);
iv_Chat = (ImageView) findViewById(R.id.iv_Chat);
iv_Post = (ImageView) findViewById(R.id.iv_Post);
iv_Alert = (ImageView) findViewById(R.id.iv_Alert);
iv_More = (ImageView) findViewById(R.id.iv_More);
iv_Home.setOnClickListener(this);
iv_Chat.setOnClickListener(this);
iv_Post.setOnClickListener(this);
iv_Alert.setOnClickListener(this);
iv_More.setOnClickListener(this);
tabHost = getTabHost();
tabHost.setup();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("Tab1").setIndicator("")
.setContent(new Intent(this, CategoryActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("Tab2").setIndicator("")
.setContent(new Intent().setClass(this, ChatActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost
.newTabSpec("Tab3")
.setIndicator("")
.setContent(
new Intent().setClass(this, LoginActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("Tab4").setIndicator("")
.setContent(new Intent().setClass(this, AlertActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("Tab5").setIndicator("")
.setContent(new Intent().setClass(this, MoreActivity.class));
tabHost.addTab(tabSpec);
tabHost.setOnTabChangedListener(this);
tabHost.setCurrentTab(0);
setSelectedTabColor();
}
#SuppressWarnings("unused")
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int pos = this.tabHost.getCurrentTab();
// this.mViewPager.setCurrentItem(pos);
setSelectedTabColor();
}
// private static void AddTab(MainActivity activity, TabHost tabHost,
// TabHost.TabSpec tabSpec) {
// tabSpec.setContent(new CategoryTabFactory(activity));
// tabHost.addTab(tabSpec);
// }
private void setSelectedTabColor() {
// TODO Auto-generated method stub
switch (tabHost.getCurrentTab()) {
case 0:
Global.setActionbar(this, getActionBar(),
getString(R.string.app_name));
iv_Home.setImageResource(R.drawable.btn_home_hover);
iv_Chat.setImageResource(R.drawable.btn_chat);
iv_Post.setImageResource(R.drawable.btn_post);
iv_Alert.setImageResource(R.drawable.btn_alert);
iv_More.setImageResource(R.drawable.btn_more);
break;
case 1:
Global.setActionbar(this, getActionBar(), getString(R.string.chat));
iv_Home.setImageResource(R.drawable.btn_home);
iv_Chat.setImageResource(R.drawable.btn_chat_hover);
iv_Post.setImageResource(R.drawable.btn_post);
iv_Alert.setImageResource(R.drawable.btn_alert);
iv_More.setImageResource(R.drawable.btn_more);
break;
case 2:
Global.setActionbar(this, getActionBar(),
getString(R.string.post_product));
iv_Home.setImageResource(R.drawable.btn_home);
iv_Chat.setImageResource(R.drawable.btn_chat);
iv_Post.setImageResource(R.drawable.btn_post_hover);
iv_Alert.setImageResource(R.drawable.btn_alert);
iv_More.setImageResource(R.drawable.btn_more);
break;
case 3:
Global.setActionbar(this, getActionBar(), getString(R.string.alert));
iv_Home.setImageResource(R.drawable.btn_home);
iv_Chat.setImageResource(R.drawable.btn_chat);
iv_Post.setImageResource(R.drawable.btn_post);
iv_Alert.setImageResource(R.drawable.btn_alert_hover);
iv_More.setImageResource(R.drawable.btn_more);
break;
case 4:
Global.setActionbar(this, getActionBar(), getString(R.string.more));
iv_Home.setImageResource(R.drawable.btn_home);
iv_Chat.setImageResource(R.drawable.btn_chat);
iv_Post.setImageResource(R.drawable.btn_post);
iv_Alert.setImageResource(R.drawable.btn_alert);
iv_More.setImageResource(R.drawable.btn_more_hover);
break;
default:
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == iv_Home) {
tabHost.setCurrentTab(0);
} else if (v == iv_Chat) {
tabHost.setCurrentTab(1);
} else if (v == iv_Post) {
tabHost.setCurrentTab(2);
} else if (v == iv_Alert) {
tabHost.setCurrentTab(3);
} else if (v == iv_More) {
tabHost.setCurrentTab(4);
}
setSelectedTabColor();
}
}
First code put in xml File
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/title_back_color" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:background="#color/popular_back_color"
android:orientation="horizontal"
android:showDividers="none"
android:tabStripEnabled="false" />
</LinearLayout>
</TabHost>
now in your main activity to put this code.
public class MainActivity extends FragmentActivity implements
OnTabChangeListener, OnPageChangeListener {
MyPageAdapter pageAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
// Tab Initialization
initialiseTabHost();
// Fragments and ViewPager Initialization
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(pageAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);
}
// Method to add a TabHost
private static void AddTab(MainActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec) {
tabSpec.setContent(new MyTabFactory(activity));
tabHost.addTab(tabSpec);
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
setSelectedTabColor();
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
// Manages the Page changes, synchronizing it with Tabs
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int pos = this.mViewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
#Override
public void onPageSelected(int arg0) {
}
#SuppressLint("ResourceAsColor")
private void setSelectedTabColor() {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
tv.setTextColor(getResources().getColor(R.color.white));
mTabHost.getTabWidget()
.getChildAt(i)
.setBackgroundColor(
getResources().getColor(R.color.popular_back_color));
}
mTabHost.getTabWidget()
.getChildAt(mTabHost.getCurrentTab())
.setBackgroundColor(
getResources().getColor(R.color.app_yellow_color));
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
// TODO Put here your Fragments
// DealTab f1 = DealTab.newInstance();
DealTab_New f1 = DealTab_New.newInstance();
EventTab f2 = EventTab.newInstance();
MyAccountFragment f3 = MyAccountFragment.newInstance();
MessageFragment f4 = MessageFragment.newInstance();
MoreFragment f5 = MoreFragment.newInstance();
QrCodeFragment f6 = QrCodeFragment.newInstance();
// fList.add(f1);
fList.add(f1);
fList.add(f2);
fList.add(f3);
fList.add(f4);
fList.add(f5);
fList.add(f6);
return fList;
}
// Tabs Creation
#SuppressLint("ResourceAsColor")
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
// TODO Put here your Tabs
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab1").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_deals)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab2").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_event)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab3").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_my_account)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab4").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_message)));
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Tab5").setIndicator(
"",
getApplicationContext().getResources().getDrawable(
R.drawable.btn_more)));
mTabHost.setOnTabChangedListener(this);
setSelectedTabColor();
}
its work proper way and i use this in my application.
Thanks & Regards.
Related
public class Test extends Fragment {
EditText subject,bodybd;
TextView buttonpass;
private long mLastClickTime = 0;
TabHost tHost;
public static Test newInstance() {
Test fragment = new Test();
return fragment;
}
ImageView my_loc_btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
setTitleFragment("Email to GSD");
View rootView = inflater.inflate(R.layout.store_location, container, false);
my_loc_btn=(ImageView) rootView.findViewById(R.id.my_loc_btn);
my_loc_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment newFragment = new MyLocation();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tabcontent, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
// ///// set tab content////
Resources ressources = getResources();
TabHost tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
tabHost.setup();
// List tab
Intent intentAndroid = new Intent().setClass(getActivity(),NewMapShow.class);
TabHost.TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Android",getResources().getDrawable(R.drawable.nicon))
.setContent(intentAndroid);
// map tab
Intent intentApple = new Intent().setClass(getActivity(), MyLocation.class);
TabHost.TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Apple",getResources().getDrawable(R.drawable.nicon))
.setContent(intentApple);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecAndroid);
/** Defining Tab Change Listener event. This is invoked when tab is changed */
TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
}
};
tabHost.setOnTabChangedListener(tabChangeListener);
tabHost.setCurrentTab(2);
/////////end of tabfragment//////////////
return rootView;
}
protected void setTitleFragment(String strTitle){
Toolbar mToolbar = (Toolbar) ((AppCompatActivity)getActivity()).findViewById(R.id.toolbar);
TextView txtTitle =((TextView)mToolbar.findViewById(R.id.toolbar_title));
txtTitle.setText(strTitle);
}
}
and my logcat message:
Process: com.nahid.com.gsdambassadorpractice, PID: 22380
java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
at android.widget.TabHost.setup(TabHost.java:132)
at com.nahid.com.gsdambassadorpractice.fragment.Test.onCreateView(Test.java:85)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
now i want to know how can i run tabactivity in a fragment.Also how to change fragment on tab click.Above code is crushing the application.Please help me how can i solve this problem.
Hello I am developing an application
Which requires to add Run Time tabs in the android And all the tabs should add dynamically.
My Requirement is:
I want to add Tabhost with 5 tabs Which Should display the Screen.
But some Time it require only 3 tabs in the screen then design should not change.
Same if I want to add more then 5 tabs then tab should scroll Run time The main Thing is Design should not change.
Can any one tell me How can i do that?
Currently I am using Following Code:
public class Story_List extends ActivityGroup
{
ListView list_stories;
TabHost tab_stories;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.story_list);
tab_stories=(TabHost)findViewById(R.id.tabhoststories);
tab_stories.setup(this.getLocalActivityManager());
setupTab1(new TextView(this), "Album 1");
setupTab2(new TextView(this), "Album 2");
setupTab3(new TextView(this), "Album 3");
}
private void setupTab1(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum1.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab2(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum2.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab3(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum3.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_text, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
try this :
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Tab1");
tabSpec.setContent(R.id.btnTab);
tabSpec.setIndicator("My Tab 1");
tabHost.addTab(tabSpec);
btnAddTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabHost.TabSpec spec = tabHost.newTabSpec("Tab"+i);
spec.setContent(new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
return new AnalogClock(MainActivity.this);
}
});
spec.setIndicator("Clock");
tabHost.addTab(spec);
}
});
i want to change color of tabwidget background. How can i change color of blue line??
My code is:
public class MainActivityNew extends ActionBarActivity implements
ActionBar.TabListener {
private FragmentTabHost mTabHost;
private int oldButtonId = 1;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private Database db;
private FrameLayout frame;
// Tab titles
private String[] tabs = {"Новости", "Мои купоны"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
private void initialization() {
frame = (FrameLayout) findViewById(R.id.content_frame);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), actionBar, frame, viewPager);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.content_frame);
///////////////Add now 16/01/2014
Bundle b = new Bundle();
mTabHost.getTabWidget().setDividerDrawable(R.color.tabTransparent);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageDrawable(getResources().getDrawable(R.drawable.news_icon_selector));
mTabHost.addTab(mTabHost.newTabSpec("1")
.setIndicator(tabIndicator),
FragmentNews.class, b);
tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageDrawable(getResources().getDrawable(R.drawable.catalog_icon_selector));
mTabHost.addTab(mTabHost.newTabSpec("2")
.setIndicator(tabIndicator), FragmentCatalog.class, b);
tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_center,mTabHost.getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageDrawable(getResources().getDrawable(R.drawable.my_cards_icon_selector));
mTabHost.addTab(mTabHost.newTabSpec("3").
setIndicator(tabIndicator),FragmentMyCards.class, b);
tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageDrawable(getResources().getDrawable(R.drawable.near_icon_selector));
mTabHost.addTab(mTabHost.newTabSpec("4").
setIndicator(tabIndicator),FragmentNear.class, b);
tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false);
icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageDrawable(getResources().getDrawable(R.drawable.settings_icon_selector));
mTabHost.addTab(mTabHost.newTabSpec("5").
setIndicator(tabIndicator), FragmentSettingsPartOne.class, b);
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
setActivityView(Integer.valueOf(tabId));
Log.d("tabs", "tabID=" + tabId);
}
});
////////////////////end of adding
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
setButtonsColor(1, 3);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
public void setActivityView(int newButtonId) {
switch (newButtonId) {
case 1:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.show();
viewPager.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 0, 8.0f));
frame.setLayoutParams(new LinearLayout.LayoutParams(0, 0, 0f));
break;
default:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.hide();
frame.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 0, 8.0f));
viewPager.setLayoutParams(new LinearLayout.LayoutParams(0, 0, 0f));
break;
}
}
#Override
protected void onResume() {
initialization();
db = Database.getInstance(this);
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
super.onResume(); /
}
}
I have read this i try next
TabWidget widget = mTabHost.getTabWidget();
for(int i = 0; i < widget.getChildCount(); i++) {
View v = widget.getChildAt(i);
// Look for the title view to ensure this is an indicator and not a divider.
TextView tv = (TextView)v.findViewById(android.R.id.title);
if(tv == null) {
continue;
}
// v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
v.setBackgroundColor(Color.RED);
}
But it only change background color of bottom tabs.
Use android.support.design.widget.TabLayout instead
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/actionbar"
app:tabIndicatorColor="#color/tab_indicator_color" // this is what you want
app:tabIndicatorHeight="3dp" // tab indicator height
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/font_color_white70"/>
This has backward compatibility also
I have bin trying to customize how my tabs look, and I don't get any errors. But when I try to run the app it crashes. This is what I got:
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
and:
private void setUpTabs() {
// tabHost.setup();
//
// TabSpec spec1 = tabHost.newTabSpec("TAB 1");
// spec1.setContent(R.id.tab_1);
// spec1.setIndicator("", getResources().getDrawable(R.raw.home));
//
// TabSpec spec2 = tabHost.newTabSpec("TAB 2");
// spec2.setContent(R.id.tab_2);
// spec2.setIndicator("", getResources().getDrawable(R.raw.most));
// TabSpec spec3 = tabHost.newTabSpec("TAB 3");
// spec3.setContent(R.id.tab_3);
// spec3.setIndicator("", getResources().getDrawable(R.raw.favorites));
// TabSpec spec4 = tabHost.newTabSpec("TAB 4");
// spec4.setContent(R.id.tab_4);
// spec4.setIndicator("",
// getResources().getDrawable(R.raw.search_icon));
// tabHost.addTab(spec1);
// tabHost.addTab(spec2);
// tabHost.addTab(spec3);
// tabHost.addTab(spec4);
// tabHost.setOnTabChangedListener(this);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.xml.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
The section commented out, is what I did before using the standard tab design.
The .xml parts so far should have no problems, sins that it so far is based 100% on other stuff that I dug up in the net.
private void setupTab(final View view, final String tag) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return view;
}
});
tabHost.addTab(setContent);
}
like this..........
You have to specify the view inside createTabContent{} method........the view returns null thats why its crashing.......
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
icon = (ImageView) findViewById(R.id.header);
Resources res = getResources();
// construct the tabhost
setContentView(R.layout.tab_layout);
setupTabHost();
// mTabHost.getTabWidget().setDividerDrawable(R.drawable.hotels);
setupTab(new TextView(this), "Hotels");
res.getDrawable(R.drawable.hote);
setupTab(new TextView(this), "MyAccount");
res.getDrawable(R.drawable.ma);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.tab_layout);
View title = getWindow().findViewById(R.id.tabsLayout);
}
#TargetApi(4)
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec set_Content = mTabHost.newTabSpec(tag).setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
switch (tag) {
case ("Hotels"): {
Intent setClass = new Intent(this, ListSample.class);
set_Content.setContent(setClass);
break;
}
case ("MyAccount"): {
/*
* Intent setClass = new Intent(this, AccountActivity.class);
* set_Content.setContent(setClass);
*/break;
}
}
mTabHost.addTab(set_Content);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
I'm customizing the Tabs and trying to start an activity for each tab. I'm not able to find the error.
try the following code:
final TabHost tabHost = getTabHost();
TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
ts.setIndicator("1");
ts.setContent(Intent1);// Intent to start activity
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_2");
ts.setIndicator("2");
ts.setContent(Intent2);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_3");
ts.setIndicator("3");
ts.setContent(Intent3);
tabHost.addTab(ts);