how to set position of fragment in center in RelativeLayout - android

hi guys I was playing aroung with fragments in android.
I read that there are two ways to bind a fragment in an activity.
1)using <fragment> tag in layout(XML) file.
2)using FragmentManager and FragmentTransaction .
I want my Activity to handle fragment dynamically so i went for the 2nd approach.
But when a fragment is added it is added to left-top corner in RelativeLayout Activity(which appears to be the default behaviour of RelativeLayout)
I want my fragment to appear at center of the Activity Layout.
I just can't figure it out how to give position to fragmet in JAVA code.
I looked into stackoverflow also,
which uses --RelativeLayout.LayoutParams
which appears to work with View objects(Button,TextView etc).
Any suggestions.

You can set a placeholder FrameLayout inside RelativeLayout in your XML and align it with android:layout_centerInParent="true". Then replace this placeholder with your fragment in the code FragmentTransaction#replace(int containerViewId, Fragment fragment, String tag)
In your Activity XML layout:
<?xml version="1.0" encoding="utf-8"?>
<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">
<FrameLayout
android:id="#id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
In your Activity class:
getFragmentManager().beginTransaction()
.replace(R.id.fragment, new MyFragment())
.commit();

In your Activity's layout use the attribute android:layout_gravity="center" for RelativeLayout in which Fragment resides. This will make all it's child element appear in center.

Related

IllegalStateException : ScrollView can host only one direct child

I have a fragment, which comprises of a scrollview which has a single LinearLayout(I have wrapped up other LinearLayouts into this)
Fragment_Details.xml
<ScrollView
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=".DetailsActivityFragment">
<LinearLayout...>
</ScrollView>
In the DetailsActivity i'm adding a fragment
DetailsActivity.java
DetailsActivityFragment fragment = new DetailsActivityFragment();
fragment.setArguments(b);
getSupportFragmentManager().beginTransaction()
.add(R.id.movie_details,fragment)
.commit();
Activity_Details.xml
<fragment
android:id="#+id/movie_details"
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:name=".DetailsActivityFragment"
tools:ignore="MergeRootFrame"/>
But when i run the program i get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.IllegalStateException: ScrollView can host only one direct child
I have followed threads on the issue of ScrollView, but i'm not able to solve this. Help please!!
Use Frame Layout instead of fragment in activity_details bcoz fragment is used when u have a fixed fragment to show.
Here u r showing ur movie detail fragment using it but then again u r inflating another fragment(another instance of movie detail fragment) in ur activity and creates the problem.
What all views are there in your Linear Layouts? It seems that any of the layouts is having some child which implements it's own scrolling, like Listview.
See this: ListView inside ScrollView is not scrolling on Android

Top part of Fragment get's hidden under the Toolbar which has elevation. How do I bring the fragment to full screen [Overlapping Toolbar]

I am having RelativeLayout as root layout, and it's id is container. Toolbar has an elevation of 4dp. When I am adding a Fragment in R.id.container [RelativeLayout], the Fragment is launching with part of it under the Toolbar.
However when I remove the elevation on Toolbar, Fragment is appearing above the Toolbar. which is what I want. But I want to keep the elevation, and I don't want to change the elevation to 0, while inflating the Fragment, because I am using animations.
How do I get the Fragment on top of Toolbar.
This is the XML code
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/_4dp"
android:text="Chiri"
android:minHeight="?attr/actionBarSize" />
</RelativeLayout>
And in Activity
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.add(R.id.container, new UploadFragment()).commit();
This is how my UI looks right now
But the frameLayout or the fragment tag under the toolbar, also make sure the root tag is linearlayout
I know this is somewhat old now, but showing up for me on Google searches, and no answer, so to help anyone else who stumbles here...
I had this problem too. The answer for me was to define a sibling layout to the layout.
Currently R.id.container is the root in this layout. Create a new parent, and a sibling layout to R.id.container (say R.id.fragment_container), and use that to replace the fragment.
mFragmentTransaction.replace(R.id.fragment_container, new UploadFragment()).commit();

How to create an overlay view within an activity

I have a requirement where i have an activity which shows list of items like facebook feeds and when clicking on a button from one of the list item a dialog has to popup which will show comments for that item.
I was going through the documentation and found out that we have to create a DialogFragment on the fly to achieve this. Please advice if this is the right approach.
You don't actually have to use a Dialog. I think dialogs are more appropriate when you want to show simple views or just an alert/confirmation to the user (normally done with an AlertDialog).
For your situation I guess the best approach would be to have a FrameLayout on your Activity, sibling of your main layout element, and add a Fragment to it when you want to show a popup like that over your main Activity's layout. As long as you put the fragment's view after your activity's root layout element, the fragment will be displayed on top of your main layout, as an overlay. e.g.:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Activity's main layout here -->
</LinearLayout>
<FrameLayout android:id="#+id/overlay_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</merge>
and then on your Activity when you want to display the fragment you do:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.overlay_fragment_container, yourFragment)
.commit();
Hope it helps :) Luck!

Android fragments overlap previous view and button listeners

I have an activity A with a fragment A inside.
Activity A uses layout X, and fragment A uses layout A.
code of layout X:
<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" >
<fragment
android:id="#+id/fragment1"
android:name="android.app.DialogFragment"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="113dp"
class="com.example.fragtester.FragA" />
</RelativeLayout>
Layout A is just textview + linearlayout.
I set up another fragment B that uses layout B.
Now that I use the following code in activity A to change the fragments:
Fragment f = new FragB();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
I end up having layout B displaying under layout A.
So I use a FrameLayout to wrap the fragment in layout X and use
ft.replace(R.id.FrameLayout1, f);
Now the view is working nicely. Though, another problem arises.
Although layout B covers layout A, but the buttons are still active.
That means when I am viewing layout B, I can still click buttons on layout A, even if I am not seeing it.
And even when I add fragment C/D/E..... (layouts C/D/E....), the buttons on layout A is still active.
Can anybody explain why is that? Am I using fragments wrongly? Thanks!
A way to get through is to make layout A blank, and use other layout to cover it. But it doesn't seems to be the "right" way??
Remove the fragment and add a FrameLayout
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
</FrameLayout>
then add fragments programmatically.
In android fragment button click pass through the fragments (i dont know if the fragments are suppose to work like that). what I used to do in such a situation is to make the layout of the fragment clickable. so the clicks wont pass through.
Instead of having fragment in your xml, try to create empty container for a fragments. For example empty frame layout. And then programmatically put your fragments in there.
Add the following attribute to the XML root layout of the fragment that goes on top.
android:clickable="true"
This will ensure that touch events will not propagate further than the top layer.

add Viewpager dynamically in a framelayout

Is it possible to replace a fragment in a FrameLayout with the .replace() to use a ViewPager? since both are ViewGroups.
For example, in xml, I have only
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id ="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
and I would like to replace the FrameLayout with id content that is holding a fragment to a ViewPager of fragments in runtime. Is it possible??
i would like to replace the framelayout with id content that is
holding a fragment to a viewpager of fragments in runtime. is it
possible??
You can't replace a Fragment with a simple ViewGroup like a ViewPager, but thanks to the new changes in 4.2 you could put the ViewPager itself in a Fragment and use nested Fragments as the children of the ViewPager. Then you could replace the initial Fragment with the Fragment that holds the ViewPager.
See this guide, nested Fragments are also available to lower APIs through the compatibility package. See this question for an implementation example.

Categories

Resources