Fragment doesn't replace and hide another fragment - android

I'm working on an android project and my fragment are not working...
I'm trying to have this kind of UI ->
http://developer.android.com/training/basics/fragments/fragment-ui.html
However my second fragment doesn't replace or hide the first one, it's simply going under it!
This is how I declare my first fragment in the main activity:
setContentView(R.layout.finallayout);
Bundle bundle = new Bundle();
bundle.putStringArrayList("urllist", URLlist);
// set Fragmentclass Arguments
MainFragment main = new MainFragment();
main.setArguments(bundle);
getFragmentManager().beginTransaction().add(R.id.scrollcontentcontainer, main,"list").commit();
In the onCreateView of this one, I'm adding some content:
mainview = inflater.inflate(R.layout.finallayout, container, false);
scollviewgroup= (ViewGroup) container.findViewById(R.id.scrollcontentcontainer);
//mainview.setOnClickListener(this);
for (int i = 0; i < json.size(); i++) {
lineartab.add(inflater.inflate(R.layout.relativeaqi, scollviewgroup, false));
createMainLayout(this.json.get(i), i, lineartab.get(i), scollviewgroup, inflater);
}
and that's the way I declare the second one:
Fragment contentFragment = new CoreFragment();
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// fragmentTransaction.add(contentFragment, "core");
fragmentTransaction.replace(R.id.scrollcontentcontainer, contentFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
On the onCreateView, I'm inflating with a layout contained in a other layout file:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//container.removeAllViews();
//IF I USE THIS ONE I HAVE THE EXPECTED RESULT, HOWEVER I M NOT SURE THATS THE BEST WAY TO DO IT SINCE ITS REMOVING THE CONTENT INCLUDED IN THE OTHER FRAGMENT
View view = inflater.inflate(R.layout.placecore, container, false);
return view;
}
Trying to use add instead of replace, or using hide doesn't work.
I want my two fragments to use the same layout template, but with an unique instance of it for each fragment, I don't know if it's possible or not. I tried to use a different layout file for the second one but then I had always an error saying No view found for id...
Anyone know how to fix that? thanks
xml files:
finallayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/bottomcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="false">
</RelativeLayout>
<LinearLayout
android:id="#+id/scrollcontentcontainer"
android:focusable="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
and placecore.xml, which contains the content of the 2nd fragment.
It contains many textviews and buttons I hide to make it more clear(i put "some content" instead):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/corecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="20dp"
android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/alayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
some content
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#1d010101"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
some content
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/playout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
some content
</LinearLayout>
</LinearLayout>

Following Michael's comment, using the support library ('android.support.v4.app.Fragment') fixed my problem.

Related

Fragment Transaction

I have a ListFragment with a floating action button. When the button is clicked, i want to replace the ListFragment with a CreateListFragment. Instead, the CreateListFragment is placed on top of the ListFragment and the button and textview from the ListFragment are still visible. I am not sure what went wrong. How do I completely replace the ListFragment with the CreateListFragment?
Here is the screenshot before the button is clicked
http://i.stack.imgur.com/k2HxP.png
Here is a screenshot after the button is clicked
http://i.stack.imgur.com/TYN47.png
list_fragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listFrame">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fab"
android:layout_gravity="bottom|end"
app:backgroundTint="#color/colorAccent"
android:src="#drawable/create"/>
<TextView
android:id="#+id/tvEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="No Shopping Lists Yet"
android:layout_gravity="center"
android:textSize="20sp"/>
</FrameLayout>``
createlist_fragment
<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"
android:padding="10dp"
tools:context="com.lavineoluoch.helloworld.swiftpay.app.CreateListFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/etListTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/title"/>
<EditText
android:id="#+id/etAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/items"/>
<RelativeLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnEditItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnEdit"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
</RelativeLayout>
</LinearLayout>
ListFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.list_fragment, container, false);
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateListFragment createListFragment = new CreateListFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.listFrame,createListFragment);
fragmentTransaction.commit();
}
});
return view;
}
In your root view for your Fragments insert this line of code:
android:background="?android:windowBackground"
As you have a see through background your view can still see the reminiscence of the replaced Fragment as the view hasn't yet updated.
The above answer should solve your problem, however I would recommend re-structuring your approach and instead launching a new Activity which contains the CreateListFragment when the FAB is pressed. Doing so would result in easier to maintain code which is also easier to read and would be best practice. It would also mean that the user can press their back button and navigate back to ListActivity/ListFragment without you needing to manually handle Fragment back stacks.

Fragment doesnt show up

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>

Activity layout is still visible after adding fragment to the activity

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

Fragment containing customized ListView

I am stuck, I dont get how to implement a ListView that is inside a Fragment, I am trying to make a chat gui.
Is it possible to do something like this or am I totaly out in the wind?
Thanks for help! Regards a newbie! ^^
Here is the Fragment code:
public static class ChatFragment extends Fragment {
public ChatFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
/*FragmentManager fm = getActivity().getFragmentManager();
DataListFragment list = new DataListFragment();
fm.beginTransaction().add(R.id.list_message_History, list).commit();
*/
getActivity().setTitle(R.string.chat);
return rootView;
}
}
But as u see I dont udnerstand how to connect it all. I have med a customized ListFragment that uses a ArrayAdapter to handle the data.
Here is my layout file:
<?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">
<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="wrap_content"
android:id="#id/android:list"
android:orientation="horizontal">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
<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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
</LinearLayout>
You don't have to add another Fragment inside your ChatFragment to just have a ListView. Get a reference for the ListView out of your layout like this:
ListView list = (ListView)rootView.findViewById(R.id.list);
Now set the adapter and the Array/List to it and you're done.
And don't make a Fragment transaction inside a Fragment. Use your Activity and the DrawerLayout for this job.
No need of ChatFragment
Make you activity a FragmentActivity and directly inflate the layout(R.layout.fragment_chat) with the ListFragment in that Activity.
class MainActivity extends FragmentActivity {
onCreate(Bundle extra) {
...
setContentView(R.layout.fragment_chat);
...
}
}
Check this example for using ListFragments.

Android how to interact with Fragment tabs

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.

Categories

Resources