How to position a textView above recyclerView? - java

I have a problem related with my xml code. Here is a screenshot from my page: https://i.stack.imgur.com/M6kvA.jpg
In this photo you can see that the Text View which says "Popular Tv Series" is over the recycler view items. I want to position this textView above the recycler view so they dont't cover each other. Here is the xml code for both layout that i am using. I tried playing with the constraints but it just seems that the textView is fixed. Thanks!
fragment_search.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=".ui.SearchFragment.SearchFragment">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/edit_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="8dp"
app:endIconMode="clear_text"
app:startIconDrawable="#drawable/ic_search_bottomnav"
android:hint="#string/searchbar_hint"
app:boxStrokeColor="#color/dark_red"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/light_gray"
android:layout_marginTop="20dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toBottomOf="#+id/edit_text_layout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/search_fragment_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view_recommended"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/text_view_recommended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider"
android:layout_margin="10dp"
android:text="#string/searchFragment_mostPopular"
android:textStyle="bold"
android:textColor="#color/white"
android:textSize="23sp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
item_fragmentsearch_popular.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/movie_image"
android:layout_width="150dp"
android:layout_height="250dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:scaleType="fitXY"
android:layout_margin="5dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

1- Declare textview before recyclerview .
Assign height=0dp to recyclerview, contraints will automatically assign height to it.
Other Note: if you assign match parent width to any view then you dont need to write start to start and end to end contraint.

Related

Add an element outside a ConstraintLayout for my adapter

I'm trying to display cool dot when I want to notify new items. When i set negative margin or translation, the dot is cut, theses littles ilustration will make things a lot clearer:
What i want :
What I get :
Here's my layout xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="240dp"
android:layout_margin="10dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:elevation="1dp"
android:background="#drawable/bg_element_list"
android:foreground="#drawable/effect_ripple"
android:clickable="true"
android:focusable="true">
<ImageView
android:id="#+id/file_img"
android:layout_width="fill_parent"
android:layout_height="210dp"
android:paddingTop="10dp"
android:paddingBottom="5dp"
android:src="#drawable/ic_pdf"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="20sp"
android:paddingEnd="20sp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="teeeeeeeeeeeeeeeeeeeeeeeeeeeeeest"
android:textAlignment="center"
android:textColor="#color/dark_grey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/file_img" />
<ImageView
android:id="#+id/file_new"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_newfile"
android:translationY="-22dp"
android:translationX="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Someone have an idea?
Thanks
Try this with your parameter's
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/profileCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="210dp"
android:scaleType="fitXY"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="teeeeeeeeeeeeeeeeeeeeeeeeeeeeeest"
android:textColor="#727272"
android:textSize="20sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/userImageProfile"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignEnd="#+id/profileCard"
android:layout_marginEnd="-15dp"
android:elevation="8dp"
android:src="#mipmap/ic_launcher"
app:tint="#color/black" />
</RelativeLayout>
On the ConstraintLayout, set
android:clipChildren="false"
This will allow the ImageView to draw outside the ConstraintLayout. See the documentation.
If you use padding on the layout instead of layout_margin, and set clipToPadding as false, things will be able to draw within the padding area. Margin is really for required space outside of the View, padding is space inside it (where it draws its contents)

How to create responsive list item in recyclerView with fixed height and width

So basically I want to create list items like this using grid layout in recyclerView
So if I fixed my height and width of the list item it the content within it is not fitted and also it becomes unresponsive something like this
and if I make its height and width to match parent it becomes responsive but couldn't achieve the required list item that I want
So how can I achieve a layout like the first picture which is responsive as well?
My list item code with fixed height and width
<?xml version="1.0" encoding="UTF-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView1"
android:layout_width="#dimen/_166sdp"
android:layout_height="#dimen/_195sdp"
app:cardCornerRadius="#dimen/_20sdp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp"
android:backgroundTint="#color/light_gray"
android:layout_margin="#dimen/_4sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="#dimen/_120sdp"
android:background="#drawable/list_item_image_view_bg"
android:scaleType="centerCrop"
android:src="#drawable/school_bag"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/light_gray"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView">
<TextView
android:id="#+id/itemName_TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_4sdp"
android:text="School Bag"
android:textColor="#color/blue"
android:fontFamily="#font/poppins_medium"
android:textSize="#dimen/_12ssp" />
<TextView
android:id="#+id/inExchange_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/list_exchange_item_bg"
android:text="Rs. 400"
android:padding="#dimen/_6sdp"
android:textColor="#color/blue"
android:fontFamily="#font/poppins_medium"
android:textSize="#dimen/_8ssp" />
<TextView
android:id="#+id/institutionName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_4sdp"
android:layout_marginBottom="#dimen/_4sdp"
android:text="University of Sindh"
android:textColor="#color/black"
android:textSize="#dimen/_8ssp"
android:fontFamily="#font/poppins_regular"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

Make recycler view to be scrollable vertically

I'm using the following code to create a recycler view. The recyclerview has got a grid layout manager with an item count of up to 2 per row.
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#color/white"
android:clickable="true"
android:focusableInTouchMode="true"
android:focusable="true">
<ImageView
android:id="#+id/backPress"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/hello"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title!"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="#+id/textGoing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle"
android:textSize="#dimen/text_average"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="#+id/hello" />
<com.google.android.material.textfield.TextInputLayout
android:layout_margin="#dimen/dimen_20"
app:layout_constraintTop_toBottomOf="#+id/textGoing"
android:id="#+id/anchor_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxBackgroundColor="#android:color/white"
android:background="#android:color/transparent" >
<EditText
android:id="#+id/anchor_edit_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
app:layout_constraintTop_toBottomOf="#+id/anchor_input"
app:layout_constraintStart_toStartOf="#+id/anchor_input"
android:id="#+id/anchor_hint"
android:layout_below="#+id/anchor_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/add_cat_btn"
app:layout_constraintTop_toBottomOf="#+id/anchor_hint"
app:layout_constraintEnd_toEndOf="parent"
android:layout_below="#+id/anchor_hint"
android:layout_marginTop="#dimen/dimen_20"
android:text="START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"/>
<TextView
android:id="#+id/category_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/add_cat_btn"
android:text="Description"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_rv"
app:layout_constraintTop_toBottomOf="#+id/category_title"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:itemCount="4"
android:layout_margin="#dimen/dimen_20"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/move_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
app:tint="#android:color/white"
app:layout_constraintEnd_toEndOf="#+id/category_rv"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Unfortunately, the recycler view that I have nested within the ConstraintLayout is not scrolling at all. What am I missing? Does constraint layout support the same?
To solve RecyclerView scrolling issue with ConstraintLayout parent change your RecyclerView height from wrap_content to 0dp and add top and bottom constraints like below:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_rv"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/category_title"
app:layout_constraintBottom_toBottomOf="parent"/>
The error comes from
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:itemCount="4"
replace it by
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"

EditText doesn't appear in emulator

I'm creating a simple app for Android - RecyclerView, two Buttons and an EditText. Everything is OK except the EditText - it doesn't appear in the emulator, but it figures in the Design View. I tried different configurations - no luck.
Any help will be greatly appreciated. Thanks in advance,
Ad.
The code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvA"
android:layout_width="411dp"
android:layout_height="652dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="88dp"
android:layout_height="0dp"
android:text="Button"
android:onClick="fur"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/button3"
android:layout_width="88dp"
android:layout_height="48dp"
android:onClick="ed"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button" />
<EditText
android:id="#+id/et1"
android:layout_width="200dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rvA" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try to set height of recycler view 0dp and bottom to top of buttons or editext like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvA"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/button"/>
<Button
android:id="#+id/button"
android:layout_width="88dp"
android:layout_height="48dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/button3"
android:layout_width="88dp"
android:layout_height="48dp"
android:text="Button3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button" />
<EditText
android:id="#+id/et1"
android:layout_width="200dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rvA" />
</androidx.constraintlayout.widget.ConstraintLayout>
I try this on emulator and edittext is displaying fine.

android Seekbar width "match_parent" not working

I'm creating a seekbar in a dialog fragment, and for some reason, the seekbar width is not taking the whole available width. I guess it has something to do with the dialogfragment somehow.
here is my xml file:
<androidx.constraintlayout.widget.ConstraintLayout
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">
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageButton
android:id="#+id/play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#drawable/play_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/seekBar" />
<TextView
android:id="#+id/player_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/play_pause"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/seekBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
you can see that the seekbar width is set to "match parent" but it's not working.
Although it's look good in the preview.
Use RelativeLayout as root layout for dialog XML file like this...
<RelativeLayout 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="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#drawable/play_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/seekBar" />
<TextView
android:id="#+id/player_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/play_pause"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/seekBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>

Categories

Resources