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!
Related
I want to change layouts dynamically in a persistent bottom sheet, not the modal one.
I have 4 layouts and have to show those layouts in the bottom sheet step by step.
Is it a good idea to use a fragment container in bottomsheet like the below one and add fragments in it one by one?
or I have to use a layout container and then add other layouts using addView and removeView.
The layouts have different heights so I want to adjust the bottomsheet height according to the height of every layout when they show.
I checked some examples using Modal bottom sheet and fragments. But I need it in a persistent one like google maps. So I am not sure if fragments will cause any problems in a persistent sheet and if there are better and simple solutions than fragments.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:padding="12dp"
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
app:layout_insetEdge="bottom"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<View
android:layout_width="40dp"
android:layout_height="4dp"
android:background="#drawable/circle_background"
android:layout_gravity="center_horizontal"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/bottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginTop="15dp"/>
</FrameLayout>
The main layout for an app I'm writing is on RelativeLayout with a LinearLayout and a FrameLayout inside. In the LinearLayout, there's the buttons and text and stuff the user actually interacts with. Inside the FrameLayout, there's a graphic. The FrameLayout stays in the bottom-right corner and the LinearLayout is at the top.
When displayed on most different size screens, this layout works great. However, there is one device whose screen is too small and just an annoying little bit of this graphic shows. Is there a way to automatically disable an object if there isn't enough room for it in the XML? I know I could programmatically calculate it, but I'm wondering if there's a better way
Here's a the relevant parts of my layout file:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/buttonLayout">
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buttonLayout">
<ImageView
android:layout_width="139dp"
android:layout_height="48dp"
android:layout_gravity="bottom" />
</FrameLayout>
</RelativeLayout>
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 an activity Which has Text to show then Some content under that then a list view.
While some content could be a picture, relative layout, Video control, or a linear layout. Depending upon the data it got from previous activity.
Is that possible to do that or I have to make separate layouts for all items?
You need a layout like this:
<?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="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<FrameLayout
android:id="#+id/flSpecialContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
You can add any View you want in code to the FrameLayout (flSpecialContainer) based on what you need.
you can create all possible items in one layout and set visible and gone them depend on data you receive
in this case you don't need create them in code , and can preview activity layout in design mode.
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.