Populating Listview causes the other element to partially hide - android

I am new to android layout and having the following problem in my app:
here's the layout for fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_margin="0dp">
<ListView
android:id="#+id/listview_books_to_issue"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.87"
/>
<ImageView
android:id="#+id/imageView_empylist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.87"
android:src="#drawable/ic_empty_flask"
android:visibility="gone"/>
<View
style="#style/Divider"/>
<Button
android:id="#+id/add_newbook_upcoming_issues"
android:layout_width="match_parent"
android:text="New"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="0.13"
android:drawableTop="#drawable/ic_add"
android:background="?android:attr/selectableItemBackground"/>
I turn the ImageView's(imageView_empylist) visibility on when the ListView(listview_books_to_issue) is empty, so far the button add_newbook_upcoming_issues looks good, but when the listview is populated even with a single item the button gets hidden partially in the bottom.
Here's the layout code for the item used to populate list.
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview_upcoming_issues_book_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textAlignment="center"
android:gravity="center_vertical"
android:text="Title"/>
<TextView
android:id="#+id/textview_upcoming_issues_book_author"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textAlignment="center"
android:gravity="center_vertical"
android:text="By:-Author"/>
<TextView
android:id="#+id/textview_to_issue_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textAlignment="center"
android:gravity="center_vertical"
android:text="to be issued on"/>
<TextView
android:id="#+id/textview_upcoming_issues_book_no"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:visibility="gone"/>
</LinearLayout>
Don't know what's causing it, any help?
Thanks in advance :)
PS: I am using weights instead of height so that I can have a fixed ratio of screen for items. If in case this is causing the problem please mention the correct way of doing this.
EDIT found the solution here Limit height of ListView on Android

In your main layout fragment, please use RealativeLayout replace for LinearLayout. Your ListView will be layout_above for these views

Related

RecyclerView fill empty space inside LinearLayout

Background
I have a header and footer (LinearLayout) with a RecyclerView between them.
The problem
I need that the footer stay fixed at the bottom of the screen and the RecyclerView fills the empty space
What I've tried
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="br.com.sigane.coletordifal.activity.EnderecamentoActivity">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/produtos_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Footer" />
</LinearLayout>
</LinearLayout>
Also tried to use ConstraintLayout, but without success
Try this
Add android:layout_height="0dp" and android:layout_weight="1" to the RecyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/produtos_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Footer" />
</LinearLayout>
</LinearLayout>
I don't think you need to have separate linear layouts. I think just one is enough. Just set your weights like this for your recyclerview and footer.
<android.support.v7.widget.RecyclerView
android:id="#+id/produtos_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:scrollbars="vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Footer" />
I recommend using the constraint layout, it is a FrameLayout on steroids. If you are using android studio 3.2, right-clicking on the root element in the design view shows a menu with the option "convert to constraintlayout"
The reason you do not want to nest layouts like this, Each nested element results in additional measure passes, nested layouts are messy and harder to inflate than flat layouts.
Highly recommend spending an hour or so reading about the constraint layout and how to use it. You will not regret it, helped my improve productivity by 2x.
Hope this is helpful.

Layout not showing under listview within ScrollView

I have a layout with 1 layout inside of it which is all inside a ScrollView. I am trying to get the whole screen to scroll with the content inside of the listview then the textviews underneath that listview. I get the whole listview to populate and show correctly but the bottom layout with all the textviews do not show up.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_below="#id/app_bar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="#+id/bannerReceiptLogo"
android:layout_width="170dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/img_logo_receipt_cub" />
<TextView
android:id="#+id/bannerAddressHeader"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#id/bannerReceiptLogo"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="#string/storeHeader"
android:textAlignment="center"
android:textSize="18sp" />
<ListView
android:id="#+id/fullEReceiptListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bannerAddressHeader"
android:layout_marginTop="10dp"
android:scrollbars="none"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/fullEReceiptListView"
android:layout_weight="1">
<View
android:id="#+id/totalDivider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/colorGrey"
/>
<TextView
android:id="#+id/txtSubTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBTOTAL"
android:textSize="20sp"
android:layout_below="#+id/totalDivider"/>
<TextView
android:id="#+id/txtSubTotalFinal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$13.58"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/totalDivider"/>
<TextView
android:id="#+id/txtTaxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAX"
android:textSize="20sp"
android:layout_below="#+id/txtSubTotal"/>
<TextView
android:id="#+id/txtTaxTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$0.80"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/txtSubTotalFinal"/>
<TextView
android:id="#+id/txtCompleteTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:textSize="20sp"
android:layout_below="#+id/txtTaxText"/>
<TextView
android:id="#+id/txtCompleteTotalNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$14.38"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:layout_below="#+id/txtTaxText"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Do not put ListView inside ScrollView. ListView is a widget that is already scrollable. See NestedScrollView from Support library.
If you set the ListView height to wrap_content the height will have the height to comport all items. That way, the below content will be pushed away.
However Android don't have a great support for nested scrolls, it's considered a bad practice. This way, your ListView will not have the native scroll feature, and will need some hack to work as expected.
A better workaround is use the NestedScrollView, so you scroll may work (maybe you will need put your ListView inside another parent to control the height)
change ListView's layout_height to wrap_content

framelayout edittext not expanding as expected

I have a problem in edit text its not getting expanded as I type.
I wanted it to show a maximum of 4 lines but when I click the exittext to type something, its shrinking, not even completely showing a single line.
the code used is.
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Comments"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.28"
android:background="#000000"
android:maxLines="1"
android:paddingEnd="20dp"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_weight="10">
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="11">
<ListView
android:id="#+id/list2"
android:drawSelectorOnTop="false"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:footerDividersEnabled="true">
</ListView>
</FrameLayout>
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/linearLayout">
<EditText
android:id="#+id/enter_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:layout_weight="1"
android:inputType="textMultiLine"
android:maxLines="4"
android:minLines="1"
android:textColor="#android:color/black"
/>
<Button
android:id="#+id/send_button"
android:layout_width="45dp"
android:layout_height="30dp"
android:onClick="sendMessage"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
screen shot of whats happening..
thank you..
You have two framelayouts nested in a linearlayout and they are weighted. The first nested framelayout has a weight of 11
compared to 1. So the edittext will not be able to expand beyond it's the weighted width for the framelayout.
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_weight="10">
The nested frames.
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="11">
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
The total parent layout has been broken in 12 parts. The top frame has 11, the bottom 1. 1 is not much. There is not enough room to display all the text, plus you have a vertical scrollbar. The text will be added and be scrolling as you type. It would be impossible to read as it scrolls in such a short space.
I would even suggest not setting the weight for the bottom frame (it defaults to 0) and then try setting the top one to say 3? and increase it to see what works for you. Please try this, before commenting again.

Swiperefreshlayout with fragment not scrolling up

I have a layout with some preferences. It works correctly in portrait, but not in landscape. I want this layout to refresh with a swiperefreshlayout, and does work. But as i i say in the title, it doesnt scroll up.
Ive been playing with a normal LinearLayout, with a RelativeLayout, using the swiperefreshlayout as the root view, playing with weights, ... but nothing. It either doesnt scroll up, or doesnt show the preferences correctly (only the 1st title). Dont know what else to try.
I should also say, i want this screen to keep a linearlayout at the bottom, fixed there. So, only the preferences would scroll.
Heres what i have right now:
<?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:id="#+id/edittexts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:elevation="8dp"
android:background="#color/primary"
android:visibility="gone"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/setwallpaperbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onActivarClick"
android:text="#string/setwallpaperbuton"
android:visibility="gone"
android:clickable="true"
android:enabled="true"/>
<EditText
android:id="#+id/edittextlocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:textColor="#android:color/white"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:layout_gravity="center"
android:inputType="textMultiLine"
android:maxLines="2"
android:background="#android:color/transparent"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/edittextupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:textColor="#android:color/white"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:layout_gravity="center"
android:inputType="text"
android:background="#android:color/transparent"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/swipelayout"
android:isScrollContainer="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/edittexts">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<fragment
android:name="es.jnsoft.nowweather.PreferencesFragment"
android:id="#+id/preferencesfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#xml/settings"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Thanks in advance.
According to the documentation,
[SwipeRefreshLayout] should be made the parent of the view that will be
refreshed as a result of the gesture and can only support one direct
child.
This clause is violated in your layout.

Make layout clickable

In my app I have several fragments with different ListViews, but for all them I want to use the same empty view, so I created a separate layout with the empty view and added it to the other layouts using
<include
android:id="#+id/empty"
layout="#layout/empty_view"/>
Everything works as expected but now I'm trying to make that view clickable so I can reload ListView when the user taps on the empty view, however I can't make it work. Any idea on how to do it?
This is what I have tried so far:
(empty_view.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="#drawable/error_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#929292"
android:textSize="24sp"
android:text="#string/empty_list_error"/>
</LinearLayout>
fragment_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<Spinner
android:id="#+id/spinner_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_vertical_margin"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
<include
android:id="#+id/empty"
layout="#layout/empty_view"/>
<ProgressBar
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:indeterminate="true"
style="#android:style/Widget.Holo.ProgressBar.Large"/>
<LinearLayout
android:id="#+id/list_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary_color"
android:padding="#dimen/activity_vertical_margin"
android:layout_below="#id/spinner_filter"
android:visibility="gone">
<TextView
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:textColor="#color/white_90pct"
android:textAllCaps="true"
android:text="#string/subject_indicator"/>
<View
android:layout_width="1dp"
android:layout_height="15dp"
android:layout_gravity="center_vertical"
android:background="#color/light_color"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="end"
android:textColor="#color/white_90pct"
android:textAllCaps="true"
android:text="#string/grade_indicator"/>
</LinearLayout>
<ListView
android:id="#+id/semesters_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/list_header"/>
</RelativeLayout>
Java class (onCreateView)
LinearLayout emptyView = (LinearLayout) view.findViewById(R.id.empty);
emptyView.setOnClickListener(this);
I also tried adding the next attributes to the xml but still not working:
android:focusableInTouchMode="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"
Since you have already specified the linearlayout to be clickable, you just need to specify the method for the onClick event.
So add you your linearLayouts xml, the onClick attribute as below: (focusable may be needed too)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:onClick="layoutClicked"
android:focusable="true"
android:gravity="center">
Then in your code have the method to go with it
public void layoutClicked(View v)
{
// Your code here
}
Try emptyView.setOnTouchListener()
Are you including empty_view into another view that blocks descendant focusability?
Since your edit
Your ListView is on top of the empty_view and blocking clickability.
Since your empty view fills its parent, make sure your include tag for your empty view is the last in the other layout so it will be top-most in the z-order.

Categories

Resources