ip696
ip696

Reputation: 7084

How raised EditText above the keyboard if it open?

I use theme

<style name="Theme.TransparencyDemo" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

And some form on the layout. Form has some EditTexts. If editText has focus opens keybord. I want raised above the keyboard my editText but it not work. And all my layout rises. It is wrong.My layuot:

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

<RelativeLayout 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"
                android:background="@drawable/background_purple"
                android:orientation="vertical"
                android:weightSum="1">

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="38.50dp"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/imageMenu"
            android:layout_width="43dp"
            android:layout_height="43dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="14.50dp"
            android:layout_weight="1"
            android:src="@drawable/back_login"
            />

        <com.skip.client.customer.utils.MyTextView2D
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Back"
            android:id="@+id/textView44"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/imageMenu"
            android:layout_marginLeft="7dp"
            android:textColor="#ffffff"
            android:textSize="16.8dp"
            android:textStyle="bold"
            android:layout_marginTop="11dp"
            style="@style/CustomTextViewStyle"/>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:orientation="vertical"
        android:layout_above="@+id/linearLayout16"
        android:layout_centerHorizontal="true">

        <com.skip.client.customer.utils.MyTextView2D
            android:id="@+id/textView17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="4"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ffffff"
            android:textSize="24dp"
            android:textStyle="bold"
            android:layout_alignBottom="@+id/progressBar"
            android:layout_centerHorizontal="true"
            android:text="Almost there!"
            style="@style/CustomTextViewStyle"/>

        <RelativeLayout
            android:layout_width="116dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            >

            <com.pkmmte.view.CircularImageView
                android:id="@+id/receiptImageView"
                android:layout_width="116dp"
                android:layout_height="116dp"
                android:layout_alignParentBottom="false"
                android:layout_alignParentEnd="false"
                android:layout_alignParentLeft="false"
                android:layout_alignParentRight="false"
                android:layout_alignParentStart="false"
                android:layout_alignParentTop="false"
                android:layout_alignWithParentIfMissing="false"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="25dp"
                android:src="@drawable/profile_photo"
                app:border="true"
                app:border_width="4dp"
                app:border_color="#ffffff"/>

            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:indeterminate="false"
                android:visibility="gone"/>

            <ImageView
                android:layout_width="33dp"
                android:layout_height="33dp"
                android:id="@+id/imageView2"
                android:background="@drawable/avatar_plus"
                android:layout_alignTop="@+id/progressBar"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="32dp"/>

        </RelativeLayout>
    </LinearLayout>
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_below="@+id/linearLayout16"
        android:layout_alignParentStart="true">

    </ScrollView>

    <LinearLayout
        android:id="@+id/applyButton"
        android:layout_width="match_parent"
        android:layout_height="67dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_alignParentStart="false"
        android:layout_alignParentTop="false"
        android:background="#007bb6"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical">

        <com.skip.client.customer.utils.MyTextView2D
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Continue"
            android:textColor="#ffffff"
            android:textSize="17.76dp"
            android:textStyle="bold"
            style="@style/CustomTextViewStyle"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@+id/applyButton"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout16">




        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:orientation="vertical">

            <EditText
                android:id="@+id/editEmail"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:autoText="false"
                android:background="#00000000"
                android:ems="10"
                android:hint="Your email"
                android:inputType="textEmailAddress"
                android:nextFocusDown="@+id/editPassword"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:singleLine="false"
                android:textColor="#ffffff"
                android:textColorHint="#9377ab"
                android:textSize="19sp"
                android:textStyle="bold"
                android:gravity="center_vertical|center_horizontal"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="16dp"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingBottom="8.50dp">

                <EditText
                    android:id="@+id/editFirstName"
                    android:layout_width="0dp"
                    android:layout_height="50dp"
                    android:layout_weight="1.09"
                    android:background="#00000000"
                    android:ems="10"
                    android:hint="Your first name"
                    android:inputType="text"
                    android:maxLength="254"
                    android:singleLine="true"
                    android:nextFocusDown="@+id/editLastName"
                    android:textColor="#ffffff"
                    android:textColorHint="#9377ab"
                    android:textSize="19sp"
                    android:textStyle="bold"
                    android:gravity="center_vertical|center_horizontal"
                    android:layout_marginRight="3dp"/>

                <View
                    android:layout_width="1dp"
                    android:layout_height="20dp"
                    android:background="#ffffff" />

                <EditText
                    android:id="@+id/editLastName"
                    android:layout_width="0dp"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:ems="10"
                    android:hint="Your last name"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="254"
                    android:singleLine="true"
                    android:nextFocusDown="@+id/editPhone"
                    android:textColor="#ffffff"
                    android:textColorHint="#9377ab"
                    android:textSize="19sp"
                    android:textStyle="bold"
                    android:gravity="center_vertical|center_horizontal"
                    android:layout_marginRight="3dp"/>
            </LinearLayout>

            <EditText
                android:id="@+id/editPhone"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#00000000"
                android:ems="10"
                android:hint="Your Phone number"
                android:inputType="text|phone"
                android:nextFocusDown="@+id/editFirstName"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:textColor="#ffffff"
                android:textColorHint="#9377ab"
                android:textSize="19sp"
                android:textStyle="bold"
                android:gravity="center_vertical|center_horizontal"
                android:layout_marginTop="12dp"/>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:layout_marginTop="2dp">

                <LinearLayout
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/line_white"
                    android:orientation="horizontal"
                    android:paddingBottom="17.25dp"
                    android:paddingTop="19.75dp"
                    android:layout_marginLeft="3dp">

                    <com.skip.client.customer.customviews.SkipTextView
                        android:id="@+id/tvDob"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="2dp"
                        android:background="#00000000"
                        android:ems="10"
                        android:hint="Age"
                        android:inputType="number"
                        android:paddingLeft="2dp"
                        android:textColor="#ffffff"
                        android:textColorHint="#9377ab"
                        android:textSize="19sp"
                        android:textStyle="bold"
                        android:gravity="center_horizontal"/>
                </LinearLayout>


                <RadioGroup
                    android:id="@+id/radioSex"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="14.50dp"
                    android:layout_marginTop="1.75dp"
                    android:gravity="right"
                    android:orientation="horizontal"
                    android:layout_gravity="bottom">

                    <RadioButton
                        android:id="@+id/radioMale"
                        android:layout_width="93dp"
                        android:layout_height="41dp"
                        android:background="@drawable/radio_button_background_reg"
                        android:button="@android:color/transparent"
                        android:checked="true"
                        android:gravity="center_vertical|center_horizontal"
                        android:text="Male"
                        android:textColor="#ffffff"
                        android:textSize="19sp"
                        android:textStyle="bold"
                        android:textColorHint="#9377ab"
                        android:layout_marginLeft="5dp"
                        android:autoText="false"/>

                    <RadioButton
                        android:id="@+id/radioFemale"
                        android:layout_width="93dp"
                        android:layout_height="41dp"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/radio_button_background_reg"
                        android:button="@android:color/transparent"
                        android:checked="false"
                        android:gravity="center_vertical|center_horizontal"
                        android:singleLine="true"
                        android:text="Female"
                        android:textColor="#ffffff"
                        android:textSize="19sp"
                        android:textStyle="bold"
                        android:textColorHint="#9377ab"
                        android:autoText="false"/>
                </RadioGroup>

            </LinearLayout>


            <EditText
                android:id="@+id/do_not_remove"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="50dip"
                android:layout_marginLeft="15dip"
                android:layout_marginRight="15dip"
                android:layout_marginTop="5dp"
                android:background="@drawable/line_white"
                android:ems="10"
                android:inputType="textPersonName"
                android:paddingBottom="19.50dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff"
                android:textStyle="bold" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

in the manifes i write:

<activity
            android:name=".activities.StartActivity"
            android:theme="@style/Theme.TransparencyDemo"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="portrait"/>

I want to scroll scrollView and raised EditText above the keyboard

more than just an example

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

<RelativeLayout 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"
              android:orientation="vertical"
              android:weightSum="1"
              android:background="#376e0a">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="62dp"
        android:background="#420603"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

Upvotes: 0

Views: 2746

Answers (2)

Abhishek
Abhishek

Reputation: 2370

similar ques.

Adjust layout when soft keyboard is on

Here's a solution that works like the Evernote login screen:

First, define a class that will be your special LinearLayout like this:

public class MyLayout extends LinearLayout {

public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyLayout(Context context) {
super(context);
}

private OnSoftKeyboardListener onSoftKeyboardListener;

@Override
protected void onMeasure(final int widthMeasureSpec, final int    heightMeasureSpec) {
if (onSoftKeyboardListener != null) {
    final int newSpec = MeasureSpec.getSize(heightMeasureSpec); 
    final int oldSpec = getMeasuredHeight();
    if (oldSpec > newSpec){
        onSoftKeyboardListener.onShown();
    } else {
        onSoftKeyboardListener.onHidden();
    }
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public final void setOnSoftKeyboardListener(final OnSoftKeyboardListener listener) {
this.onSoftKeyboardListener = listener;
}

public interface OnSoftKeyboardListener {
public void onShown();
public void onHidden();
}

}

This layout listens to measure changes, and if new measurements are < than the old ones, that means part of the screen is eaten by soft keyboard.

Though, for it to work, in your manifest you need to set android:windowSoftInputMode="adjustResize" so the content will be resized and not just shifted.

And the whole system works as follows: You have your layout:

<MyLayout id="layout">
<SomeImage id="image"/>
<SomeText>
<SomeInput>
</MyLayout>

It's like evernotes login screen. Then, in your activity:

((MyLayout)findViewById(R.id.layout)).setOnSoftKeyboardListener(new OnSoftKeyboardListener() {
    @Override
    public void onShown() {
        findViewById(R.id.image).setVisibility(View.GONE);
    }
    @Override
    public void onHidden() {
        findViewById(R.id.image).setVisibility(View.VISIBLE);
    }
    });

Then go to manifest.xml and set

android:windowSoftInputMode="adjustResize"

What will happen, is when soft keyboard is shown, it'll hide the image and will resize the rest of content. (You can actually see how text is resized in Evernote)

Image hide is, of course, one of the many things you can do. But you must be careful, since different layout changes will also call onMeasure.

Of course it's a dirty variant. You need to check for orientation changes, and the right time when actually take the measurements, and maybe some more logic when comparing the new specs with the old ones. But i think this is the only way to do it.

Upvotes: 0

Darshil Shah
Darshil Shah

Reputation: 361

add following

android:windowSoftInputMode="stateVisible|adjustResize"

in you Manifest file.

Upvotes: 1

Related Questions