Debug: How to start an activity using custom Tabs - android

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

Related

How to run tabactivity in a fragment?

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.

My tabhost is not working

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.

How to add Run Time Tabs into TabHost in Android?

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

FragmentTabHost not creating view inside Fragment in android

I am having an issue getting the view to change on a tabhost - when I select a tab the content stays blank.
From what I can tell, onCreateView is not being called on the child Fragments. onMenuCreate runs fine because the menu changes like it is supposed to.
public class PatientTabFragment extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
NewPatientFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
NoteListFragment.class, null);
return mTabHost;
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
according to the docs:
Special TabHost that allows the use of Fragment objects for its tab
content. When placing this in a view hierarchy, after inflating the
hierarchy you must call setup(Context, FragmentManager, int) to
complete the initialization of the tab host.
(emphasis mine)
So I suggest somethong like this:
public class PatientTabFragment extends Fragment {
private FragmentTabHost mTabHost;
private boolean createdTab = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
NewPatientFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
NoteListFragment.class, null);
return mTabHost;
}
public void onResume(){
if (!createdTab){
createdTab = true;
mTabHost.setup(getActivity(), getActivity().
getSupportedFragmentManager());
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
Now we can use TabLayout and ViewPager do those things.This is a good guide to use it.Here is my code:
viewPager=(NonSwipeableViewPager)view.findViewById(R.id.circleresdyn_viewpager);
tabLayout=(TabLayout)view.findViewById(R.id.circleresdyn_tablayout);
if (viewPager != null) {
Adapter adapter = new Adapter(((AppCompatActivity)activity).getSupportFragmentManager());
ContentFragment con=new ContentFragment();
con.setArguments(bundleForFramgnet);
MemberFragment memberFragment=new MemberFragment();
memberFragment.setArguments(bundleForFramgnet);
CirResDynTileFragment cirResDynTileFragment=new CirResDynTileFragment();
cirResDynTileFragment.setArguments(bundleForFramgnet);
adapter.addFragment(cirResDynTileFragment, "Tab1");
adapter.addFragment(con, "Tab2");
adapter.addFragment(memberFragment, "Tab3");
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).select();
}
Check this peace of code. It may help you:
import android.app.Fragment;
public class Change_password extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.change_password, container,false);
setTabs();
return rootView;
}
private void setTabs() {
try {
addTab("Airlines", R.drawable.tab_home, HomeActivity_bkp.class);
addTab("Advance Search", R.drawable.tab_search,
AdvanceSearchAcitivty.class);
addTab("Booking", R.drawable.tab_home, Booking.class);
addTab("Settings", R.drawable.tab_search, SettingAcitivty.class);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
// TODO: handle exception
}
}
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);
}

ANDROID: Customized tab design crashes app on startup

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.......

Categories

Resources