Fragment with Tabs in android - android

I have 2 fragments FragmentA and FragmentB. FragmentA is on top of FragmentB. Now I want to add tabs on FragmentB. Please Help.
My code so far :-
layout/fragmentA.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Fragment" />
</LinearLayout>
layout/fragmentB.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F6CE"
android:orientation="vertical" >
<TabHost
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" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="#+id/tabdata1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout=“#layout/tabdata1_view"/>
<include
android:id="#+id/tabdata2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout=“#layout/tabdata2_view"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
FragmentB.java
public class Fragment2 extends Fragment{
TabHost myTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
myTabHost = (TabHost) getView().findViewById(R.id.tabhost)
return rootView;
}
}
Error:-
The line “myTabHost = (TabHost) getView().findViewById(R.id.tabhost)”gives an error as it cannot find tabhost.
Please suggest a way to achieve this.

Try this
myTabHost = (TabHost) rootview.findViewById(android.R.id.tabhost);
This should remove the error which you are facing now

That's because until you return from onCreateView, the fragment does not actually have a view yet. You just need to call findViewById relative to rootView. Like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
myTabHost = (TabHost) rootView.findViewById(R.id.tabhost)
return rootView;
}

Related

Horizontal Scrollable Tabs Using FragmentTabHost In A Class That Extends Fragment

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

Placing Tabs in DialogFragment

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

FragmentTabHost shows content in tab

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

SlidingPaneLayout does not slide

The slidingPaneLayout does not slide!
pane.isSlideable() is false!
I inflate the layout in this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
slidingView = inflater.inflate(R.layout.available_nodes_fragment, container, false);
SlidingPaneLayout pane = (SlidingPaneLayout)slidingView.findViewById(R.id.sliding_pane_layout);
Fragment fragmentA = new ListNodesFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.child_fragmentA, fragmentA );
Fragment fragmentB = new ReserveNodesFragment();
transaction.add(R.id.child_fragmentB, fragmentB);
transaction.commit();
//pane.setPanelSlideListener(new SliderListener());
pane.openPane();
Log.i("Available Nodes","PANE IS SLIDABLE" + pane.isSlideable());
return slidingView;
}
The available_nodes_fragment layout that i inflate is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="right" />
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
I do not know why the SlidingPaneLayout does not slide. I do not know if this problem has to do with the fact that i use nested fragments.
I have some progress.
I changed the available_nodes_fragment layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reserve_nodes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="left"
android:layout_marginRight="25dp"
android:background="#A588DC" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#B6D4D3"
android:layout_gravity="right" />
</android.support.v4.widget.SlidingPaneLayout>
<Button
android:id="#+id/btnReserveNodes"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:text="Reserve"/>
</RelativeLayout>
Now, the SlidePaneLayout layout slides. I do not know why. When i changed the fixed layout_width of the FrameLayouts to wrap_content, the SlidePaneLayout started to slide.
My problem now is that i want the FrameLayout with id= "child_fragmentA" with the ListView to get the 30% of the screen horizontally. I did not found a way to do it, since FrameLayout does not support weights.
I also provide the code of ListNodesFragment and ReserveNodesFragment.
ListFragments.java
public class ListNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View listNodesView = inflater.inflate(R.layout.nodes_categories_listview, container, false);
String[] items = { getResources().getString(R.string.orbit_nodes),
getResources().getString(R.string.grid_nodes),
getResources().getString(R.string.usrp_nodes),
getResources().getString(R.string.diskless_nodes),
getResources().getString(R.string.icarus_nodes) ,
getResources().getString(R.string.base_station)
};
ListView nodesListView = (ListView) listNodesView.findViewById(R.id.nodes_list);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), R.layout.nodes_categories_listview_item, items);
nodesListView.setAdapter(adapt);
return listNodesView;
}
}
ReserveNodesFragment.java
public class ReserveNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View reserveNodesView = inflater.inflate(R.layout.reserve_nodes_fragment, container, false);
TextView txtView = (TextView) reserveNodesView.findViewById(R.id.textView1);
txtView.setText("fragment 2");
return reserveNodesView;
}
}

TextView not adding in fragment layouts

I have a master-detail activity with fragments. I try to add TextView dynamically in detail fragment. But it's not adding to detail fragment layouts. But the layouts are displayed properly.
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_configuration_detail, container, false);
RelativeLayout cdf_detail_part1=((RelativeLayout) rootView.findViewById(
R.id.cdf_detail_part1));
Log.i("config", "mItem1->"+mItem1);
if (mItem1 != null) {
TextView name_text = (TextView) cdf_detail_part1.findViewById(
R.id.cdf_tv_name);
name_text.setText(name);
}
return rootView;
}
My layout XMl file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
tools:context="com.x.y.configurations.ConfigurationDetailFragment" >
<RelativeLayout
android:id="#+id/cdf_detail_part1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00FF00" >
<TextView
android:id="#+id/cdf_tv_name"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/cdf_detail_part2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000FF" >
</RelativeLayout>
</LinearLayout>

Categories

Resources