Is it legal to use a static fragment inside another fragment.
I just want to reuse an old fragment and add some design to it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.android.camera2video.Camera2VideoFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/camera_fragment">
</fragment>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello from another fragment"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="18sp"/>
</RelativeLayout>
You shouldn't do this. It is explicitly called out as something that won't work in the documentation about nested Fragments:
Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
See https://developer.android.com/about/versions/android-4.2.html#NestedFragments
Create a container to hold that Fragment instead and add it programmatically using the parent Frament's FragmentManager returned by getChildFragmentManager.
Yes, that's fine. Now doing a transaction and replacing a nested fragment has some issues.
Related
My app consists of multiple activities, they all have a common layout in the sense that they are drawer layout, have a tool bar, but they have different main content. Specifically, their complete layouts are as follows
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/action_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
.......main content, this is different for each activity..........
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Since the shared components are defined at "parent level", I'm not sure how to structure these layouts so that the generic layout is only defined once. Is it possible to achieve that? Thanks.
Two options:
1. ViewStub
Define the main layout with a ViewStub where you will have different sub-layouts.
A ViewStub is an invisible, zero-sized View that can be used to lazily
inflate layout resources at runtime. When a ViewStub is made visible,
or when inflate() is invoked, the layout resource is inflated. The
ViewStub then replaces itself in its parent with the inflated View or
Views.
In each Activity you set the same layout and then inflate the specific sub-layout onto the ViewStub.
2. Fragments
If you want to avoid having massive quantities of code on a single Activity you might want to think about using Fragments.
Some other topics to guide you:
How to use View Stub in android
Loading Views On Demand
Yes it is possible by using Fragment. For this structure first you need to define a FrameLayout in your activity_main.xml . This framelayout will be replaced by the contents of the fragments over the time.
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.safallwa.zahan.oreader.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Then you can create your layouts as you want and classes as those extends Fragment . Set these new layouts as contentView of Fragment classes. Now these fragments can replace that FrameLayout of activity_main. Suppose we have a Framgent class name First_Fragment.Java then we can show that fragment by replacing activity_main framelayout by below code
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);//container_body is the id of the framelayout
fragmentTransaction.commit();
For full featured example with navigation drawer also we can follow below link
http://www.codedisect.com/?p=134
i used this 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="sanjaqak.com.karaoke.artist.AllArtistActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="sanjaqak.com.karaoke.Srch.SrchSubFrag"
android:id="#+id/fragment2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
this fragment must have argument to work. but i don't know how can initialize argument for this.
i am very sorry for bad speak English.
AFAIK this is not possible with the current Fragment framework.
In such cases I just create the parent ViewGroup (usually a simple FrameLayout) for the Fragment that needs correct runtime arguments to work, and load that Fragment with those Arguments in the Activity's / parent Fragment's setup phase (usually in .onCreate()) into that ViewGroup.
i want to separate my layout in two parts:
the bottom for ads, and the other part to the app(few activities).
why i want to do that? because i want the ad appears every time even when new activity started.
http://i.stack.imgur.com/plVeO.png
i realized that is possible to do that with fragment.
I have few questions about fragment:
fragment can hold two activities?
can i start a fragment like activity with intent?
The main question is how TO SEPARATE THE LAYOUT IN TO PARTS while using fragment?
No, fragment can not have two Activities, but vise versa - yes. For dynamic actions with fragments create xml file in which will 2 frames, one attached to the bottom, and read about http://developer.android.com/guide/components/fragments.html, http://developer.android.com/reference/android/app/FragmentManager.html, http://developer.android.com/reference/android/app/FragmentTransaction.html
Try this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="#+id/frame2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3" >
</FrameLayout>
</LinearLayout>
in Activity:
NewFragment fragment = new NewFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.frame1, fragment);
ft.commit();
Or, static in xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="#+id/article_fragment"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
you could make one parent activity which will hold adverts and have free space for content, than you inherit that activity and fill up content from inherited one, maybe inflate other layouts in base. if you will be using more than one layout for content you could make also parent layout and "inherit" it as well.
or you can use one activity which would hold adverts and use fragments for content.
In my android app, I have defined a fragment in my xml layout. The code is below:
<fragment
android:id="#+id/list_fragment"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_marginLeft="62dp"
android:layout_marginTop="20dp"
class="com.example.shantaportfolio.ListFragment" />
But I need to change the class attribute of the fragment from my java code. How can I do it? which method use?
You need to change <fragment/> by <FrameLayout/> in your .xml file
<fragment
android:id="#+id/list_fragment"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_marginLeft="62dp"
android:layout_marginTop="20dp"
/>
and then use java code to add fragment programatically
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListFragment fragment = new ListFragment();
fragmentTransaction.replace(R.id.list_fragment, fragment);
fragmentTransaction.commit();
you can get more details from
http://developer.android.com/guide/topics/fundamentals/fragments.html
If you just want to replace a Fragment implementation in the layout insert a "container element" into your layout and set the Fragment there:
in the layout:
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
In the code:
final FragmentTransaction tx = this.getFragmentManager().beginTransaction();
tx.replace(R.id.fragment_container, aFragment);
tx.commit();
You cannot intercept LayoutInflator instantiating the declared fragment. But You can replace() any fragment in your Activity with another afterwards.
Update:
You can use an Implementation of LayoutInflator.Factory, and use its onCreateView() method to do something different for some particular tags in layout.
You provide this factory implementation to LayoutInflator using setFactory()
You cannot alter Fragments which are declared in XML. Instead, declare a container where a fragment will belong and in the onCreate() method of your activity, attach a Fragment to this container. When you want to swap Fragments, simply use the replace method and specify which fragment you want to swap.Note: containers are usually made with a LinearLayout by arranging them to the specific size. Below is a simple XML layout which provides a layout on the left which is 2/3 the screen size and another on the right which is 1/3
<?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:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/fragment_container_parent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<LinearLayout
android:id="#+id/fragment_container_child"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
I'm developing an android application making use of fragments which is more of a Master/Detail form. I want the main activity to consist of a list fragment on the left side, based on the item chosen on the left i want to display a fragment with different layout on the right side.
(Note: each fragment on the right require different layouts/views)
All the examples that I've come across make use of only one common fragment on the right by changing some values in it or swapping/replacing new fragments having same layout.
If someone could shed some light on this problem then it will help me immensely. Thanks!
If you are using framelayouts to hold your fragments, it's the same as those other you mention. You just instantiate your fragment (whatever the layout) and swap it into the framelayout in place of the other one.
If you have hardcoded your fragments into the XML, you won't be able to do that (as far as I've been able to determine).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frames"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/hline1"
android:layout_below="#id/horizontalline"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/leftpane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".4" />
<TextView
android:id="#+id/verticalline"
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#color/bar_background"
android:gravity="center_horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
<FrameLayout
android:id="#+id/rightpane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
Then you use the id for the framelayout and the name of your instantiated fragment to put your fragment into the framelayout.
EventListFragment eventlist = new EventListFragment();
getFragmentManager().beginTransaction().replace(R.id.leftpane, eventlist).commit();
EventDetailFragment eventadd = new EventDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventadd).commit();
When you want to change the contents, you do the same thing again (the following would replace the fragment in the right pane with a new/different fragment, which can have it's own, different, layout associated with it):
EventSuperDetailFragment eventsuper = new EventSuperDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventsuper).commit();