Layout weight with two ListViews - android

I want two set Listviews vertically.
I want them to be 40% and 60% length of vertical screen respectively.
Here is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_weight="0.4"
android:layout_width="match_parent"
android:layout_height="0dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="0dp">
<ExpandableListView
android:id="#+id/listEvents"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>
But this doesn't work.
The first ListView takes all the screen.
I have also tried removing the LinearLayout that embeds the two ListViews, but it didn't work either.
I also tried with android:layout_height="wrap_content" in both Listviews and didn't work.
If I rotate screen to landscape and after back to port again, it works.
It fails the first instance creation.
Any idea on how to solve it?

This layout will work:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="#+id/lvwView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
/>
<ExpandableListView
android:id="#+id/elvEvents"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
/>
</LinearLayout>
Just don't inherit from ListActivity or ListFragment!

Related

Android. Touch listener get mixed between two views

I have a really weird issue. I have a complex linear layout containing 3 children.The first left child is a list (a lot like the list detail pattern of android(two pans)) the second child is a separator view (just a simple view as a line separator) and the third is a Big Image view (https://github.com/Piasy/BigImageViewer)
The problem is when trying to zoom on the right side the recycler view items get clicked
<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="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<LinearLayout
android:id="#+id/total_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:id="#+id/menu_container"
android:layout_width="180dp"
android:layout_height="match_parent"
>
<include
android:id="#+id/list"
layout="#layout/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="#+id/devices_new_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
<View
android:id="#+id/line_seperator"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/grey"
></View>
<RelativeLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.github.piasy.biv.view.BigImageView
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
app:customSsivId="#+id/mSSIV"
app:initScaleType="centerInside"
app:optimizeDisplay="true">
<ImageSSIV
android:id="#+id/mSSIV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.github.piasy.biv.view.BigImageView>
<ProgressBar
android:id="#+id/image_progress_bar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
Check your match_parent attributes so they are not triggered by the screen touches
For whoever if facing this
My main issue was that I was using a Chinese tablet Huawei and whenever you plug it in the pc it goes crazy all the touches are mixed.

Layout, one ViewGroup takes the 60% of the height, and the other one fills the rest

I've bounced around quite a bit and haven't been able to find a solution that matches what i'm trying to do.
I have 2 RelativeLayouts inside a LinearLayout.
I would like the first RelativeLayout to take up 60% of the screen.
I would like the second layout to take up the remainder of the screen.
I am using weights but I'm trying to see if there is a way that I can avoid using a secondary weight on the second RelativeLayout.
The reason for this is that the user has the ability through a Button click to hide the first layout.
In code I do this by setting the visibility of the first RelativeLayout to "Gone".
The problem is if I have weight set to .6 and .4 and sum of 1, when the first one is set to gone, the second one moves up correctly, but its height doesn't increase because it was set to 40%.
Ideally I could have it set to fill the remaining space on the screen and therefore when the first one is hidden, it will now take up all 100% of the screen.
Is this possible or do I need to do more programmatically when I hide the first View to increase the allowed height of the second view?
Hopefully this makes sense, if not let me know and i'll try to clarify further.
I've stripped out some of the contents of the RelativeLayouts for simplicity as well as some ids.
Code below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:breadCrumbNavigationLayout="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:id="#+id/linearlayout1"
android:focusable="true"
android:focusableInTouchMode="true"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/relativeLayout1"
android:layout_weight="0.6">
<FrameLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame1" />
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/progress"
android:indeterminateOnly="true"
android:keepScreenOn="true"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relative2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16dp"
android:gravity="center_horizontal"
android:layout_below="#+id/relativeLayout1" />
<ListView
android:id="#+id/list"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp" />
</RelativeLayout>
</LinearLayout>
You can use Android Support Library's Percentage Relative Layout to achieve this.
Percentage Relative Layout
import percentage support library by adding following line to your build.gradle(app level),
compile 'com.android.support:percent:24.2.1'
And Your XML file will be,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:breadCrumbNavigationLayout="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:id="#+id/linearlayout1"
android:focusable="true"
android:focusableInTouchMode="true"
android:weightSum="1">
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_height="0dp"
android:layout_width="match_parent"
app:layout_heightPercent="60%"
android:id="#+id/relativeLayout1"
>
<FrameLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frame1" />
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/progress"
android:indeterminateOnly="true"
android:keepScreenOn="true"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout1"
android:id="#+id/relative2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16dp"
android:gravity="center_horizontal"
android:layout_below="#+id/relativeLayout1" />
<ListView
android:id="#+id/list"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
Once you set,
relativeLayout1.setVisibility(View.GONE);
you are able to see that the Relative layout 2 will occupy the whole parent space.
I hope it will help you. All the best.

Two RecyclerViews in one layout is not working because one is missing in Android

I am developing an Android app. In my app, I want to use two RecyclerViews in one layout. I used LinearLayout to wrap up RecyclerViews because of this Stack Overflow question (Two RecyclerViews under each other in one layout).
As you can see the answer says, to use LinearLayout and set RecyclerViews height to wrap_content. I followed it. But when I run only one RecyclerView is appear and one is missing.
This is the screenshot:
As you can see, only on RecyclerView is appeared.
This is my XML layout:
<LinearLayout
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
android:id="#+id/ai_rc_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
android:id="#+id/ai_rc_reviews_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
I tried this as well. Nothing appears on screen:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="#+id/ai_rc_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#color/white"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/ai_rc_reviews_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"/>
</LinearLayout>
</ScrollView>
How can I fix my code to use two RecyclerViews in single layout. Is there any better way to do it?
Try using the layout_weight property instead of wrap_content. Give both RecyclerViews same weight and change height to 0dp.
<LinearLayout
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
android:id="#+id/ai_rc_reviews"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
android:id="#+id/ai_rc_reviews_2"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Generally it's not a good idea to have two scroll containers one inside the other if they scroll along the same axis. That behavior will most likely cause confusion with the user. Instead, if you are 100% sure that you need to have two scrolling containers vertically stacked - and please try not to do it if not 100% necessary, you should use fixed heights for each container as it will positively impact overall performance of the created layout.
<ScrollView `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:orientation="vertical"
android:scrollbars="none"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- Usual Size -->
<LinearLayout
android:id="#+id/usualSize_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_f5f5f5"
android:layout_marginLeft="#dimen/common_10"
android:layout_marginTop="#dimen/common_10"
android:orientation="vertical"
>
<TextView
android:id="#+id/usualSize_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="size"
android:textSize="#dimen/font_15"
android:textColor="#color/color_999999"
android:layout_marginTop="#dimen/common_16"
android:layout_marginBottom="#dimen/common_10"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/usualSize_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="#dimen/list_height"
/>
</LinearLayout>
<!-- MarkImage -->
<LinearLayout
android:id="#+id/mark_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_f5f5f5"
android:layout_marginLeft="#dimen/common_10"
android:layout_marginTop="#dimen/common_10"
android:orientation="vertical"
>
<TextView
android:id="#+id/mark_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mark"
android:textColor="#color/color_999999"
android:textSize="#dimen/font_15"
android:layout_marginTop="#dimen/common_16"
android:layout_marginBottom="#dimen/common_10"
/>`
<android.support.v7.widget.RecyclerView
android:id="#+id/mark_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="#dimen/list_height" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Here we should think twice the WRAP_CONTENY ,so we should in the file:build.gradle.
Please update version of a library in gradle file :
compile 'com.android.support:recyclerview-v7:23.2.1'
and higher version.please check your version.
How do I make WRAP_CONTENT work on a RecyclerView
<!-- Usual Size -->
<LinearLayout
android:id="#+id/usualSize_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_f5f5f5"
android:layout_marginLeft="#dimen/common_10"
android:layout_marginTop="#dimen/common_10"
android:orientation="vertical">
<TextView
android:id="#+id/usualSize_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="size"
android:textSize="#dimen/font_15"
android:textColor="#color/color_999999"
android:layout_marginTop="#dimen/common_16"
android:layout_marginBottom="#dimen/common_10"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/usualSize_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="#dimen/list_height"/>
</LinearLayout>
<!-- MarkImage -->
<LinearLayout
android:id="#+id/mark_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_f5f5f5"
android:layout_marginLeft="#dimen/common_10"
android:layout_marginTop="#dimen/common_10"
android:orientation="vertical">
<TextView
android:id="#+id/mark_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mark"
android:textColor="#color/color_999999"
android:textSize="#dimen/font_15"
android:layout_marginTop="#dimen/common_16"
android:layout_marginBottom="#dimen/common_10"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/mark_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="#dimen/list_height"/>
</LinearLayout>
</LinearLayout>
Here we should think twice the WRAP_CONTENY, so we should in the file:build.gradle.
Please update version of a library in gradle file :
compile com.android.support:recyclerview-v7:23.2.1
and higher version. please check your version.
How do I make WRAP_CONTENT work on a RecyclerView
Just in case someone has two RecyclerViews and other views inside one Srcollview,
if you have problem that the first recyclerview won't scroll up or second one part missing,
if you used LinearLayout to contain these RecyclerViews , try to use relativeLayout instead, which solved my problem.

ScrollView filling screen when child textview is too large

I have a LinearLayout with 3 children with weights to set their proportions: 1/7, 3/7, 3/7. The last section contains text that could extend outside the space allowed, so I added a scrollview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical"
android:background="#drawable/package_status_border"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<TextView
android:id="#+id/PackageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
This layout is working fine if the PackageContent text is small:
expected formatting
But, if the PackageContent text is large, rather than maintaining the expected height and just scrolling the extra text it is filling the entire screen:
bad formatting, with scroll view filling entire screen
How to I make sure that the last section is still only 3/7 of the screen, with content scrolling, regardless of how much content the textView contains?
I have also tried putting the ScrollView within its own LinearLayout but that did not work either.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical"
android:background="#drawable/package_status_border"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/PackageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</FrameLayout>
</LinearLayout>

How to assign proportional dimensions to Fragments in a LinearLayout?

I'm trying to get 3 distinct Fragment's displaced vertically at a single Activity.
I'd like to:
Assign a proportional (preferently in percentage) height to each Fragment.
Fragments should take whole device width.
Change it's displacement according to rotation.
This is what my Activity layout looks like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<fragment
android:id="#+id/messageFragment"
android:name="apps.android.expressa.expressa.Fragments.MessageFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/message_fragment_layout" />
<fragment
android:id="#+id/listsFragment"
android:name="apps.android.expressa.expressa.Fragments.ListsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#layout/lists_fragment_layout" />
<fragment
android:id="#+id/bottomBarFragment"
android:name="apps.android.expressa.expressa.Fragments.BottomBarFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
tools:layout="#layout/bottom_bar_layout" />
</LinearLayout>
The problem is that right now, only first and second fragment and displayed and for third only a top is seen. As a note, I'm really new to Android now.
Hope this helps
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:weightSum="6" >
<fragment
android:id="#+id/messageFragment"
android:name="apps.android.expressa.expressa.Fragments.MessageFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="#layout/message_fragment_layout" />
<fragment
android:id="#+id/listsFragment"
android:name="apps.android.expressa.expressa.Fragments.ListsFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
tools:layout="#layout/lists_fragment_layout" />
<fragment
android:id="#+id/bottomBarFragment"
android:name="apps.android.expressa.expressa.Fragments.BottomBarFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
tools:layout="#layout/bottom_bar_layout" />
</LinearLayout>

Categories

Resources