问题
I have custom dialog which is attached to bottom of the screen. When I focus to the edittext, keyboard opens but it focuses to edittext so covers my button. I want to see the button. If I use same xml in activity it behaves correctly
I tried all of the windowSoftInputMode's but didn't work, I also put my whole view in scrollview it also didn't work.
My dialog XML, (I believe problem is here)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:focusableInTouchMode="true"
android:fitsSystemWindows="true"
android:clickable="true"
android:layout_height="wrap_content" android:id="@+id/linearLayout" android:focusable="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="@color/hard_white"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:animateLayoutChanges="true">
<Button
android:layout_width="0dp"
android:layout_height="48dp"
android:textSize="16sp"
android:textColor="@color/hard_white"
android:lineSpacingExtra="10sp"
android:gravity="center"
android:text="@string/continuee"
android:textAllCaps="false"
android:background="@drawable/radius_background_lipstick"
android:id="@+id/btn_login"
android:enabled="true"
app:fontFamily="@font/akkuratprobold"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"/>
<View android:layout_width="0dp" android:layout_height="1dp"
android:background="@color/greyish"
android:alpha="0.5"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout7" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:id="@+id/view"
android:layout_marginBottom="8dp" app:layout_constraintHorizontal_bias="0.0"/>
<View android:layout_width="0dp" android:layout_height="1dp"
android:background="@color/greyish"
android:layout_marginBottom="24dp"
android:alpha="0.5"
app:layout_constraintBottom_toTopOf="@+id/textView20" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:id="@+id/view2"
app:layout_constraintHorizontal_bias="0.0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="Order type" android:layout_marginBottom="24dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout5"
android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:id="@+id/textView20"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
android:visibility="gone"
app:fontFamily="@font/akkuratproregular"
android:text="Order type"
android:id="@+id/textView24"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"
app:layout_constraintTop_toBottomOf="@+id/view2" android:layout_marginTop="16dp"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="100dp"
tools:layout_editor_absoluteX="0dp"
android:id="@+id/constraintLayout5"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/view">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp" app:srcCompat="@drawable/ic_tick"
android:id="@+id/imageView13" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="24dp" android:layout_marginRight="24dp"
app:layout_constraintTop_toTopOf="@+id/textView23"
app:layout_constraintBottom_toBottomOf="@+id/textView23"/>
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:gravity="center|left"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="Cash"
android:id="@+id/textView23"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="16dp"
android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:gravity="center|left"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="Principal"
android:id="@+id/textView27"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="16dp"
android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/textView23"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
app:layout_constraintBottom_toTopOf="@+id/btn_login"
tools:layout_editor_absoluteX="0dp"
android:layout_marginBottom="32dp"
android:id="@+id/constraintLayout7">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:layout_editor_absoluteX="0dp"
android:id="@+id/layout_principal" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
app:fontFamily="@font/akkuratproregular"
android:text="Price per $100" android:id="@+id/textView28"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/textView29" android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
app:fontFamily="@font/akkuratproregular"
android:text="Accured interest" android:id="@+id/textView32"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView28" android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
app:fontFamily="@font/akkuratproregular"
android:text="Payment" android:id="@+id/textView33"
app:layout_constraintTop_toBottomOf="@+id/vieww" android:layout_marginTop="20dp"
android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
app:fontFamily="@font/akkuratproregular"
android:text="$200,4" android:id="@+id/textView30"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/textView25" android:layout_marginEnd="12dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
app:fontFamily="@font/akkuratproregular"
android:text="$20,4" android:id="@+id/textView31"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView30" android:layout_marginEnd="12dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="Principal Amouth"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"
android:id="@+id/textView29"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="Payment Method"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"
android:id="@+id/textView34" app:layout_constraintTop_toBottomOf="@+id/textView33"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="Cash"
android:id="@+id/textView35"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView36" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
app:fontFamily="@font/akkuratprobold"
android:text="$1000"
android:id="@+id/textView25"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="16dp"
android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"/>
<View android:layout_width="0dp" android:layout_height="1dp"
android:background="@color/greyish"
android:alpha="0.5" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:id="@+id/vieww"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintTop_toBottomOf="@+id/textView32"
android:layout_marginTop="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="@color/lipstick"
android:lineSpacingExtra="7sp"
android:gravity="end"
app:fontFamily="@font/akkuratprobold"
android:text="Add payment method"
android:layout_marginEnd="16dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="16dp" app:layout_constraintTop_toBottomOf="@+id/vieww"
android:layout_marginTop="20dp" android:id="@+id/textView36"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:text="Pound Sterling"
app:fontFamily="@font/akkuratproregular" android:id="@+id/textView17"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView18"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
android:text="Cash amount"
app:fontFamily="@font/akkuratprobold"
android:id="@+id/textView18"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView19"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="13sp"
android:textColor="#747474"
app:fontFamily="@font/akkuratproregular"
android:lineSpacingExtra="7sp"
android:text="Amount you’d like to buy"
android:id="@+id/textView19" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:gravity="center"
android:textSize="30sp"
android:background="#70FFFFFF"
android:textColor="#2d2d2d"
android:textCursorDrawable="@drawable/black_cursor"
android:lineSpacingExtra="-4sp"
app:fontFamily="@font/akkuratproregular"
android:id="@+id/editText2"
android:text="100"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="@+id/textView18"
app:layout_constraintBottom_toBottomOf="@+id/textView17"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textView17"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintHorizontal_bias="0.958" android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textColor="#747474"
android:gravity="center_horizontal"
app:fontFamily="@font/akkuratproregular"
android:text="Senior Secured"
app:layout_constraintBottom_toTopOf="@+id/view2" android:layout_marginBottom="28dp"
android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:id="@+id/textView21"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
android:text="TalkTalk"
app:fontFamily="@font/akkuratprobold"
app:layout_constraintBottom_toTopOf="@+id/textView21" android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"
android:id="@+id/textView22" android:layout_marginTop="29dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"/>
<ImageView
android:layout_width="40dp"
android:padding="5dp"
android:layout_height="40dp"
app:srcCompat="@drawable/aston_martin_logo"
android:id="@+id/imageView9"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="@+id/textView22"
android:background="@drawable/radius_corner_grey"
app:layout_constraintBottom_toBottomOf="@+id/textView21" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
app:layout_constraintVertical_bias="1.0"/>
<ImageView
android:layout_width="25dp"
android:layout_height="0dp" app:srcCompat="@drawable/ic_down"
android:id="@+id/imageView12" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
app:layout_constraintVertical_bias="0.461"
android:layout_marginBottom="4dp"
app:layout_constraintTop_toTopOf="@+id/textView14"
app:layout_constraintBottom_toBottomOf="@+id/textView14"/>
<TextView
android:layout_width="80dp"
android:layout_height="0dp"
android:textSize="16sp"
android:textColor="#2d2d2d"
android:lineSpacingExtra="10sp"
android:gravity="center|left"
app:fontFamily="@font/akkuratprobold"
android:text="Cash"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout5"
android:id="@+id/textView14"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintTop_toBottomOf="@+id/view2" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
My custom dialog
import android.app.Activity
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import com.transitionseverywhere.TransitionManager
import android.view.animation.AnimationUtils
import android.widget.ScrollView
class BottomDialog(private val activity: Activity){
interface BottomDialogCallBack{
fun onDialogDismissed()
}
private val scrollBackground = ScrollView(activity)
private val background = RelativeLayout(activity)
private lateinit var childView: ViewGroup
init {
background.setBackgroundColor(ContextCompat.getColor(activity, R.color.transparent))
val param = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
scrollBackground.addView(background, param)
setCancelable(true)
background.setOnClickListener { dismiss() }
}
fun setView(view: ViewGroup):BottomDialog{//WASSAP
childView = view
setToBottom(view)
return this
}
private fun setToBottom(view: View){
val param = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
//param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, view.id)
background.gravity = Gravity.BOTTOM
background.addView(view, param)
}
fun setCancelable(value: Boolean): BottomDialog{
background.isClickable = value
background.isFocusable = value
return this
}
fun autoTransition() {
autoTransition(childView)
}
private fun autoTransition(view: ViewGroup){
TransitionManager.beginDelayedTransition(view)
}
fun show():BottomDialog{
activity.window.decorView.findViewById<ViewGroup>(android.R.id.content).addView(background)
val animation = AnimationUtils.loadAnimation(activity, R.anim.slide_up)
background.startAnimation(animation)
return this
}
fun dismiss():BottomDialog{
val animation = AnimationUtils.loadAnimation(activity, R.anim.slide_down)
background.startAnimation(animation)
(activity as BottomDialogCallBack).onDialogDismissed()
activity.window.decorView.findViewById<ViewGroup>(android.R.id.content).removeView(background)
return this
}
}
回答1:
Place your button at end of the layout and add this line to your button
android:focusableInTouchMode="true"
回答2:
try to not attach the dialog to the bottom of the screen make the bottom constraint to the top of the button
回答3:
Try This:
<activity android:windowSoftInputMode="adjustResize">
来源:https://stackoverflow.com/questions/55531811/push-whole-dialog-above-the-keyboard-keyboard-covers-button