I have been searching for a long time, but none of the answers were helpful.
I'm making a chat app. When the user clicks on input edit text the keyboard shows up, and I want to automatically scroll to the bottom of scroll view, but it does not work as expected. It looks like it thinks that the scrollview has different dimensions than it really has. (probably doesn't realize that keyboard is taking some space)
When keyboard is hidden, programitacally scrolling to the bottom works as expected.
This is what i have now:
In manifest:
<activity
android:name=".ChatActivity"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait" />
In activity:
messageEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
{
HideKeyboard(v);
// ScrollToBottom ();
}
else
{
ScrollToBottom();
messagesScrollView.post(new Runnable() {
@Override
public void run() {
messageEditText.requestFocus();
}
});
ScrollToBottom();
}
}
});
private void ScrollToBottom ()
{
messagesLinearLayout.requestLayout();
messagesLinearLayout.post(new Runnable() {
@Override
public void run() {
messagesScrollView.scrollTo(0, messagesScrollView.getBottom());
}
});
}
Layout file:
<?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"
android:background="@color/black"
android:clickable="true"
android:focusableInTouchMode="true"
tools:context=".ChatActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.LetsGOFriends.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="66dp"
android:background="#242A2F"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:contentInsetEnd="0dp"
app:buttonGravity="center_vertical"
app:popupTheme="@style/Theme.LetsGOFriends.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|start">
<TextView
android:id="@+id/trainerNameChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/profilePictureImageView"
android:layout_marginStart="10dp"
android:layout_marginTop="-2dp"
android:layout_toEndOf="@+id/profilePictureImageView"
android:maxLines="1"
android:textSize="14sp"
android:text="LoriMontana"
android:textColor="@color/white" />
<TextView
android:id="@+id/trainerCodeChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/profilePictureImageView"
android:layout_alignStart="@+id/trainerNameChat"
android:layout_marginBottom="-2dp"
android:layout_marginStart="0dp"
android:maxLines="1"
android:textSize="14sp"
android:text="0467 0321 3849"
android:textColor="@color/white" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/profilePictureImageView"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="15dp"
android:layout_marginStart="0dp"
android:layout_marginTop="15dp"
android:padding="1dp"
app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
app:srcCompat="@drawable/ic_default_profile_picture"
app:strokeColor="#484848"
app:strokeWidth="1dp" />
<ImageView
android:id="@+id/languageFlagChatImageView"
android:layout_width="18dp"
android:layout_height="16dp"
android:layout_alignBottom="@+id/trainerNameChat"
android:layout_alignTop="@+id/trainerNameChat"
android:layout_marginBottom="4dp"
android:layout_marginStart="5dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="@+id/trainerNameChat"
tools:srcCompat="@tools:sample/avatars" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:id="@+id/messagesScrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="-15dp"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toTopOf="@+id/cardView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout">
<LinearLayout
android:id="@+id/messagesLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusableInTouchMode="true"
android:paddingBottom="20dp"
android:paddingTop="5dp"
android:orientation="vertical"
android:focusable="true" />
</ScrollView>
<androidx.cardview.widget.CardView
android:id="@+id/cardView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout2"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
app:boxBackgroundColor="#242A2F"
app:boxCornerRadiusBottomEnd="15dp"
app:boxCornerRadiusBottomStart="15dp"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp"
app:boxStrokeColor="@color/teal_700"
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:endIconContentDescription="Send"
app:endIconDrawable="@drawable/ic_baseline_send_24"
app:endIconMode="custom"
app:endIconTint="@color/white"
app:hintEnabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Enter your message.."
android:maxLength="250"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="@color/white"
android:textColorHint="#D3D3D3" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
I think it's something wrong with layout file.