Java Android - SwipeRefreshLayout ListView hides the last result - java

why my last row is hidden in listview?
Hello, why my last row is hidden in listview?
Hello, why my last row is hidden in listview?
I have changed the xml code 1000 times already and still can't fix it.
SwipeRefreshLayout work good.
enter image description here
fragment_list.xml
`
<?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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:divider="#null"
android:dividerHeight="0dp">
</ListView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/empty"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/empty_list_text" />
</RelativeLayout>
`
list_row.xml
`
<?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"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rippleColor="#android:color/transparent"
card_view:cardElevation="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:strokeWidth="0dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="194dp"
app:srcCompat="#drawable/image_list"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.SelectedCornerRadius"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline6" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceBody2" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:layout_marginTop="12dp"
android:textAppearance="?attr/textAppearanceBody2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:layout_gravity="center"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_check_region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="#string/check_list" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/check_delete" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
`
I have no idea how to fix it -.-

The ListView's height is MATCH_PARENT which constrains it to the height of its parent ViewGroup. You should always define the height of scrollable views like ListView as WRAP_CONTENT so that it can freely expand according to the number of children.
Also it might be a good idea for the parent LinearLayout to be anchored to the top of the RelativeLayout:
android:layout_alignParentTop="true".
Leaving views floating freely in RelativeLayouts may have surprising consequences.

Related

adView is not visible with wrap content

In the XML layout, everything seems perfect. When I used the emulator or real device, the adView is not visible. I tried to give height to adView like 200 dp. Then the adView is visible. But, when I gave the value of wrap_content for the height of the adView, there is a problem. I used LinearLayout and RelativeLayout but the problem is the same. What should I do for this?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="#string/nav_contact_us_content_explanation" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:gravity="center"
android:selectAllOnFocus="false"
android:text="#string/nav_contact_us_content_mail" />
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adview_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
</LinearLayout>
Try to put AdView in FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="#string/nav_contact_us_content_explanation" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:gravity="center"
android:selectAllOnFocus="false"
android:text="#string/nav_contact_us_content_mail" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.ads.AdView
android:id="#+id/adview_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
</FrameLayout>
</LinearLayout>
In the case of LinearLayout, as the weight was set to 1, it took the whole screen when the ad was not yet loaded. However, you should see a warning like the following in your logcat here.
W/Ads: Not enough space to show ad. Needs 320x50 dp, but only has 288x495 dp.
In order to fix the problem, you might consider using a RelativeLayout instead of a LinearLayout like the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adview"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="Contact us" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:gravity="center"
android:selectAllOnFocus="false"
android:text="Email" />
</LinearLayout>
<LinearLayout
android:id="#+id/adview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<com.google.android.gms.ads.AdView
android:id="#+id/adview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />
</LinearLayout>
</RelativeLayout>
On the other hand, you might also consider setting up a fixed size for the AdView so that it does not hide when the content is not yet loaded. You can get the sizes of different types of ads for different screen sizes here.

Android linearLayout children not expanding to fill parent

I have the following as a layout of mine:
<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=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
This makes my edittext and ratingbar be pushed up to the top and both of the heights are just 'wrap_content'. I have tried with RelativeLayouts as well and all that happens is the EditText takes up the entire screen and pushes the ratingbar off the screen.
Why are the layout weights not working as expected? If edittext weight is 3 and the ratingbar is 1 I would assume the edittext would take 75% of the screen, and so forth?
The background is a 9patch image so it should have no problem expanding either.
Thank you
I believe you want something like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:ems="10"
android:id="#+id/editText"
android:layout_weight="0.53" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53">
<RatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ratingBar"/>
</RelativeLayout>
</LinearLayout>
use Relative Layout to center object:
<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=".MyActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
</RelativeLayout>
Change LinearLayout's
android:layout_height="wrap_content"
to
android:layout_height="match_parent".
Also, instead of setting "0dp" to EditText and RatingBar set it to match_parent.
[Update]
<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=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"
android:rating="4"
android:stepSize="1" />
</LinearLayout>
</LinearLayout>

XML layout 2 views in centre, one at top

I created linearlayout with two buttons in center (buttons 1 and 2), but I can't manage it to create the view 3 on top, which I need to make it fill whole width and adjust it's height by it (keep proportions). How do I create third view?
Image:
Code:
<?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:background="#drawable/bg"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<ImageButton
android:background="#drawable/btn_one"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
<ImageButton
android:background="#drawable/btn_second"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
</LinearLayout>
I would recommend you use relative layouts, they are the closest thing in android to an "absolute layout" replace the button tags with Image button tags
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
try this code set height and width what size of image button you need and simple look what you need.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical"
android:gravity="center">
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp"/>
<ImageButton
android:layout_width="200dp"
android:layout_height="50dp" />
</LinearLayout>

Overlaying textview with imageview linear layout

I've got a few linear layouts stacked on top of each other, the centre one has an imageview. I'm trying to get a textview on top of the imageview in the centre however I'm having trouble. I've tried with relative layout however it messes up all my other layouts. Any simple solutions to this?
EDIT ----
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<ImageButton
android:id="#+id/btnBck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical" >
<ImageView
android:id="#+id/picPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:contentDescription="#string/imageDesc"
android:src="#null" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<ImageButton
android:id="#+id/btnAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/btnDecline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
I'm trying to get the text view on top of the id/picPreview image view
LinearLayout is designed to place layouts/views in a row (linearly).
One solution would be something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/picPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:contentDescription="#string/imageDesc"
android:layout_centerInParent="true"
android:src="#null" />
<TextView
android:id="#+id/picPreviewLbl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Overlay" />
</RelativeLayout>
</LinearLayout>
Why not just use a TextView in place of the centered ImageView and then set the background of the text be the solid color that you are currently using? Eg.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:text="Sample Text" ... />

Setting a button at the bottom without textview going with it

I'm having a problem with my button I have set it to the bottom succesfully but when I set it the textview I have goes down with it too all I want is the button only to be at the bottom and textview stay at its position which is center without following the position of the bottom I hope you can help me...
thanks in advance
this is my first.xml
<?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:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
android:gravity="center"
android:orientation="vertical"
tools:context=".FirstActivity" >
<TextView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:paddingTop="10sp"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)"
android:textIsSelectable="true"
android:textSize="25sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="Button" />
</LinearLayout>
if you want to button at bootm then try
android:layout_gravity="bottom|center_horizontal"
instead of only gravity
in button and if you want to text center then
android:layout_gravity="center"
Simply layout gravity assign your layout at perticular position and gavity assign it's content in perticular position in own layout
The simplest solution is using FrameLayout and it's android:layout_gravity attribute.
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
tools:context=".FirstActivity" >
<TextView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="top|center_horizontal"
android:paddingTop="10sp"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)"
android:textIsSelectable="true"
android:textSize="25sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="Button" />
</FrameLayout>
This solves the problem but this solution is not very flexible. If you want more flexibility for future changes, you could do a similar thing using RelativeLayout instead of FrameLayout.
try this
-------
<?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:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
android:orientation="vertical"
tools:context=".FirstActivity" >
<LinearLayout
android:id="#+id/lnrfirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginTop="20dp"
>
<TextView
android:id="#+id/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:paddingTop="10sp"
android:text="قال (عليه السلام): (من كرمت عليه نفسه هانت عليه الدنيا)"
android:textIsSelectable="true"
android:textSize="25sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>

Categories