Reputation: 13803
I am trying to add an image view as a banner on top of recycler view. so in some condition I can hide (View.GONE) or to show that banner image view (View.VISIBLE). but the problem is, the banner image view will never show when I run the app, even though I have set android:visibility="visible"
on the image view xml.
as you can see, I have image view with red background, but that red background image view will not displaying
how to solve this ?
the layout in my fragment is like this
and here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.Search.SearchKeywordResultFragment"
android:id="@+id/constraintLayout_search_keyword_fragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
tools:listitem="@layout/item_general_event"
android:id="@+id/recyclerView_keyword_search_result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/imageView_banner_search_keyword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#E91E63"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/recyclerView_keyword_search_result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:src="@tools:sample/avatars[3]" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 1
Views: 1352
Reputation: 6035
layout fixed. there was problem with your bottom approach of recycle view. you are using tools for displaying image it will not show image on device or emulator for that you have to set Image using java/Kotlin code.
imageView.setBackgroundResource(R.drawable.img1)
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.img1));
or if you are getting image form server then try using Glide or anyother image loader Library.
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
tools:listitem="@layout/item_general_event"
android:id="@+id/recyclerView_keyword_search_result"
app:layout_constraintBottom_toTopOf="@+id/imageView_banner_search_keyword"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/imageView_banner_search_keyword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#E91E63"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:src="@tools:sample/avatars[3]" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 87
Try to set android:adjustViewBounds="true"
or android:scaleType="fitCenter"
on your ImageView
Upvotes: 0
Reputation: 163
Because on your imageView have code line tools:src="@tools:sample/avatars[3]"
Tools function it will only show on android layout editor.
try change into real Image from bitmap or from drawable at app:srcCompat="@drawable/{your_drawable_or_mipmap_data_file}"
Upvotes: 1
Reputation: 98
I have used same like this you can try it , it is working
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:shimmer="http://schemas.android.com/apk/res-auto"
android:background="@color/darkgrey"
tools:context=".ui.ProductList.ProductListFragment">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="@dimen/padding_10"
android:text=" Select an item closest to your desired search"
android:textSize="16sp"
android:textColor="@color/black"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/product_list_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginBottom="60dp"
android:scrollbars="vertical" />
</LinearLayout>
</ScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/list_pager"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_gravity="end"
android:visibility="invisible"
android:background="@color/white"
android:gravity="bottom|center"
/>
</RelativeLayout>
On the place of viewpager you can use Imageview.
Upvotes: 0
Reputation: 896
Just change your Recycler's TopToTop constrains to the id of the Image view and change it to TopToBottom. For doing so you also need to declare the imageview first so you'll be able to address its Id. Also give the banner width:match_parent
<ImageView
android:id="@+id/imageView_banner_search_keyword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#E91E63"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/recyclerView_keyword_search_result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:src="@tools:sample/avatars[3]" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
tools:listitem="@layout/item_general_event"
android:id="@+id/recyclerView_keyword_search_result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="imageView_banner_search_keyword" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
Upvotes: 0