I've been experimenting with tab navigation on android but I can't set up the content area correctly.
The layout of the application:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:tag="tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="linear">
<TabWidget
android:id="#+id/tabs"
android:tag="tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</TabWidget>
<FrameLayout
android:id="#+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
</android.support.v4.app.FragmentTabHost>
The fragment:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/info_drawer_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0">
<FrameLayout
android:id="#+id/info_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment info"
android:id="#+id/info_textView"
android:layout_gravity="center_horizontal" />
</FrameLayout>
<ListView android:id="#+id/info_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
And the code I used in the activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost)findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
tabHost.addTab(tabHost.newTabSpec("Tab 0").setIndicator("", getResources().getDrawable(R.drawable.ic_launcher)), InfoFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("Tab 1").setIndicator("Tab 1"), HomeFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("Tab 2").setIndicator("", getResources().getDrawable(R.drawable.ic_launcher)), AppsFragment.class, null);
}
This generates the following result:
As you can see the content area overlaps the tabs. I would simply like it to be below them and fill up the remaining space like this:
I've tried wrapping them in a RelativeLayout tag and applying the below attribute but it didn't work. I trid using the weight attribute without any luck as well.
Thanks.
You don't need to have the FrameLayout inside the Tabhost, when you do that you're using the same space for the content of the fragments and the tabs themselves.
Just put the TabHost inside a vertical LinearLayout and put the FrameLayout outside, below the TabHost.
Just like this:
<?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">
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</android.support.v4.app.FragmentTabHost>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Sorry, i cannot go into detail with your specific code, but if you anyway just try to implement it for the first time i would go this approach, its really easy to understand and to manage. The code is pretty easy and serves all you need i think.
You just have to make own layoutfiles for the 3 (or whatever) fragments like this:
Layoutfile for a Fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="15sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Notebook"
android:id="#+id/tv_hdl_tab3"
android:textColor="#android:color/holo_blue_light"
android:textSize="#dimen/dim_hdl_h1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/tab3_introText"
android:id="#+id/tv_tab3_IntroText"
android:layout_marginTop="5sp"
android:layout_marginBottom="5sp"
android:textSize="#dimen/dim_txt_p" />
MainActivity:
public class MainActivity extends Activity {
ActionBar.Tab tab1, tab2, tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_test);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = actionBar.newTab().setText("Home");
tab2 = actionBar.newTab().setText("Calculator");
tab3 = actionBar.newTab().setText("Notebook");
tab1.setTabListener(new MyTabListener(fragmentTab1));
tab2.setTabListener(new MyTabListener(fragmentTab2));
tab3.setTabListener(new MyTabListener(fragmentTab3));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
And a fragment looks like this then (3 of them of course, one for every tab as long it should be different):
public class FragmentTab3 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.tab3, container, false);
TextView textview = (TextView) view.findViewById(R.id.tabtextview);
textview.setText(R.string.Three);
return view;
}
}
And the Tablistener:
public class MyTabListener implements ActionBar.TabListener {
Fragment fragment;
public MyTabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// nothing done here
}
}
Code comes from http://www.linux.com/learn/tutorials/761642-android-app-development-for-beginners-navigation-with-tabs
Related
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();
I am trying to make an activity containing two fragments.The first one has 3 nested fragments which I will use as a tabs and the second one only contains buttons. I don't want to add the buttons fragment into other fragments because I don't want it to reload. Right now i don't have any errors but my buttons fragment doesn't show up. Here are my fragments and my activity.
my main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
Fragment statistics = new StatisticsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_statistics, statistics).commit();
Fragment buttons = new MainPageButtonsFragment();
FragmentTransaction buttonsTransaction = getSupportFragmentManager().beginTransaction();
buttonsTransaction.add(R.id.main_page_buttons_fragment,buttons).commit();
his layout
<RelativeLayout android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment_statistics"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/main_page_buttons_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/fragment_statistics"/>
I wont write my buttons fragment because it is not necessary I will only say its root tag is FrameLayout.
my statistics fragment (the one with tabs)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Fragment currentDayFragment = new CurrentDayFragment();
FragmentTransaction transaction1 = getChildFragmentManager().beginTransaction();
transaction1.add(R.id.current_day_fragment, currentDayFragment).commit();
Fragment configurationInfoFragment = new ConfigurationInfoFragment();
FragmentTransaction transaction2 = getChildFragmentManager().beginTransaction();
transaction2.add(R.id.configuration_info_fragment, configurationInfoFragment).commit();
Fragment expensesInfoFragment = new ExpensesInfoFragment();
FragmentTransaction transaction3 = getChildFragmentManager().beginTransaction();
transaction3.add(R.id.expenses_info_fragment, expensesInfoFragment).commit();
LayoutInflater lf = getActivity().getLayoutInflater();
View statisticsView = lf.inflate(R.layout.fragment_statistics, container, false);
return statisticsView;
}
his layout:
<FrameLayout >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/current_day_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#id/configuration_info_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#id/expenses_info_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
the nested fragments just have root tag FrameLayout and couple of TextViews in it. The result is just an activity with 3 tabs and no buttons fragment.
I am new to android programming and I am completely lost in this. Help Please!
Try using giving custom height like this....
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/fragment_statistics"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<FrameLayout
android:id="#+id/main_page_buttons_fragment"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Relative layout does not have android:orientation param
I thing first fragment cover second one
Change relative layout with LinearLayout
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_statistics"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/main_page_buttons_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/fragment_statistics"/>
</LinearLayout>
This is a really lame question, but I really cannot figure out why it happens, so please help me out.
I have a simple activity and I add a fragment to it dynamically. The problem is that once the fragment is added to the activity, the activity layout is also visible. Why does this happen?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<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" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
HelloFragment.java
public class HelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
return v;
}
}
hello_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="fragment 1"/>
</LinearLayout>
Convention is to have a FrameLayout that you use fragmentTransaction.replace()/add() on.
You shouldn't have views like TextView and Button in your MainActivity layout file. Instead, have those in another fragment and just have a FrameLayout in your MainActivity xml. Load the fragment with TextView/Button first, and call replace on button click with your new fragment.
Set a background color to your fragment linearLayout and make it clickable
The fragment is just another element on the layout. Adding it to the layout does not automatically remove other views. If you want the fragment to replace the content of rootView you need to remove rootview's children views first. Something like:
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
So your onClick method should look like this:
#Override
public void onClick(View v) {
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
your rootview work as container for both fragment and your views on activity. check it and make separate view for fragment.
I know it's very late to answer this question but for those who are still looking do the following in your activity layout:
<RelativeLayout
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" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
</RelativeLayout>
And fragment layout to following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/white" <!--Add this-->
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
activity_main.xml
<FrameLayout 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
MainActivity.java
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.fragmentContainer, hello, "HELLO");
fragmentTransaction.commit();
}
};
I have a solution for this to achieve what you can do is make two fragment one for textview and button your showing as default when activity is opened and another which you have already created as HelloFragment.
From Activity onCreate() method call the first fragment with default textview and button and from the click listener of that button call HelloFramgent.
Step1: Crate another fragment with any name MainFragment having a xml layout as follows:-
main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
MainFragment.java - Class
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.main_fragment_layout, container, false);
Button btnLoad = (Button) v.findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
return v;
}
}
Step2: Call the MainFragment from activity onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment mainFragment = new MainFragment();
fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
fragmentTransaction.commit();
}
Build your user interface in Fragments (In most cases one Fragment equals one screen) and then use Activities to arrange and display those Fragments.
check this: click here to view detailed answer
You have two linear layouts, one inside another, so you need to do the following steps:
Set id to 1 linear layout rootView and next to the childview.
Find your child view:
View view = findViewById(R.id.child_view);
view.setVisibility(View.INVISIBLE);
Replace your rootView with your fragment:
getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
.addToBackStack(null).commit();
Then override:
#override
public void onBackPressed() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
view.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
The Activity views are not being masked against the Fragment's views, so both appear. One solution is to load the fragment into a frame in the main activity's layout which has a slight elevation (2 dp). When the fragment is loaded its base elevation will cause it to always draw over the activity. Set the fragments parent container with a color to hide the activity views.
MasterActivity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Activity Title"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="24sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button2" />
<!-- Fragment holder, with elevation -->
<FrameLayout
android:translationZ="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragHolder"
/>
The Fragment layout has a background color:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test Background Task"
android:textSize="24sp" />
</RelativeLayout>
Make your First Layout invisible while calling fragment and again while returning from fragment make your main view visible and remove fragment at that time so it will not conflict with each other it worked for really well just check it once
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>
Hello I am having difficulty changing the visual view of the fragment activity. I created all the tabs but onClick I do not know how to pass the activity into that white space.
so the structure of this is a simple activity named Main which just sets the content view to a maintabholder.xml document that holds the fragment view which connects to a class named MainNav whose content view contains all the tabs at the bottom.
maintabholder.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/header" android:layout_alignParentTop="true" android:id="#+id/headerArea"></include>
<fragment
class="my.package.name.MainNav"
android:id="#+id/fragment_tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainNav.java code snippet:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
mRoot = inflater.inflate(R.layout.fragment_tabs, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
and R.layout.fragment_tabs:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:background="#drawable/tab_background"
android:gravity="center_horizontal"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/tab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</RelativeLayout>
and Finally the contents of an Activity that I want to load into the Fragment (upon pressing the tab1 button)
public class HomeFragment extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
That is to help you get the gist of things, but the problem occurs here in MainNav.java:
#Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
if (TAB_HOME.equals(tabId)) {
updateTab(tabId, R.id.tab_1);
mCurrentTab = 0;
return;
}
}
when a tab is clicked, it is registered in the onTabChanged activity, and the updateTab function is called:
private void updateTab(String tabId, int placeholder) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
//fm.beginTransaction()
// .replace(placeholder, /*need fragment object here*/ , tabId)
// .commit();
//
}
}
the commented out replace method is looking for an (int, fragment, string) and I guess it should then change the white space for me.
but I don't understand where I get the fragment object from! This phantom object needs to somehow contain my HomeFragment's xml layout and allow me to interact with everything else I've coded for the HomeFragment activity.
My first hunch is that my HomeFragment activity needs to extend FragmentActivity instead
just activity. But this is where I am hitting a wall.
There is very little documentation on the compatibility pack methods, but the documentation for TabActivity says not to use TabActivity anymore, so here I am. Developing for Android 2.2+
Insight appreciated.
You can't put Activities into Fragments, it's jut that simple. You have to convert your Activities that serve the tab-contents into Fragments. It shouldn't be a problem, given that you don't use fragments in those Activities.
So here are 2 rules to follow:
You can't put Activities into Fragments.
You can't put Fragments into Fragments.
I recommend that you keep that bottom fragment on your layout around just to handle the tab-switches in the tab-widget. If a tab switch happens, you report this to the parent Activity, which will load the corresponding Fragment into the are above the tab-fragment. For this to work, you'd have to modify you layout so it has a ViewGroup above the tab-fragment and below the header. You'd dynamically load the fragments with the tab-contents into that ViewGroup.
Here's the layout (maintabholder.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/header" android:layout_alignParentTop="true" android:id="#+id/headerArea"></include>
<FrameLayout android:id="#+id/TabContent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<fragment
class="my.package.name.MainNav"
android:id="#+id/fragment_tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
When a tab-switch happens in your bottom fragment, you tell this to the parent Activity:
...
(YourActivity)getActivity().switchToTab(tabId);
...
And in the YourActivity:
...
public void switchToTab(int tabId){
switch(tabId){
...
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.TabContent, TabContentFragment2.newInstance(), "someTag").commit().
break;
...
}
}
Something like that.