10

I have an EditText (that the user can type numbers in), so when the user clicks on the EditText text box a keyboard with numbers is opened.

This is how it looks when the text box is clicked

as you can see the keyboard hides a small part of the text box.

But when I press a key, for example, 0, it looks ok. This is how it looks after after clicking 0

Is there anything I can do (besides putting the EditText higher) so it will looks like it does in the second picture?

Edit: the .xml code:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weightSum="1">
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true">
        <android.widget.CheckedTextView android:id="@+id/checkedTextView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="17sp" android:text="@string/toString"></android.widget.CheckedTextView>
        <AutoCompleteTextView android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent" android:text="@string/emptyString" android:textSize="17sp" android:gravity="top|left" android:minHeight="62dp">
            <requestFocus></requestFocus>
        </AutoCompleteTextView>
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2">
            <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0.33333333333" android:text="@string/contactsString" android:textSize="17sp" android:id="@+id/contactsButton"></Button>
            <Button android:layout_weight="0.33333333333" android:layout_height="wrap_content" android:text="@string/groupsString" android:layout_width="fill_parent" android:id="@+id/groupsButton" android:textSize="17sp"></Button>
            <Button android:layout_weight="0.33333333333" android:layout_height="wrap_content" android:text="@string/favouritesString" android:layout_width="fill_parent" android:id="@+id/button3" android:textSize="17sp"></Button>
        </LinearLayout>
        <TextView android:id="@+id/textView1" android:text="@string/messageString" android:layout_height="wrap_content" android:textSize="17sp" android:layout_width="fill_parent"></TextView>
        <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_width="fill_parent" android:gravity="top|left" android:minHeight="105dp"></EditText>
        <TextView android:id="@+id/textView2" android:text="@string/repetition" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="17sp"></TextView>
        <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/spinner"></Spinner>
        <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="fill_parent">
            <ImageView android:src="@drawable/button_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/timeET" android:inputType="number" android:layout_width="wrap_content" android:layout_weight="0.4"></EditText>
            <ImageView android:src="@drawable/button_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView2" android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/dateET" android:inputType="number" android:layout_width="wrap_content" android:layout_weight="0.4" android:layout_marginRight="3dp"></EditText>
        </LinearLayout>
        <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentLeft="true">
                <Button android:layout_weight="0.5" android:layout_height="wrap_content" android:text="@string/button_ok" android:layout_width="fill_parent" android:id="@+id/button4" android:textSize="17sp"></Button>
                <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/button5" android:layout_weight="0.5" android:text="@string/button_cancel" android:textSize="17sp"></Button>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

</LinearLayout>
thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
Belgi
  • 14,542
  • 22
  • 58
  • 68

8 Answers8

22

I have tried your XML and yes you are right the problem occur.

To solve the problem I have written this line in my MainActivity.java hope this help to you,And put the layout XML in ScrollView.

Activity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temp);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        final EditText time = (EditText)findViewById(R.id.timeET);
        time.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                time.requestLayout();
                MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

                return false;
            }
        });
        final EditText date = (EditText)findViewById(R.id.dateET);
        date.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                time.requestLayout();
                MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

                return false;
            }
        });
         }

And The XML is Like,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" > 

        <ScrollView android:id="@+id/scrollView1"
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:weightSum="1">
---
---
---
        </ScrollView> 
</LinearLayout> 
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • I will try this later on today or tomorrow morning and I will let you know how this works. thanks! – Belgi Nov 12 '11 at 18:01
  • p.s : did you mean to write date.requestLayout(); at date.setOnTouchListener ? – Belgi Nov 12 '11 at 18:03
  • @Frankenstein hehe well thank you very much for the comment. I wanted to delete my answer now but your comment prevented me! Anyway I answered roughly I thought that a root ScrollView would solve the problem. Seems I am mistaken. Why do we need the root LinearLayout in your answer? I really am not much into the details of this question but I think OnFocus listeners are the right choice. Anyway I can not comment on details I have my weekend to enjoy. Thank you again !! +1 – Sherif elKhatib Nov 12 '11 at 18:45
  • I tried using the code (both for the .xml and the java), the problem that arised in another answer is unfortunately true here : the space between the time and date EditText's is now gone so the buttons are higher. also, the text I had in the edit texts is gone. :/ – Belgi Nov 12 '11 at 23:07
  • For me it was sufficient to set the soft input mode in `onCreate`, I didn't need the `OnTouchListeners` at all. Thanks for that anyway! – domsom Jul 26 '13 at 08:45
  • @YogGuru Sure, Please tell me do you have any scrollview,listview,gridview in the screen and having EditText in it? is there any kind of UI ? – MKJParekh Apr 01 '14 at 11:02
  • @MKJParekh Yes i use Scroll view. EditText is inside it. – Yog Guru Apr 01 '14 at 11:39
  • 1
    @MKJParekh my problem solved by Padding :). I give a padding = 10dp to edittext and it works fine for me.By the way thanks you. – Yog Guru Apr 07 '14 at 11:05
5

Change to ScrollView in this way:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="1" >
        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:layout_width="wrap_content"
                android:orientation="vertical"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout1"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true" >
                <android.widget.CheckedTextView
                    android:id="@+id/checkedTextView1"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:textSize="17sp"
                    android:text="@string/toString" />
                <AutoCompleteTextView
                    android:layout_height="wrap_content"
                    android:id="@+id/autoCompleteTextView1"
                    android:layout_width="fill_parent"
                    android:text="@string/emptyString"
                    android:textSize="17sp"
                    android:gravity="top|left"
                    android:minHeight="62dp" >
                    <requestFocus></requestFocus>
                </AutoCompleteTextView>
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/linearLayout2" >
                    <Button
                        android:layout_height="wrap_content"
                        android:layout_width="fill_parent"
                        android:layout_weight="0.33333333333"
                        android:text="@string/contactsString"
                        android:textSize="17sp"
                        android:id="@+id/contactsButton" />
                    <Button
                        android:layout_weight="0.33333333333"
                        android:layout_height="wrap_content"
                        android:text="@string/groupsString"
                        android:layout_width="fill_parent"
                        android:id="@+id/groupsButton"
                        android:textSize="17sp" />
                    <Button
                        android:layout_weight="0.33333333333"
                        android:layout_height="wrap_content"
                        android:text="@string/favouritesString"
                        android:layout_width="fill_parent"
                        android:id="@+id/button3"
                        android:textSize="17sp" />
                </LinearLayout>
                <TextView
                    android:id="@+id/textView1"
                    android:text="@string/messageString"
                    android:layout_height="wrap_content"
                    android:textSize="17sp"
                    android:layout_width="fill_parent" />
                <EditText
                    android:layout_height="wrap_content"
                    android:id="@+id/editText1"
                    android:layout_width="fill_parent"
                    android:gravity="top|left"
                    android:minHeight="105dp" />
                <TextView
                    android:id="@+id/textView2"
                    android:text="@string/repetition"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:textSize="17sp" />
                <Spinner
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner" />
                <LinearLayout
                    android:layout_height="wrap_content"
                    android:id="@+id/linearLayout3"
                    android:layout_width="fill_parent" >
                    <ImageView
                        android:src="@drawable/button_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/imageView1"
                        android:layout_weight="0.1" />
                    <EditText
                        android:layout_height="wrap_content"
                        android:id="@+id/timeET"
                        android:inputType="number"
                        android:layout_width="wrap_content"
                        android:layout_weight="0.4" />
                    <ImageView
                        android:src="@drawable/button_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/imageView2"
                        android:layout_weight="0.1" />
                    <EditText
                        android:layout_height="wrap_content"
                        android:id="@+id/dateET"
                        android:inputType="number"
                        android:layout_width="wrap_content"
                        android:layout_weight="0.4"
                        android:layout_marginRight="3dp" />
                </LinearLayout>
                <RelativeLayout
                    android:id="@+id/relativeLayout2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/linearLayout4"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentLeft="true" >
                        <Button
                            android:layout_weight="0.5"
                            android:layout_height="wrap_content"
                            android:text="@string/button_ok"
                            android:layout_width="fill_parent"
                            android:id="@+id/button4"
                            android:textSize="17sp" />
                        <Button
                            android:layout_height="wrap_content"
                            android:layout_width="fill_parent"
                            android:id="@+id/button5"
                            android:layout_weight="0.5"
                            android:text="@string/button_cancel"
                            android:textSize="17sp" />
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • button_ok and button4 (thats button Cancel) are no longer in the buttom of the screen, and the keyboeard opens up without a reason... – Belgi Nov 10 '11 at 15:59
  • you have `` on the `autoCompleteTextView1`. So the keyboard must open! `button_ok` and `button4` are the last thing in your layout.. So they must be at the bottom of the screen. – Sherif elKhatib Nov 10 '11 at 16:19
  • The code posted does not open the keyboared. it's true that these button are lower than every other thing that's on the screen but the space between the time and date EditText's is now gone so the buttons are higher . – Belgi Nov 10 '11 at 16:32
  • 1
    @Sherif you awesome! I admire you,and would like to have your comment on my answer above..please..also want to mention your blog for linkify..helped me a lot..thanks :) – MKJParekh Nov 12 '11 at 12:29
4

put the entire view inside a ScrollView and set the android:windowSoftInputMode = adjustPan it will do the trick.

you just need to add this piece of code,

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

Your linear layout here....

</ScrollView>

I have tested it on my HTC Desire and its working fine for me hope it will work for you too.

wasaig
  • 666
  • 5
  • 18
  • Can you help me with how to make the layout a ScrollView ? I'll add the .xml code in a sec – Belgi Nov 09 '11 at 19:50
3

Set android:windowSoftInputMode on the Activity to "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.

Beware of one potential bug when using this technique with a fullscreen activity.

Community
  • 1
  • 1
mik3y
  • 4,214
  • 1
  • 22
  • 29
  • A more visual explanation of the different input modes, including the one suggested above - which should do the trick-, can be found [here](http://developer.android.com/resources/articles/on-screen-inputs.html). – MH. Nov 09 '11 at 06:55
  • Looks exactly like in the first pic :/ – Belgi Nov 09 '11 at 16:55
2

This is a much simpler fix than the accepted answer. The key is the <item name="android:windowSoftInputMode">adjustUnspecified</item> line. Add it to your styles.xml:

<style name="AppTheme" parent="@android:Theme.Holo.Light.DarkActionBar">
    <item name="android:alertDialogTheme">@style/iconPopUpDialogTheme</item>
</style>

<style name="DialogAppTheme" parent="AppTheme">
    <item name="android:dialogTheme">@style/iconPopUpDialogTheme</item>
</style>

<style name="PopUpDialogTheme">
    <item name="android:windowSoftInputMode">adjustUnspecified</item>
</style>
thecoolmacdude
  • 2,036
  • 1
  • 23
  • 38
2

You can give some hints to the system on how to handle this via android:windowSoftInputMode element on the declared activity in the AndroidManifest. Try the "adjustResize" value.

android:windowSoftInputMode

BrianPlummer
  • 164
  • 1
  • 5
1

try changing linear layout to scroll view...so that if keyboard comes above editt text user can scroll and type...

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<RelativeLayout android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="wrap_content"
        android:orientation="vertical" android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:layout_alignParentRight="true">
        <android.widget.CheckedTextView
            android:id="@+id/checkedTextView1" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:textSize="17sp"></android.widget.CheckedTextView>
        <AutoCompleteTextView android:layout_height="wrap_content"
            android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent"
            android:textSize="17sp" android:gravity="top|left"
            android:minHeight="62dp">
            <requestFocus></requestFocus>
        </AutoCompleteTextView>
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/linearLayout2">
            <Button android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:layout_weight="0.33333333333"
                android:textSize="17sp" android:id="@+id/contactsButton"></Button>
            <Button android:layout_weight="0.33333333333"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:id="@+id/groupsButton" android:textSize="17sp"></Button>
            <Button android:layout_weight="0.33333333333"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:id="@+id/button3" android:textSize="17sp"></Button>
        </LinearLayout>
        <TextView android:id="@+id/textView1" android:layout_height="wrap_content"
            android:textSize="17sp" android:layout_width="fill_parent"></TextView>
        <EditText android:layout_height="wrap_content" android:id="@+id/editText1"
            android:layout_width="fill_parent" android:gravity="top|left"
            android:minHeight="105dp"></EditText>
        <TextView android:id="@+id/textView2" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:textSize="17sp"></TextView>
        <Spinner android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/spinner"></Spinner>
        <LinearLayout android:layout_height="wrap_content"
            android:id="@+id/linearLayout3" android:layout_width="fill_parent">
            <ImageView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/imageView1"
                android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/timeET"
                android:inputType="number" android:layout_width="wrap_content"
                android:layout_weight="0.4"></EditText>
            <ImageView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/imageView2"
                android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/dateET"
                android:inputType="number" android:layout_width="wrap_content"
                android:layout_weight="0.4" android:layout_marginRight="3dp"></EditText>
        </LinearLayout>
        <RelativeLayout android:id="@+id/relativeLayout2"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/linearLayout4"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentLeft="true">
                <Button android:layout_weight="0.5" android:layout_height="wrap_content"
                    android:layout_width="fill_parent" android:id="@+id/button4"
                    android:textSize="17sp"></Button>
                <Button android:layout_height="wrap_content"
                    android:layout_width="fill_parent" android:id="@+id/button5"
                    android:layout_weight="0.5" android:textSize="17sp"></Button>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

please make necesscery changes...i have removed strings and drawable src for my convience..u need to change first linear layout to scrollview..try without this android:windowSoftInputMode = adjustPan

Rockin
  • 723
  • 4
  • 25
  • 51
-1

Add this simple line in your Manifest.xml file, this adjustResize will keep your EditText above the SoftKeyboard.

android:windowSoftInputMode="adjustResize|stateHidden"

and according to documentation:

"adjustResize"

The activity's main window is always resized to make room for the soft keyboard on screen.

"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.

for more info visit here.

Rohit Bhati
  • 371
  • 2
  • 11
MEGHA DOBARIYA
  • 1,622
  • 9
  • 7
  • 2
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm May 06 '20 at 08:32