How to make a fragment height take up all space left - android

I have an activity with two fragments A and B. Both have widths set to match_parent. Fragment B has a height of 200dp and is underneath Fragment A. I would like the height of Fragment A to fill up whatever space remains in the activity. How can I achieve this? Any hints will be highly appreciated.
I have posted my activity_main.xml
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="#+id/image_showing_list"
android:name="com.example.gallery.ImageShowingList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<fragment
android:id="#+id/image_showing_list_list"
android:name="com.example.gallery.ImageShowingList"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>

If they are in a vertical LinearLayout, then assign android:layout_height="0dp" and android:layout_weight="1" to Fragment A.

make your linear layout android:orientation="vertical" and watch the magic happen

Related

How to achieve that background image doesn't get stretched on linear layout

I have a linear layout with a background image.
I need to scroll view before I create the linear layout.
After I do that, my image on linear layout is getting stretched.
Can you help me to create my image so it doesn't get stretched?
This is my code on .xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
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:layout_width="match_parent"
android:layout_height= "wrap_content"
android:gravity="center_horizontal"
android:background="#drawable/bg_image_login"
android:orientation="vertical"
android:windowSoftInputMode="stateVisible|adjustResize"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.fania.suara_project.LoginActivity">
<ImageView
android:id="#+id/haha"
android:background="drawable/bg_image_login"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Help me please. Thank You :)
put scrollview background
android:background="#drawable/bg_image_login"
instead of Linearlayout.

layout_weight is not working in scroll view

I am trying to use this code but its not working when i put layout weight.If i remove layout weight and enter height for image view,it works fine.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:id="#+id/informative_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.7"
android:scaleType="fitXY"></ImageView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_option_menu"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.3" />
</LinearLayout>
</ScrollView>
Actually you don't need scrollview. Remove it. And if you are using recyclerview inside scrollview then you need to specify height in dp of list to make it work properly.

Cannot add weight param to fragment in android studio?

I am trying to add weight param to fragment xml in android studio so that my two fragements can take half screen each but adding weight have no effect on the layout. what interesting is i still can add weight to normal activity views here is the screen shot
Here is 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.designcoder.vivz.lifecycle.xml.ActivityFragmentXml">
<fragment
android:id="#+id/fragment_xml_a"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout="#layout/fragment_xml_a"
/>
<fragment
android:id="#+id/fragment_xml_b"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlB"
android:layout_width="match_parent"
android:layout_below="#id/fragment_xml_a"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_xml_b"
/>
</RelativeLayout>
remember that: weight param can only work in linearlayout. if you want to let the two fragments share the screen in horizontal, make the orientation of linearlayout horizontal and make the width of each fragment "0dp" or "wrap_content" so that you can weight them. The same way in vertical. You can look my codes below for example.
the weight example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.dysaniazzz.testdemo.MainActivity">
//the background only to distinguish the fragment
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_red_light" />
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_green_light" />
</LinearLayout>
You try using code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="cungxunu.cunghoangdao.cheng.myapplication.MainActivity">
<fragment
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<fragment
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</LinearLayout>
You should not use Relative layout as weight would not work there.
So User Linear layout for that.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:weightSum="2"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.designcoder.vivz.lifecycle.xml.ActivityFragmentXml">
<fragment
android:id="#+id/fragment_xml_a"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlA"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="#layout/fragment_xml_a"
/>
<fragment
android:id="#+id/fragment_xml_b"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlB"
android:layout_width="match_parent"
android:layout_below="#id/fragment_xml_a"
android:layout_height="0dp"
tools:layout="#layout/fragment_xml_b"
/>
</LinearLayout>

Android ScrollView height is too short

I'm new to Android have this problem making a scrollview that is supposed to scroll over a ListView of 10+ items.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.naqishop.naqi.MainActivity"
tools:showIn="#layout/activity_main">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:entries="#array/options" />
</ScrollView>
</LinearLayout>
The problem is that I can not extend scroll views to show more that one item at a time. I tried to tamper with layout_height of the scrollview (like assigning the value to 500dp)
Also I tried adding
android:fillViewport="true"
as suggestd here
but it did not change the height. So appreciate your hints.
This is because you've set android:layout_height="wrap_content". Replace that with
android:layout_height="match_parent" and it'll take up the whole height.

What is the best way to create this Layout in Android?

I have been struggling for hours to get this layout to work.
Here is my code:
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainPreviewActivity">
<fragment
android:name="com.apps.foo.CleanPreviewFragment"
android:id="#+id/clean_preview_fragment"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<fragment
android:name="com.apps.foo.pointop.DirtyPreviewFragment"
android:id="#+id/filters_fragment"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<fragment
android:name="com.apps.foo.pointop.ProcessedPreviewFragment"
android:id="#+id/processed_preview_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
Every fragment is a simple RelativeLayout (all have the same view):
<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="com.apps.<whatever the fragment name is>">
</RelativeLayout>
Now I want make it work like this:
1) No nested layout_weight
2) No Nesting at all (for example nest the 2 first fragments etc
etc)
3) Not using code to do it programmatically after the view has
rendered.
In my opinion the cleanest most readable way of doing this, would be to set the orientation of fragment 1 and fragment 2 to horizontal, and fragment 3 to vertical, but it does not work.
I've also checked this answer,
but the guy, uses a layout_weight with a RelativeLayout. RelativeLayouts do ignore weights this will not work.
Any help will be appreciated?
You can add a dummy view of 0dp width and height in the center of the layout using a RelativeLayout, and position your Fragments accordingly:
<RelativeLayout
... >
<View
android:id="#+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<fragment
android:layout_toLeftOf="#id/center_anchor"
android:layout_above="#id/center_anchor"
... />
<fragment
android:toLeftOf="#id/center_anchor"
android:layout_below="#id/center_anchor"
... />
<fragment
android:toRightOf="#id/center_anchor"
... />
</RelativeLayout>

Categories

Resources