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>
Related
I'm facing a tricky situation here and I don't know how to solve this problem.
In my project I have a custom BottomSheetDialogFragment and in the layout a FrameLayout to add or replace Fragments.
Now I have a Fragment and inside I have a RecyclerView with the height:="wrap_content" because I want the BottomSheetDialogFragment only use the necessary space. Everything looks great, the problem appear when I put another view inside of the same layout and set the RecyclerView bellow or above of that view.
The RecyclerView ignores the size of the other view (or views) and always grows to the max screen size, and then it's no possible to see a few elements and even scroll.
I saw a solution, some developers are suggesting to add paddingBottom equals to the height of the view. But in my case doesn't works because I want to have a dynamic solution.
Above I'll share a few images of the problem and GitHub Repository with a sample.
Thanks for your attention!
I've manage to do what you need just need to use this as your fragment_sample.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rclItems"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<Button
android:id="#+id/btnAddMoreItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rclItems"
android:text="#string/add_1_item"/>
</LinearLayout>
Explanation
Using a LinearLayout gives you the ability to work with weight, and the vertical orientation allows you to place an item below the other. The weight on the recyclerview will increase the height of it as needed until filling the screen. The next item you add would be added to the recyclerview but you'll need to scroll the list to see it
The android developers blog says that :-
The scrolling containers in your bottom sheet must support nested scrolling .
Try changing your fragment_sample.xml as below to make the recyclerview scroll working and to make the add button persistent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/next"
android:layout_above="#id/btnAddMoreItems"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/rclItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.NestedScrollView>
<Button
android:id="#+id/btnAddMoreItems"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:text="#string/add_1_item"/>
</RelativeLayout>
Note: making bottomsheet layout a child view of CoordinatorLayout will allow you to get the implement BottomSheetBehavior and recieve its transitions callbacks .
I am designing a UI for a calculator, but I want to use different fragments for different parts of the UI displayed simultaneously. However I want stack fragments vertically in a relative layout.
Yet, I want the fragment1 size be set by the size of the controls and not hard-code some height. That way I can easily change the controls of fragment1 without worrying about its total height.
I tried a relative layout but I can't get fragment2 to be displayed below fragment1. Here's what I have tried
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/side_panel"
android:layout_width="match_parent"
<!--this sort of sucks (don't want hadcode 100dp, I want the controls I put in here to resize this automatically.-->
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.guitarv.www.ndigitcalc.SidePanelFragment">
</fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_panel"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_below="#+id/side_panel"
class="com.guitarv.www.ndigitcalc.MainPanelFragment">
</fragment>
</RelativeLayout>
any suggestions in how to make this work?
thx!
I haven't developed for Android in more than a year and I'm a bit rusty with it. I'm trying to setup a kinda simple UI: a bottom bar at the bottom of the screen and a fragment above it (but without filling the whole height). Something like this:
I thought this would be quite simple, but after a while struggling with it I can't manage to make it work without some "hacks".
The bottom bar is also implemented as a Fragment. This is my XML:
<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:orientation="vertical" >
<fragment
android:id="#+id/bottomBar"
android:name="com.mytestpackage.BottomBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/bottomBar"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The Fragment in fragmentContainer is dynamically loaded from code. With the code paste above, fragmentContainer is aligned bottom but it's not above bottom bar, they're overlapped. If I remove the alignParentBottom from fragmentContainer, then it's placed in top of the screen.
Like I said, I found two "hacks" to solve it but I don't like them much:
1- Set a padding/margin bottom to fragmentContainer.
2- Use a filler empty layout on top of the screen and set fragmentContainer to be below that one.
Is there any way to achieve the layout I want without having to use some tricks like the ones I said?
Thanks!
Add to the relative layout:
android:gravity="bottom"
Ah, and android:orientation="vertical" is meaningless for RelativeLayout
A simpler solution would be to use a LinearLayout with vertical orientation and gravity bottom instead of the RelativeLayout.
Can someone tell me if this is possible? I have a RelativeLayout and I want to display two FrameLayouts, one above the another. The one that I want it to be at the bottom, has a width attribute set to wrap_content, and the other should take all the space of its parent above.
This is what I have, and it's not working. The problem is that the layout above is not displayed, only the one at the bottom.
Here is my code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/upper_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/down_container" >
</FrameLayout>
<FrameLayout
android:id="#+id/down_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="4dip" />
</RelativeLayout>
Am I doing something wrong here?
change android:layout_above="#+id/down_container" to android:layout_above="#id/down_container"
#+id/ is use for create id and #id/ is use for give the reference of this particular component.
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.