SwipeRefreshLayout not obeying constraints - java

I have a SwipeRefreshLayout containing a RecyclerView for a chat activity. I have its bottom constrained to the top of a linear layout at the bottom of the screen:
<android.support.constraint.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="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimaryDark"
android:paddingLeft="0dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="0dp"
android:paddingBottom="0dp"
tools:context="org.andrewedgar.theo.ChatRoomActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/listFooter"
app:layout_constraintBottom_toTopOf="#+id/listFooter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/chatRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:layout_constraintBottom_toTopOf="#+id/listFooter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
android:id="#+id/listFooter"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#drawable/custom_border"
android:gravity="bottom"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="#+id/messageInput"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent"
android:ellipsize="start"
android:gravity="center"
android:hint="#string/prompt_msg"
android:imeActionLabel="#string/action_send"
android:imeOptions="actionUnspecified"
android:inputType="textCapSentences|textAutoCorrect"
android:maxLines="2"
android:textColor="#color/white"
android:textColorHint="#color/hintColor"
android:importantForAutofill="no" tools:targetApi="o"/>
<ImageButton
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:contentDescription="#string/action_send"
android:padding="10dp"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
..but the SwipeRefreshLayout still takes up the whole screen and the messages appear behind the message input:
Is there a special type of constraint required for this to work?

Change the height of SwipeRefreshLayout to 0dp
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="0dp"
Why?
As per the documentation:
You can set the view size to a ratio such as 16:9 if at least one of
the view dimensions is set to "match constraints" (0dp).
Also
Note: You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).

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 increase the scrollview height with gridview inside in android

current -> https://i.stack.imgur.com/OJin0.jpg
how to increase the scrollview height in android
I want to make a screen that scrolls continuously with a gridview in the remaining space below.
code
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/scrollView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="10dp"
android:fillViewport="true"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridView
android:id="#+id/gridView_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:scrollbars="none"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#+id/scrollView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridView_thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:scrollbars="none"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
Try this, it might work.

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"

NestedScrollView transparent inside CoordinatorLayout but: i do not want it to be transparent

here i am asking for your help.
I'm trying to implement a nestedscrollview inside a coordinatorlayout with some other stuff but the nestedscroll view keeps transparent no matter what i do.
I have already thought a lot but couldn't find any solution.
I want it to have a "solid" white background not this (+/- 30% white) background, here as some printscreens:
Before
Then when i start it sliding:
After
As we can see it the background text (that is inside the coordinator but out the nested) still readable and i can even start actions like press the drawer button that should be hidden.
Here is my main layout code which hold the NestedScrollView:
<androidx.coordinatorlayout.widget.CoordinatorLayout 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.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="none"
app:behavior_hideable="true"
app:behavior_peekHeight="64dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<include layout="#layout/fragment_fast_access" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And this is the Fragment i want to show inside it:
<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=".FastAccessFragment">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="288dp" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="64dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="#+id/fastAccess_inCard_coverAlbum"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:contentDescription="#string/album_cover"
android:scaleType="centerCrop"
android:src="#drawable/gradient_bg_main"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/fastAccess_inCard_songName"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginHorizontal="16dp"
android:layout_marginBottom="2dp"
android:ellipsize="end"
android:gravity="bottom"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black"
app:layout_constraintBottom_toTopOf="#id/fastAccess_inCard_artistName"
app:layout_constraintEnd_toStartOf="#id/fastAccess_inCard_buttonPlay"
app:layout_constraintStart_toEndOf="#id/fastAccess_inCard_coverAlbum"
app:layout_constraintTop_toTopOf="parent"
tools:text="Song name" />
<TextView
android:id="#+id/fastAccess_inCard_artistName"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginHorizontal="16dp"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/fastAccess_inCard_buttonPlay"
app:layout_constraintStart_toEndOf="#id/fastAccess_inCard_coverAlbum"
app:layout_constraintTop_toBottomOf="#id/fastAccess_inCard_songName"
tools:text="Artist" />
<ImageButton
android:id="#+id/fastAccess_inCard_buttonPlay"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="#string/button_play"
android:focusable="true"
android:src="#drawable/ic_play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/fastAccess_inCard_buttonPlaylist"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/fastAccess_inCard_buttonPlaylist"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="#string/show_playlist"
android:focusable="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_playlist"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks in advance ;)

Make buttons in GridLayout fill empty space

Need help at stretching my buttons in GridLayout so they would take all of the free space under the TextView
Here is my hierarchy
P.S. source code:
<android.support.constraint.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">
<TextView
android:id="#+id/display"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/display">
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/display" />
check if you are assigning the height and width in px, and change them to % (percentage).. is there any way you can show the source ?
P.S. this is how i fixed it.. i've changed it to Linear
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/re``s-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/display"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/display">
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/display" />
</GridLayout>

Categories

Resources