In a Bottom Navigation Activity I've a Fragment with a button, an EditText and a TextView.
The problem is that when I focus the EditText to write with the keyboard it moves up and the button bugs in it. How can I solve?
<?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"
<TextView
android:layout_width="0dp"
android:layout_height="178dp"
android:gravity="start|top"
android:ems="10"
android:id="#+id/output"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
android:autofillHints="" app:layout_constraintVertical_bias="0.386"
app:layout_constraintTop_toBottomOf="#+id/button" app:layout_constraintHorizontal_bias="0.666"
style="#style/Widget.AppCompat.TextView" android:scrollbars="vertical" android:textSize="18sp"
/>
<EditText
android:layout_width="0dp"
android:layout_height="178dp"
android:inputType="textMultiLine"
android:gravity="start|top"
android:ems="10"
android:id="#+id/input"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
android:autofillHints="" android:hint="#string/hint" android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="#+id/button" app:layout_constraintVertical_bias="0.574"
android:scrollbars="vertical"/>
<Button
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.498" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Use this
' android:windowSoftInputMode="adjustPan" '
in AndroidMenifest.xml file
<activity
android:name=".ui.activity.approve_pending.ApprovalsActivity"
android:windowSoftInputMode="adjustPan" />
You will find more information here https://developer.android.com/training/keyboard-input/visibility
Your constraints are probably what's causing this behavior.
Try removing the bottom constraint on your EditText, and setting the top constraint on your button to the bottom of the EditText instead of the top of the parent. I believe that will fix it.
it work for me ,it grateful ,try it
android:windowSoftInputMode="adjustPan"
Related
Please know i a using a constrain layout. The views are properly constrained.
On the layout editor preview , both the buttons and textviews are visible. When I ran the up the textviews are visible but the button is invisible.
See attached layout code and screenshots.
<layout 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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_welcome_instructions"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:fontFamily="#font/roboto"
android:gravity="center"
android:text="#string/welcome_text"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/shoe_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:fontFamily="#font/roboto_mono"
android:text="#string/store_text"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_welcome_instructions" />
<Button
android:id="#+id/instructions_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="264dp"
android:background="#color/colorPrimary"
android:text="#string/welcome_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Thanks in advance.
It seems the issue was Margin Bottom. I have instead used Margin Top.
I have a layout - let's call it layout A - with some EditTexts, which I inflate inside a DialogFragment and all goes well with them. I have another DialogFragment whose layout - let's call it layout B - has a ViewPager2 in it, and I use it with a FragmentStateAdapter which hosts fragments where I reuse layout A for each of them.
Now my problem is that when I tap on the EditTexts inside the ViewPager2, they seem to be getting focus (my grasp of that subject isn't perfect), the cursor starts showing and I can manipulate the text with it ("select", "select all", "cut", etc.), but the soft keyboard won't open up.
In the dialog without the viewPager, those same EditTexts work flawlessly and the keyboard opens up.
In the Android Manifest I have android:windowSoftInputMode="adjustPan"
(I use a single activity in my app).
What could be the problem?
My code:
Layout A:
<?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">
<TextView
android:id="#+id/title_text_view"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/double_margin"
android:layout_marginLeft="#dimen/double_margin"
android:text="#string/title_colon"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#id/title_edit_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/title_edit_text" />
<EditText
android:id="#+id/title_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/double_margin"
android:layout_marginRight="#dimen/double_margin"
android:autofillHints="false"
android:ellipsize="end"
android:hint="#string/enter_track_title"
android:inputType="textCapWords|textMultiLine"
android:maxLines="2"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="#id/position_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/title_text_view"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/position_text_view"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/double_margin"
android:layout_marginLeft="#dimen/double_margin"
android:text="#string/number_dot"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#id/position_edit_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/position_edit_text" />
<EditText
android:id="#+id/position_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin"
android:layout_marginEnd="#dimen/double_margin"
android:layout_marginRight="#dimen/double_margin"
android:layout_marginBottom="#dimen/margin"
android:autofillHints="false"
android:ellipsize="end"
android:hint="#string/enter_track_position"
android:inputType="text"
android:maxLength="6"
android:maxLines="1"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="#id/duration_text_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/position_text_view"
app:layout_constraintTop_toBottomOf="#id/title_edit_text"
tools:ignore="TextFields" />
<TextView
android:id="#+id/duration_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/duration_colon"
android:textStyle="bold"
android:layout_marginBottom="#dimen/double_margin"
app:layout_constraintBottom_toTopOf="#id/colon_text_view"
app:layout_constraintEnd_toEndOf="#id/seconds_picker"
app:layout_constraintStart_toStartOf="#id/minutes_picker"
app:layout_constraintTop_toBottomOf="#id/position_edit_text" />
<NumberPicker
android:id="#+id/minutes_picker"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/double_margin"
android:layout_marginLeft="#dimen/double_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/colon_text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/duration_text_view" />
<TextView
android:id="#+id/colon_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin"
android:text="#string/colon"
android:textSize="42sp"
app:layout_constraintBottom_toBottomOf="#id/minutes_picker"
app:layout_constraintEnd_toStartOf="#id/seconds_picker"
app:layout_constraintStart_toEndOf="#id/minutes_picker"
app:layout_constraintTop_toTopOf="#id/minutes_picker"
app:layout_constraintVertical_bias="0.42000002" />
<NumberPicker
android:id="#+id/seconds_picker"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/double_margin"
android:layout_marginRight="#dimen/double_margin"
app:layout_constraintBottom_toBottomOf="#id/minutes_picker"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/colon_text_view"
app:layout_constraintTop_toTopOf="#id/minutes_picker" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout B:
<?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">
<View
android:id="#+id/edit_track_divider_1"
style="#style/divider"
android:layout_marginTop="#dimen/double_margin"
app:layout_constraintBottom_toTopOf="#id/left_arrow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/left_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/quadruple_margin"
android:layout_marginLeft="#dimen/quadruple_margin"
android:layout_marginTop="#dimen/double_margin"
android:background="#android:color/transparent"
android:contentDescription="#string/left_arrow_image"
android:scaleX="1.5"
android:scaleY="1.5"
app:layout_constraintBottom_toTopOf="#id/edit_tracks_pager"
app:layout_constraintEnd_toStartOf="#id/right_arrow"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/edit_track_divider_1"
app:srcCompat="#drawable/ic_action_left" />
<TextView
android:id="#+id/position_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="#id/left_arrow"
app:layout_constraintEnd_toStartOf="#id/right_arrow"
app:layout_constraintStart_toEndOf="#id/left_arrow"
app:layout_constraintTop_toTopOf="#id/left_arrow" />
<ImageButton
android:id="#+id/right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/quadruple_margin"
android:layout_marginRight="#dimen/quadruple_margin"
android:background="#android:color/transparent"
android:contentDescription="#string/right_arrow_image"
android:scaleX="1.5"
android:scaleY="1.5"
app:layout_constraintBottom_toBottomOf="#+id/left_arrow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/left_arrow"
app:layout_constraintTop_toTopOf="#+id/left_arrow"
app:srcCompat="#drawable/ic_action_right" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/edit_tracks_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/edit_track_divider_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/left_arrow" />
<View
android:id="#+id/edit_track_divider_2"
style="#style/divider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/edit_tracks_pager" />
</androidx.constraintlayout.widget.ConstraintLayout>
I eventually found some "hackish" unsatisfying way to fix this issue... I have added a dummy EditText in the layout right above the ViewPager2 and set its visibility to gone. Now the EditTexts work as expected - i.e. the keyboard opens up. I have no idea why this works and therefore still don't know what caused the problem. I found this after playing around hopelessly for solutions. If anyone knows what caused the problem, why my solution practically solves it and what is the correct way to solve this - please let me know.
In layout, TextView would be created but behind the TextView there should be some words are behind. How should I identify?
This is android studio latest version
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<TextView
android:id="#+id/roomnum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#color/colorBlack"
android:textSize="#dimen/activity_title" />
<TextView
android:id="#+id/roomtype"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#color/color_graylight"
android:textSize="#dimen/activity_title" />
</LinearLayout>
I want behind the words.
Use RelativeLayout to place two texts on top of each other. Then toggle their visibility accordingly.
For example when button is clicked:
textViewFirst.setVisibility(View.INVISIBLE)
textViewSecond.setVisibility(View.VISIBLE)
Seems the question is not very clear. I have sample code here that can help you.
<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">
<!--<RelativeLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content">-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="A"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="SAMPLE TWO"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--</RelativeLayout>-->
</android.support.constraint.ConstraintLayout>
Sample screenshot
I have a strange feeling that your question is asking about a hint. For example:
You have a TextView and expected input is some room in a house. So the TextView will show something like "Room name", it will be in the color of light gray. If somebody types into the TextView text color will be black with his input. Am I right?
EDIT:
After your comment I am sure you are talking about Hint. There is no reson for hint in TextView it is used in EditText to let user know what is the expected input there.
<EditText
android:id="#+id/text_New_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="HERE IS YOUR HINT"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I have an EditText which listens to click events on an activity and launches a fragment using fragmentTransaction.add(); both the activity and the fragment have a ConstraintLayout as a root ViewGroup. The fragment layout is as follows (Some elements has been omitted for brevity):
<?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:id="#+id/fragment_CitySelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="25dp"
android:background="#color/white"
android:clickable="true"
android:focusable="true"
tools:context=".CitySelectionFragment">
<EditText
android:id="#+id/city_AutoCompleteTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#layout/rounded_border_edittext"
android:hint="Ville"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="10dp"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back_ImageButton" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cities_RecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/city_AutoCompleteTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried adding android:windowSoftInputMode="adjustResize" in AndroidManifest and programmatically as of answers in some similar questions.
The problem is caused by some code I added to make the status bar completely transparent, which I found in this answer.
Adding android:fitsSystemWindows="true" to the fragment layout solved the problem.
I have a Fragment and there is a ScrollView in that Fragment.
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appbar">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.tatar.mobile.widget.CardSelectionView
android:id="#+id/card_selection_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:accountRightTextView1="#string/available"
app:accountTitleTextView="#string/from"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/line_text_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="#color/gray.light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/card_selection_view" />
<Spinner
android:id="#+id/type_spinner"
style="#style/spinner"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/line_text_view" />
<Spinner
android:id="#+id/company_spinner"
style="#style/spinner"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/type_spinner" />
<LinearLayout
android:id="#+id/dynamic_form_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/company_spinner" />
<Spinner
android:id="#+id/installment_spinner"
style="#style/spinner"
android:layout_marginTop="8dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dynamic_form_container" />
<Button
android:id="#+id/continue_button"
style="#style/action_button"
android:layout_marginTop="16dp"
android:text="#string/continue_button_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/installment_spinner" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
The LinearLayout with the id of dynamic_form_container in that ScrollView contains form input fields which will are populated via a web service call and there might be up to 6-7 input fields depending on the parameters sent to web service. So, height of the form is kind of high and that is where the problem starts. When I try to enter something to an input field, soft keyboard shows up and some of the other input fields stay under the soft keyboard and scrolling is not possible.
I tried to put this to manifest for the Activity of that Fragment but it did not help.
android:windowSoftInputMode="adjustResize|stateHidden"
I want to be able to scroll when the soft keyboard shows up. How can I fix that issue ?
Any help would be appreciated
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
in onCreateView method worked for me.
I have found solution for this problem here, include this class in your project.
And add this following line in onCreate of activity that contains fragment.
new KeyboardUtil(this,findViewById(R.id.fragment));
Hello Please refer below code
this is work for me..
<RelativeLayout 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">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.constraint.ConstraintLayout>
</ScrollView>
AndroidManifest.xml
<activity
android:name=".ui.activities.Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar" />