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
Related
I have extended 3 fragment in main activity..also how do i add recyclerview to display a list in one of my fragment.
Please help
Never mind you are begineer :) . Look first of all look how the fragment works.Fragment requires a activity . In activity you have to create framelayout and load or commit fragment in framelayout(Activity).
Now if you want to change any view in the activity you have to change it in fragment and call respective fragment in activity of change its view.
This means you have to add recyclerview in the activity but first off all you must add that fragment containing recyclerview to framelayout of the activity.
You are looking for something like this:
Note: Make sure you add this to your app gradle:
compile 'com.android.support:recyclerview-v7:23.4.0'
You have fragment1.java with layout fragment1.xml:
fragment1.xml
<?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">
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment1_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Initialize this RecyclerView inside fragment1.java and write is adapter.
For more on this, try these links:
How to implement RecyclerView with CardView rows in a Fragment with TabLayout
http://www.androidhive.info/2016/01/android-working-with-recycler-view/
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.
Template that provide Android Studio
This is how I try to do. What is the R.id.x?. I tried using layout id in xml ActivityMain but not the fragment is displayed or overlaid with out activity information.
After selecting one of the items, the fragment will be displayed above the activity. The activity has several elements belonging to material design as the FAB or the Toolbar, Could this create problems with what I do?
In activity_main.xml try replacing:
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
With this:
<FrameLayout android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And then use R.id.fragment_container as your R.id.x.
This will however replace the view with the FAB and coordinator layout. But you will probably want to add them to the fragments where you want them.
If not you could try replacing the xml found below in app_bar_main.xml, or wrap it in the FrameLayout.
<include layout="#layout/content_main"/>
Update:
Replacing in activity_main.xml will cause some issues as that removes the toolbar.
If you replace the include in app_bar_main.xml your fragments will most likely get overlapped by the toolbar at the top. To prevent this you could try adding app:layout_behavior="#string/appbar_scrolling_view_behavior" in the FrameLayout snippet above.
R.id.x is the id of the View that your fragment going to be replaced. So in your layout R.layout.xyz you have created a layout
<FrameLayout android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and add/replace the fragment to that id. (R.id.fragment_container) such that your new fragment will be placed in that layout.
transaction.add(R.id.fragment_container, firstFragment)
At the end solved the problem using some of the recommendations made in the answers they provided me.
Change content_main.xml content. Using a FrameLayout with its correspodiente id.
Add fragment on onNavigationItemSelected(MenuItem item)
Result
When I press a menu item ...
...the fragment is displayed
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.
I have a FragmentActivity where I'm dynamically adding Fragments in my onCreate. I want a Fragment above a ListFragment. Of course, the first thing I tried was the fragments in one LinearLayout in one ScrollView, but then the ListFragment would only show one row at a time.
I found Scrolling 2 fragment (one Fragment and one ListFragment) as one, but I don't know how to get the parent view of the Fragment to add to the header view of the ListFragment. I obviously get an NPE when call fragment.getView() and fragment.getView().getParent() because I'm still in the onCreate of my activity, so the views haven't been initialized yet.
How can I put a Fragment above a ListFragment?
Create a layout that contains your first fragment, like so:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/content"
android:name="your.fragment.YourFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
And then, in your ListFragment, add this layout as a header, like so:
View header = inflater.inflate(R.layout.widget_user_fragment, null, false);
getListView().addHeaderView(header, null, true);