'How can I make this EditText (comment input box) stick always on top of every kind of keyboard?
I created comments section in a dialog and I want that dialog's height to cover 60% of user's screen in portrait mode I don't want to change that. The dialog is a simple UI layout to show comments and an input EditText field to type your comment. If I focus the input field and the keyboard pops-up this is when thigs are getting weird the keyboard overlaps the input field I need to find a way to fix this issue. This problem doesn't exist in every keyboard (I have an example on image 5) or every screen size for example in the default keyboard of my phone there is no problem everything works fine but many of my friends have the same issue. I came close to a solution but it wasn't the correct thing that I need. If I set dialog's layout height to match_parent
every keyboard works fine with the EditText but I lose the initial height of the dialog which was 60% of the user's screen. I tried to set some elements for adjustment like
<item name="android:windowSoftInputMode">adjustResize</item>
on my themes but that didn't work.
I read many similar topics like the following but nothing worked as well: Keyboard overlapping EditText on click
CODE:
comments_bottom_sheet.xml layout
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/comments_sheet_dialog">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/totalCommentsTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="25 comments"
android:textColor="@color/black"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/commentsRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:clipChildren="false"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/typeYourCommentFrameLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/totalCommentsTextView" />
<FrameLayout
android:id="@+id/typeYourCommentFrameLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/purple_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/commentsRecyclerView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/typeYourCommentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Type your comment"
android:textAlignment="textStart"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:textSize="25sp" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
comments_sheet_dialog.xml drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/white" />
<corners
android:topLeftRadius="60dp"
android:topRightRadius="60dp" />
<padding
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
commentsDialog();
}
});
}
@SuppressLint("ClickableViewAccessibility")
private void commentsDialog(){
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.comments_bottom_sheet);
//Recycler view initialization
// RecyclerView recyclerView = dialog.findViewById(R.id.commentsRecyclerView);
// MaterialTextView totalCommentsTextView = dialog.findViewById(R.id.totalCommentsTextView);
// TextInputEditText typeYourCommentEditText = dialog.findViewById(R.id.typeYourCommentEditText);
// List<String> strings = new ArrayList<>();
// strings.add("comment1");
// strings.add("comment2");
// strings.add("comment3");
// strings.add("comment4");
// strings.add("comment5");
// strings.add("comment6");
// strings.add("comment7");
// strings.add("comment8");
// strings.add("comment9");
// strings.add("comment10");
// strings.add("comment11");
// strings.add("comment12");
// strings.add("comment13");
// strings.add("comment14");
// strings.add("comment15");
// strings.add("comment16");
// CommentsAdapter commentsAdapter = new CommentsAdapter(strings);
// LinearLayoutManager layoutManager = new LinearLayoutManager(dialog.getContext());
// layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// recyclerView.setLayoutManager(layoutManager);
// recyclerView.setAdapter(commentsAdapter);
int height = (int)(MainActivity.this.getResources().getDisplayMetrics().heightPixels * 0.6);
//IF I SET THE HEIGHT TO MATCH_PARENT EVERYTHING WORKS FIND BUT I NEED THE ABOVE HEIGHT
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.show();
}
}
You can ignore recyclerview it just has a textview layout for every item.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|