简体   繁体   中英

The keyboard is coming upon the EditText field & thus hiding it. How to prevent the keyboard from hiding the EditText field?

My EditText is at the bottom of screen, so when I'm tapping on it to edit it, the keyboard is hiding it. I want that when I tap on EditText, the keyboard should remain below it & should not hide it.

Here's the link to the screenshot showing EditText before tapping on it: http://imgur.com/cgme1HT

Here's the link to the screenshot after tapping on EditText ( the EditText field is below the keyboard now ): http://imgur.com/nloFrkn

Here's my SettingUpUserProfile.java file's code :

public class SettingUpUserProfile extends AppCompatActivity {

    public static final int TAKE_PHOTO_REQUEST = 0;
    public static final int PICK_PHOTO_REQUEST = 1;
    private static final int RESULT_LOAD_IMG = 2;
    String imgDecodableString;
    protected ImageView userProfilePicture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_up_user_profile);

        userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
        userProfilePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
                builder.setTitle(null);
                builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position) {
                        switch (position) {
                            case 0:
                                Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
                                break;
                            case 1:
                                Intent chooseFromGalley = new Intent(Intent.ACTION_GET_CONTENT);
                                chooseFromGalley.setType("image/*");
                                startActivityForResult(chooseFromGalley, PICK_PHOTO_REQUEST);
                                break;
                        }
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));

                ImageView imageView = (ImageView) findViewById(R.id.userProfilePicture);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }
}

Here's my activity_setting_up_user_profile.xml file's code :

<RelativeLayout 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:background="@color/light_purple"
    tools:context="com.abc.xyz.SettingUpUserProfile">

    <TextView
        android:id="@+id/settingUpUserProfileText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="@string/settingUpUserProfileText1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center_horizontal|center_vertical"/>

    <ImageView
        android:id="@+id/userProfilePicture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/settingUpUserProfileText1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="100dp"
        android:clickable="true"
        android:src="@drawable/ic_face_white_48dp" />

    <TextView
        android:id="@+id/settingUpUserProfileText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userProfilePicture"
        android:layout_marginTop="5dp"
        android:text="@string/settingUpUserProfileText2"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:gravity="center_horizontal|center_vertical"/>

    <EditText
        android:id="@+id/userName"
        android:background="@drawable/phone_number_edit_text_design"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settingUpUserProfileText2"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:gravity="center_horizontal|center_vertical"
        android:hint="@string/hint_userName"
        android:textColor="@color/white"
        android:textColorHint="#E0E0E0"
        android:textCursorDrawable="@null"
        android:inputType="textPersonName"/>

    <Button
        android:id="@+id/buttonAllSet"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userName"
        android:layout_marginTop="20dp"
        android:text="@string/button_allSet"
        android:textStyle="bold"
        android:textColor="@color/light_purple"
        android:layout_marginEnd="120dp"
        android:layout_marginStart="120dp"
        android:gravity="center_horizontal|center_vertical"/>

</RelativeLayout>

I really have no clue about what to do here!

Please let me know.

I'm new to StackOverflow, so please cooperate.

Thanks in advance.

I had the same issue where the softkeyboard was on top of the EditText views which were placed on the bottom of the screen. I was able to find a solution by adding a single line to my AndroidManifest.xml file's relevant activity. Put layout inside a ScrollView.

android:windowSoftInputMode="adjustResize|stateHidden" This is how the whole activity tag looks like:

<activity
        android:name="com.my.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" >
    </activity>

Here the most important value is the adjustResize. This will shift the whole UI up to give room for the softkeyboard.

Options-1

Try using android:windowSoftInputMode="adjustPan" in activity in AndroidManifest.xml

Options-2

Use ScrollView as parent in xml. Replace your xml with this:

<ScrollView 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"
    tools:context="com.abc.xyz.SettingUpUserProfile" >

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/light_purple">

    <TextView
        android:id="@+id/settingUpUserProfileText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="@string/settingUpUserProfileText1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center_horizontal|center_vertical"/>

    <ImageView
        android:id="@+id/userProfilePicture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/settingUpUserProfileText1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="100dp"
        android:clickable="true"
        android:src="@drawable/ic_face_white_48dp" />

    <TextView
        android:id="@+id/settingUpUserProfileText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userProfilePicture"
        android:layout_marginTop="5dp"
        android:text="@string/settingUpUserProfileText2"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:gravity="center_horizontal|center_vertical"/>

    <EditText
        android:id="@+id/userName"
        android:background="@drawable/phone_number_edit_text_design"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settingUpUserProfileText2"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:gravity="center_horizontal|center_vertical"
        android:hint="@string/hint_userName"
        android:textColor="@color/white"
        android:textColorHint="#E0E0E0"
        android:textCursorDrawable="@null"
        android:inputType="textPersonName"/>

    <Button
        android:id="@+id/buttonAllSet"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userName"
        android:layout_marginTop="20dp"
        android:text="@string/button_allSet"
        android:textStyle="bold"
        android:textColor="@color/light_purple"
        android:layout_marginEnd="120dp"
        android:layout_marginStart="120dp"
        android:gravity="center_horizontal|center_vertical"/>

</RelativeLayout>

</ScrollView>

只需使用ScrollView作为xml文件中的父视图。

    <?xml version="1.0" encoding="utf-8"?>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.abc.xyz.SettingUpUserProfile" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/light_purple" >

            <TextView
                android:id="@+id/settingUpUserProfileText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText1"
                android:textColor="@color/white"
                android:textSize="30sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/userProfilePicture"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_below="@+id/settingUpUserProfileText1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="100dp"
                android:clickable="true"
                android:src="@drawable/ic_face_white_48dp" />

            <TextView
                android:id="@+id/settingUpUserProfileText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userProfilePicture"
                android:layout_marginTop="5dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText2"
                android:textColor="@color/white"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/settingUpUserProfileText2"
                android:layout_marginEnd="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="80dp"
                android:background="@drawable/phone_number_edit_text_design"
                android:gravity="center_horizontal|center_vertical"
                android:hint="@string/hint_userName"
                android:inputType="textPersonName"
                android:textColor="@color/white"
                android:textColorHint="#E0E0E0"
                android:textCursorDrawable="@null" />

            <Button
                android:id="@+id/buttonAllSet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userName"
                android:layout_marginEnd="120dp"
                android:layout_marginStart="120dp"
                android:layout_marginTop="20dp"
                android:background="@color/white"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/button_allSet"
                android:textColor="@color/light_purple"
                android:textStyle="bold" />
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

Thank you all for your answers.

I solved the problem by changing my activity_setting_up_user_profile.xml file's code to this:

<?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"
    android:background="@color/light_purple"
    tools:context="com.abc.xyz.SettingUpUserProfile" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/settingUpUserProfileText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText1"
                android:textColor="@color/white"
                android:textSize="30sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/userProfilePicture"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_below="@+id/settingUpUserProfileText1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="100dp"
                android:clickable="true"
                android:src="@drawable/ic_face_white_48dp" />

            <TextView
                android:id="@+id/settingUpUserProfileText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userProfilePicture"
                android:layout_marginTop="5dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText2"
                android:textColor="@color/white"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/settingUpUserProfileText2"
                android:layout_marginEnd="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="80dp"
                android:background="@drawable/phone_number_edit_text_design"
                android:gravity="center_horizontal|center_vertical"
                android:hint="@string/hint_userName"
                android:inputType="textPersonName"
                android:textColor="@color/white"
                android:textColorHint="#E0E0E0"
                android:textCursorDrawable="@null" />

            <Button
                android:id="@+id/buttonAllSet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userName"
                android:layout_marginEnd="120dp"
                android:layout_marginStart="120dp"
                android:layout_marginTop="20dp"
                android:background="@color/white"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/button_allSet"
                android:textColor="@color/light_purple"
                android:textStyle="bold" />

        </RelativeLayout>
    </ScrollView>
    </LinearLayout>

This was COOL!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM