Fragment onCreateView is never called - android

I noticed that onCreateView() of one of my fragments is never called and i dont know why.I need to be able to make changes in onResume() but thats is also never called.
Here is how I add the fragments.
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
Tab1Fragment
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab1, container, false);
Log.v("FRAGMENT","FragmentActivityCreateView");
return V;
}
And finnaly the xml passed under R.layout.tab1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<ImageButton
android:id="#+id/searchBtn"
android:src="#drawable/waves"
android:background="#drawable/search_selector"
android:padding="40dp"
android:layout_height="200dp"
android:layout_width="200dp"
android:onClick="liveMatch"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I think I might have created the fragments the wrong way. As you see it now they are created as the xml says but when I changed tabs, the content is "reseted".Any changed I placed in onCreateView() or onResume() is not called.
EDIT
I have tried to implement the method described in the 1st comment. After the implementation the tabs have the same content. Is like all the tabs have the same content. This time I posted all my classes and their associated XML.
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab2Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab3Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
activity_live_game_stats.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_live_game_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
tools:context="lucian.leagueassistant.activity.LiveGameStats">
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/liveGameTabhost"
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" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/liverealtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
I wont post the code from LiveGameTab2 and LiveGameTab3 since they are almost the same. I have also another activity that
Edit2
Seems its solved when i changed from getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit(); to getSupportFragmentManager().beginTransaction().add(android.R.id.tabcontent, new LiveGameTab1Fragment()).commit();

The problem is this:
getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
In the first argument you have to pass a id to a container in the Activity layout where your fragment will be attached.
You are passing a entire layout R.id.tab1.
Add a container view in activity_live_game_stats xml file,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<FrameLayout
android:id="#+id/myContainer"
...(Other attrs)>
</FrameLayout>
</RelativeLayout>
and then
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.myContainer, new Tab1Fragment()).commit();
}

Related

Android Fragments OnCreate vs OnCreateView

I have a fragment with OnCreateView and onCreate. In onCreate() I download some icons and set the in ImageView. The first time the program runs it's ok. The icons are downloaded and set but as soon as I switch tabs and come back onCreateView() is called and resets the interface to exactly how is described in the xml file associated.
I was wondering if its possible to stop this thing to happen. Moving the code from onCreate to onCreateView is not what I want since it just keeps downloading the icons again and again.
Fragment
public class LiveGameTab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.live_game_tab1, container, false);
return V;
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//download an icon from the internet
Bitmap bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length);
Drawable iconDrawable = new BitmapDrawable(getResources(), bitmap);
imgButton.setImageDrawable(iconDrawable);
}
}
FragmentActivity
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent2, new LiveGameTab2Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent3, new LiveGameTab3Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
}
live_game_tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment">
<ImageButton
android:id="#+id/champBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_shape_champ"
/>
</LinearLayout>
activity_live_game_stats.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_live_game_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
tools:context="lucian.leagueassistant.activity.LiveGameStats">
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/liveGameTabhost"
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" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/liverealtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/liverealtabcontent2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/liverealtabcontent3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
UPDATE
Read the comments for the right answer
Sorry if the code looks out of line I'm answering from my phone
public class LiveGameTab1Fragment extends Fragment {
Bitmap bitmap;
Drawable iconDrawable;
public LiveGameTab1Fragment() {}
public LiveGameTab1Fragment(Context context
//add this if you need something from resources) {
//I don't know what you mean by icon.getIcon()
bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length);
iconDrawable = new BitmapDrawable(context.getResources(), bitmap);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.live_game_tab1, container, false);
return V;
}
#Override //you need the onViewCreated function again I'm on my phone so I might be wrong about the variables inside so make sure to take the right code and the functions inside
public void onViewCreated(View v) {
super.onViewCreated(v);
//code moved to constructor so the image is initialize only ones
ImageButton imgButton = (ImageButton) v.findViewById(R.id.yourButtonId);
imgButton.setImageDrawable(iconDrawable);
}
}

ViewPager image gallery inside a Fragment

Hello im new android developer and i try to do the next things with no success
i have Activity, inside the activity i have fragment and inside it i have ** view pager image galley**.
the problem is when i run the app with the activity with the fragment and and gallery inside it the app crash
but when i tried to run the activity with the gallery and the fragment separately they works fine
whats the problem?
here my code:
Activity
public class infoActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_info);
}
}
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="co.checkit.mobile.checkit.Model.gallery_fragment"
tools:layout="#layout/imagegalleryfragment"
android:id="#+id/fragmentid" />
</LinearLayout>
ass you can see i set the fragment inside the activity
Fragment class
public class Hotelsgallery_fragment extends Fragment {
ImageAdapter adapter;
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.imagegalleryfragment, container, false);
viewPager = (ViewPager)myFragmentView.findViewById(R.id.viewpager);
adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
return myFragmentView;
}
}
i set the gallery inside the fragment class
Fragment XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/viewpager">
</android.support.v4.view.ViewPager>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal" />
</LinearLayout>
instead of putting fragment inside xml, put frame layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/frame_layout_id" />
</LinearLayout>
create fragment inside a activity, inflate using frame layout id
YourFragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(R.id.frame_layout_id, fragment);
ft.commit();

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

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

FragmentTabHost graphical layout doesn't render

The graphical layout for a simple android.support.v4.app.FragmentTabHost never renders in either Eclipse or Android Studio.
The Console error I get is consistently:
Exception raised during rendering: No tab known for tag null
I'm using the most basic XML file:
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
but the same error occurs.
I just wanted to add more views above or below the tab widget and frame layout.
I don't care so much about seeing the tab content; I just want to see the rest of my layout - but the problem is that NO OTHER VIEWS are rendered when a android.support.v4.app.FragmentTabHost resides in the layout.
I've read and tried to resolve the issue from the answer to this post:
Android: Tabs at the bottom with FragmentTabHost
but I don't think that that is my problem; I'm not looking to put a TabWidget on the bottom.
Every other one of my XML files opens perfectly.
The same problem occurs in Android Studio:
I had the same rendering problem as well as compilation error. I fixed the problem by finding that I was not passing Fragment when i was creating Addtab. You must pass atleast one fragment on mTabHost.addTab. Below is the working code.
private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(HomeActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"), HomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mysheets").setIndicator("MySheets"));
mTabHost.addTab(mTabHost.newTabSpec("bookmarks").setIndicator("Bookmarks"));
Not sure about the error you've got (sorry, I'm really busy right now so can't spend more time checking) but in general it seems that the FragmentTabHost from the support libs doesn't care about the xml at all. See my previous answer to another question:
FragmentTabHost with horizontal scroll
From Layout i am getting the same Error..so,I resolve that Problem by Code only...It's working fine..Please try this code
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class DetailFragment extends Fragment {
/******************************************************************************************************************
* Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
*****************************************************************************************************************/
public DetailFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// R.layout.fragment_tabs_pager contains the layout as specified in your question
View rootView = inflater.inflate(R.layout.fragment_tabs_pager, container, false);
// Initialise the tab-host
FragmentTabHost mTabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
// Initialise this list somewhere with the content that should be displayed
List<String> itemsToBeDisplayed;
for (String subItem : itemsToBeDisplayed) {
// Give along the name - you can use this to hand over an ID for example
Bundle b = new Bundle();
b.putString("TAB_ITEM_NAME", subItem);
// Add a tab to the tabHost
mTabHost.addTab(mTabHost.newTabSpec(subItem).setIndicator(subItem), YourContentFragment.class, b);
}
return rootView;
}
}
/********************************************************
This class contains the actual content of a single tab
**********************************************************/
public class YourContentFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getArguments();
if (extras != null) {
if (extras.containsKey("TAB_ITEM_NAME")) {
String subItem = extras.getString("TAB_ITEM_NAME");
// Do something with that string
}
}
}
}
If U need to put fragmented tabs at bottom of screen ...
#fallow undermentioned --
Make your xml file like this ..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"> android:layout_alignParentTop="true" -->
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Now if your concern is opening several fragments with in single fragmented tabs ...
#follow steps ::
Create a container fragment. This container fragment will be default for all of your's tabs content.
For every tab content replace fragment U need with this container.
For ex:- Just like you replace your bed with different bed sheets .. :)
Your container fragment class that will be used differently in different tabs ... "LearnContainerFragment.java "
public class LearnContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
}
}
private void initView() {
Log.e("test", "tab 1 init view");
replaceFragment(new Learn(), false);
}
}
LearnContainerFragment.java --- > it's xml file container_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
# How to use Conatiner..
For every fragment U need will be replaced with id of this container fragment.
#last your BaseContainerFragment.java class --
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
Hope it helps.....
Cheers!
not sure.... but shouldn't your layout have a tabhost tag in it above the tabwidget's linear layout?
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
>
i made an app a while back that implented tabs using tabhost and this is how my layout was...one tab had a calendar view one had an image switcher and one had a listview...sorry i can't be of more help
<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:background="#drawable/background"
tools:context=".MainActivity" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
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" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="#+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="251dp" >
</ImageSwitcher>
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="#+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

Categories

Resources