I have two fragments in an activity:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
class="com.eyecreate.fragment1"
android:id="#+id/fragment1"
android:layout_height="match_parent"
android:layout_width="269dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
<fragment
android:id="#+id/fragment2"
android:layout_toRightOf="#id/fragment1"
android:layout_alignTop="#id/fragment1"
android:layout_alignBottom="#id/fragment1"
android:layout_alignParentRight="true"
class="com.eyecreate.fragment2"
android:layout_height="match_parent"
android:layout_width="match_parent"></fragment>
</RelativeLayout>
fragment1 doesn't show up on screen and isn't on the Hierarchy Viewer, but has an instance in code that still has calls to onCreate run on it. fragment2 shows up and takes up the whole screen. What could be causing it to have just disappeared?
The layout_width and layout_height attributes for the fragment2 is match_parent. Thats the reason it is taking up whole screen.
I think you wanted to use LinearLayout and not RelativeLayout. the android:orientation doesn't make sense for RelativeLayout.
Feeling kinda dumb, I had a call to
getActivity().setContentView(fragment2)
during the onresume of that fragment. That would be causing the fragment2 to be the only one showing up.
Related
How do i proceed with designing an android activity with two fragments such their layout are as follows in landscape and portrait mode respectively. I tried putting two fragments inside a frame layout set as vertical but it didnt work. I tried this with linear layout but didnt get expected result
<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="vertical" >
<fragment
android:id="#+id/titles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
class="com.examples.fragments.Fragment1" />
<fragment
android:id="#+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
class="com.examples.fragments.Fragment2" /></LinearLayout>
A FrameLayout doesn't have an orientation. You should use a LinearLayout. It supports the android:orientation attribute.
And by the looks of your disign you might need to use android:layout_weight with android:layout_height="0dp" (or android:layout_width="0dp") as well.
You should use a LinearLayout with orientation attribute. You can design two different layout. Check this link for this second approach:
http://developer.android.com/training/basics/supporting-devices/screens.html
I'm working on an alternative simplified layout for an app that is used for smaller screens. This layout should not include a map fragment that is used in the larger layout, however, I'm supposed to avoid programmatic changes if at all possible. The activity that shows the layout also does stuff with the fragment using its ID, so I can't just delete it. I tried making the fragment invisible, like this:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
But the fragment remains visible. The same happens when I set the visibility to "invisible". The fragment's default position is partially hidden by a view, but when I move the fragment around the layout, it still shows up as visible wherever I put it.
Is there a reason the fragment ignores the visibility parameter? Is there another non-programmatic solution?
For future reference, dora's answer works, but I've been advised to use FrameLayout instead of LinearLayout, it's simpler and intended for use with a single View inside:
<FrameLayout
android:id="#+id/dummyLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
There's another way that works - changing the width and height of the fragment to 0dp, but this is considered a hack and generally less correct solution.
I am not sure this would meet your requirement but you can add a dummy layout container above the fragment you want to hide and add that layouts visibility to gone/visible, whichever you want. But again when you want to show the fragment, you should also make the fragments dummy parent layout visibility to visible.
<LinearLayout
android:id="#+id/dummyLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I have little bit experience with Android and I'm not sure how to resolve this design problem. Maybe someone know a good solution. I like to build an Activity which look like this:
http://i.stack.imgur.com/CwQaU.png
The user should have to choice to drag the marker on the map or to enter the adress. I like to define the whole layout in XML without to extend from the map fragment.
Have someone a example how I can split the screen 80/20 with two fragments?
Are fragments are the right choice?
Just use a single fragment in this case. In your XML you'll want a LinearLayout so you can utilize weights to achieve the 80/20 you described above.
<LinearLayout>
<MapFragment/>
<EditText/>
</LinearLayout>
Give the linearLayout a "vertical" orientation and assign a weights of 4 and 1 to the children to split them 80/20%.
If this isn't enough to go off, let me know and I can explain further.
Good Luck!
You can use LinearLayout and play with the layout_weight parameter on the fragments to split the screen that way. Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="name"
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="0sp" />
<fragment
android:name="name"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0sp" />
I don't think you need 2 fragments for this.
You can create one main LinearLayout with vertical orientation and inside set two RelativeLayout. One with map fragment and second with edittext.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="0sp" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0sp"
android:background="#999">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center"/>
</RelativeLayout>
</LinearLayout>
I've got an activity with several fragments, in particular:
<LinearLayout
android:orientation="vertical"
android:layout_width="1056dp"
android:layout_height="match_parent">
<fragment
android:layout_width="1056dp"
android:layout_height="0dp"
android:name="FragEntryEdit"
android:id="#+id/itemEditFragment"
tools:layout="#layout/fragment_entry_edit"
android:layout_weight="2"
/>
<ScrollView
android:layout_width="1056dp"
android:layout_height="0dp"
android:layout_weight="1.5">
<fragment
android:layout_width="1056dp"
android:layout_height="match_parent"
android:name="FragPhotos"
android:id="#+id/itemPhoto"
tools:layout="#layout/fragment_photos"
/>
</ScrollView>
</LinearLayout>
The first fragment contain a few EditText controls some of which are multi-line.
When creating, I hide the soft keyboard. When an editText control gets focus, the soft keyboard appears.
However the content of the second fragments (which is beneath the first) is pushed up by the keyboard, overlapping the EditText-controls (the first fragment).
I have tried to set set the 'windowSoftInputMode' to 'AdjustPan', but this doesn't appear to work.
Anybody got any suggestions?
Figured it out!
The problem was not the fragments, but the vertical weighting in the LinearLayouts.
I removed the weighting and wrapped a ScrollView around the first fragment, and it works.
I would suggest to set a background to the fragments. I had a similar problem with overlapping fragment and I solved it setting a white background to the fragments.
my_fragment.xml
<?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:background="#android:color/white"
android:orientation="vertical" >
<!-- MORE XML -->
</LinearLayout>
I'd like to display a simple landscape layout with one list fragment on the left half of the screen and two vertically aligned fragments on the right. Showing all three fragments horizontally works fine, but my layout below shows only the list fragment and not the two others inside the vertical LinearLayout on the right. What am I doing wrong with my layout parameters?
<?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="horizontal">
<fragment class="com.copperykeenclaws.gameplanner.TeamListFragment"
android:id="#+id/team_list_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="wrap_content">
</fragment>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout android:id="#+id/team_details_frame"
android:layout_width="0px"
android:layout_height="wrap_content"/>
<FrameLayout android:id="#+id/team_players_frame"
android:layout_width="0px"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
The FrameLayouts get replaced programmatically in FragmentTransactions, which works fine in the all-horizontal layout.
Both team_details_frame and team_players_frame have widths of 0px, so they'll won't show. Either give them a layout_weight attribute (although nested layout_weights are discouraged, I doubt there will be much of a performance hit in your case) or set them to wrap_content or match_parent.