i'm trying to add a TabHost layout inside a Fragement:
public class FriendsTabFregment extends Fragment {
private TabHost tab;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_friends, container, false);
tab = (TabHost) view.findViewById(R.activity_friends.tab);
return view;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addNewTab("Friends", BasicFragment.class, savedInstanceState); //Chat Tab
setTabHeight(50);
}
for some reason i get the tab as null.
this is my XML:
<TabHost android:id="#+activity_friends/tab"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
</LinearLayout>
any idea why ?
Define your TabHost's id as
android:id="#+id/activity_friends_tab"
and find it like this:
tab = (TabHost) view.findViewById(R.id.activity_friends_tab);
Have a look at this post for further understanding of "#+id/"
What does "#+id" mean?
Android documentation source
Related
I have a problem with my TabHost, inside a Fragment, the first tab to be selected will always be highlighted, so I have often two tabs selected as shown here
Here Notifications should not be highlighted !
But the problem is only for Notifications tab, as you can see here the tab friends behave just normally, because it is the second added..
Here is my code for the fragment holding my TabHost
public class ProfileContentFragment extends Fragment {
private FragmentTabHost mTabHost;
public ProfileContentFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.profile_content_fragment,container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("notifications").setIndicator("Notifications "),
NotificationsFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("friends").setIndicator("Friends"),
FriendsFragment.class, null);
return rootView;
}
#Override
public void onDestroyView(){
super.onDestroyView();;
mTabHost=null;
}
}
And here is the XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
SOLVED :
It was solved by changing
mTabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);
To :
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
I am using a class that extends Fragment and not FragmentActivity and I cannot seem to make horizontal scroll to work. I first used a layout as simple as the first code below and it works perfectly (without the scroll of course). I decided to put a horizontal scroll because when my tab reaches 6, the text is so hard to read and when the text is too long, the complete text is not written. I tried to use the layout of other people that said "This works!". Please refer to the second code below; the third code is my fragment class. Thank you!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The layout that other people said "It works"
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Lastly, my Fragment class. Maybe I'm missing something out?
public class AlarmTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentTabHost tabHost;
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.alarm_tab);
for(int i=0; i<SmashDeviceData.get_instance().devices.size(); i++) {
Bundle bundle = new Bundle();
bundle.putInt("Arg for Frag" + i, 1);
tabHost.addTab(tabHost.newTabSpec("Tab"+i).setIndicator(SmashDeviceData.get_instance().devices.get(i).deviceName), AlarmActivity.class, bundle);
}
return tabHost;
}
public static Fragment newInstance() {
Fragment fragment = new AlarmTab();
return fragment;
}
}
try the below
for xml
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tabsColor"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tabsColor"
android:gravity="center_horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
for your fragment
public class HomeFragment extends Fragment {
private FragmentTabHost mTabHost;
TabWidget widget;
HorizontalScrollView hs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
initView(rootView);
return rootView;
}
private void initView(View rootView) {
mTabHost = (FragmentTabHost) rootView
.findViewById(android.R.id.tabhost);
widget = (TabWidget) rootView.findViewById(android.R.id.tabs);
hs = (HorizontalScrollView) rootView
.findViewById(R.id.horizontalScrollView);
mTabHost.setup(getActivity(), getChildFragmentManager(),
android.R.id.tabcontent);
}
}
I am attempting to place TabHost inside of DialogFragment but at present it is none functional and returns null on the line:
tabs.findViewById(R.id.tabHost);
How can tabhost be initialized in the DialogFragment to allow the tabs to appear?
Tabbed DialogLog class:
public class InviteFriendTabbedAlertDialog extends DialogFragment {
TabHost tabs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);
// Add tabs
tabs.findViewById(R.id.tabHost);
tabs.setup();
TabHost.TabSpec tabpage1 = tabs.newTabSpec("one");
tabpage1.setContent(R.id.shareIndividual);
tabpage1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.abc_ab_solid_dark_holo));
TabHost.TabSpec tabpage2 = tabs.newTabSpec("two");
tabpage2.setContent(R.id.shareGroup);
tabpage2.setIndicator("Tab 2", getResources().getDrawable(R.drawable.abc_ab_bottom_transparent_light_holo));
tabs.addTab(tabpage1);
tabs.addTab(tabpage2);
return inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);
}
}
tabbed layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/shareIndividual"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/shareGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Initialize tabs and also use inflated view in which have TabHost with tabHost to initialize tabs variable :
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);
// Add tabs
tabs=(TabHost)view.findViewById(R.id.tabHost);
//...your code here
return view;
}
im trying to set up a FragmentTabHost with 3 tabs. The problem im having is the content of each tab is actually being displayed in the tab tittle as well. Why is this happening? The layout for tab1 has a textview that says "Tab1" this is being displayed in the tab and not the content area. also if i change the background it changes the background of the tabs not the content area.
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
MAIN Fragment:
public class NewFragment _Frag extends Fragment{
private FragmentTabHost mTabHost;
Intent intent;
boolean created = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return super.onCreateView(inflater, container, savedInstanceState);
if(container==null)
{
return null;
}
View v = inflater.inflate(R.layout.mylayout, container, false);
mTabHost = (FragmentTabHost)v.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Tab1"),
myclass1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Tab2"),
myclass2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Tab3"),
myclass3.class, null);
return v;
}
First, lets assume your XML name is my_xml.xml.
So make following changes in the my_xml.xml first(Change the ID)
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
and then following changes in the java file.
mTabHost = (FragmentTabHost)v.findViewById(R.id.my_tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_xml);
you missed these two :
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
so add them in your Linearlayout above framelayout
actually you do not need any changes because your code is exactly like demo see below:
https://chromium.googlesource.com/android_tools/+/18728e9dd5dd66d4f5edf1b792e77e2b544a1cb0/sdk/extras/android/support/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.java
https://chromium.googlesource.com/android_tools/+/18728e9dd5dd66d4f5edf1b792e77e2b544a1cb0/sdk/extras/android/support/samples/Support4Demos/res/layout/fragment_tabs.xml
I would like to create tab in android but really I don't know how to create and
just downloaded some video but really this isn't helpful
I used TabActivity unfortunately it's deprecated :(
so that I need you to help me out thanks for any suggestion
You must setup a FragmentActivity main class to run the Fragments.
You need to use the FragmentTabHost class available in the support library android.support.v4.app
The class for using FragmentTabHost :
public class SampleTabFragment extends Fragment {
private FragmentTabHost mTabHost;
public static View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
view = (LinearLayout) inflater.inflate(R.layout.offer_tabs, container, false);
// Setting Up the TabHost
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.offer_realtabcontent);
// For adding a tab divider image
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
// For Setting up the Tabs with their respective labels
mTabHost.addTab(mTabHost.newTabSpec("info").setIndicator(createTabView
(mTabHost.getContext(), "INFO")), DetailedOfferFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("location").setIndicator(createTabView
(mTabHost.getContext(), "LOCATION")), LocationFragment.class, null);
setRetainInstance(true);
return view;
}
/***** Method for Custom TextView for Tabs *****/
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_text_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
The layout for using the tabs offer_tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#6D6C6C"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#696969" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/offer_realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>