android developer
android developer

Reputation: 116020

For EditText, how do I let the keyboard not cover it and some other views?

Background

I have an EditText, that when focusing on it, it should show a keyboard and yet show the entire EditText and I might need to include few Views below.

The problem

Due to some requirements, I need the EditText to be of some small text size yet have a minimal height too.

I've made a snippet to make a tiny example of the issue:

<?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:clipChildren="false"
    android:clipToPadding="false" tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/messageEditText" android:layout_width="0px" android:layout_height="wrap_content"
        android:hint="write here" android:minHeight="?attr/actionBarSize" android:textSize="12sp"
        app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:background="" />

</androidx.constraintlayout.widget.ConstraintLayout>

When I focus on the EditText, for some reason I don't see its line:

enter image description here

In the manifest, I have android:windowSoftInputMode="adjustPan" because the requirement is to move things and not resize them.

What I've tried

I tried to add padding, and I tried to add a fake bottom drawable. Those can only add empty space, but will make the line even further from being shown.

The question

Is it possible to have the EditText have this minimal height and textsize, while also letting it line being shown when the keyboard appears?

Is it also possible to even show a bit more below the EditText ? It's not so nice to have the EditText be so near the keyboard. I know that in this snippet I only have the EditText, but imagine I would have more views below it.

Upvotes: 3

Views: 883

Answers (3)

Gabe Sechan
Gabe Sechan

Reputation: 93688

There is no way. THere are only 3 ways of reacting when the keyboard opens:

1)Do nothing 2)Resize 3)Pan, such that the line with the cursor is on screen.

There is no way to do anything else, and no reliable signal sent to the app that the keyboard is showing you can respond to. There especially is no way to ensure the line at the bottom of an EditText is going to be on screen.

The best you can do to try to ensure that is to resize, and carefully layout your content so that the resize has minimal effect on the ui shown. But that may or may not be possible, depending on the specifics of the UI. And you haven't shown us enough of what you're trying to do to give you suggestions on it.

Upvotes: 1

Lenoarod
Lenoarod

Reputation: 3620

you can in the android manifest set windowSoftInputMode.

<activity
android:windowSoftInputMode=["stateUnspecified",
                                       "stateUnchanged", "stateHidden",
                                       "stateAlwaysHidden", "stateVisible",
                                       "stateAlwaysVisible", "adjustUnspecified",
                                       "adjustResize", "adjustPan"] > 
</activity>

In your condition, I think you should use adjustPan. your window size it not change, but the content translates up. the following is the document says about adjustPan :

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

if you don't want the content view layout translate, which makes the content scroll out screen. you can use adjustResize

if you want to know more, you can read the official document

Upvotes: 0

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2718

Use the code in AndroidManifest.xml file:

<activity
        android:name="com.example.MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" />

This code worked for me. Inside the manifest file in activity tag add this attribute:

android:windowSoftInputMode="adjustResize|stateHidden"

Upvotes: 0

Related Questions