6

In my application, the device soft-keyboard is covering the EditTextViews, so user cannot see what he is typing.

So i want to hide other Views(like a button in my case), to make the EditTextViews visible.

But instead on doing something like

EditText.onFocus(){
//hide button
}

for every EditText, i want to do something like

if(soft-keyboard.isOpenned())
//hide button
}

how to track if soft-keyboard is openned or closed?

Edit:

Actually my layout is this:

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:focusable="true"
        android:focusableInTouchMode="true" >
    </LinearLayout>

    <ImageView
        android:id="@+id/Logo"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/wic_logo_small" />

    <Button
        android:id="@+id/goButton_iWant"
        android:layout_width="35dp"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:background="@color/black"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/go" />

    <AutoCompleteTextView
        android:id="@+id/searchAutoCompleteTextView_iWant"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/goButton_iWant"
        android:layout_toRightOf="@id/Logo"
        android:hint="@string/search"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/iWantLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/iWantPageLogo"
        android:background="@color/grey"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/iWant"
        android:textColor="@color/white" />

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollViewiWant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iWantLabel"
        android:scrollbars="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/iNeedToBuy"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/iWantLabel"
                android:gravity="center_vertical|center_horizontal"
                android:padding="5dp"
                android:text="@string/iNeedToBuy"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <AutoCompleteTextView
                android:id="@+id/iNeedToBuyEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/iNeedToBuy"
                android:hint="@string/product"
                android:imeOptions="actionNext"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/error1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/iNeedToBuyEditText"
                android:height="0dp"
                android:text=""
                android:textColor="@color/error_color"
                android:textSize="12dp" />

            <TextView
                android:id="@+id/priceWillingToPay"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/error1"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/priceWillingToPay"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/priceWillingToPayEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/priceWillingToPay"
                android:hint="@string/price"
                android:inputType="number"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/error2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/priceWillingToPayEditText"
                android:height="0dp"
                android:text=""
                android:textColor="@color/error_color"
                android:textSize="12dp" />

            <TextView
                android:id="@+id/needToBuyItBy"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/error2"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/needToBuyItBy"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/date_iWant"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/needToBuyItBy"
                android:clickable="true"
                android:cursorVisible="false"
                android:focusable="false"
                android:hint="@string/date"
                android:inputType="none"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/error3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/date_iWant"
                android:height="0dp"
                android:text=""
                android:textColor="@color/error_color"
                android:textSize="12dp" />

            <DatePicker
                android:id="@+id/datePicker_iWant"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_below="@id/error3"
                android:hint="@string/date"
                android:padding="5dp"
                android:textColor="@color/black"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/iAmIn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/date_iWant"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/iAmIn"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <Spinner
                android:id="@+id/city_spinner_iWant"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iAmIn"
                android:prompt="@string/citySpinner"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <Button
                android:id="@+id/submitButton_iWant"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/city_spinner_iWant"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/submit"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/emptySpace"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_below="@id/submitButton_iWant"
                android:gravity="left" />
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/buttons_iWant"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/feedButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/feed"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/iWantButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/iwant"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/shareButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/share"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/profileButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/profile"
            android:textColor="@color/black" />
    </LinearLayout>

</RelativeLayout>

The problem comes when i am typing text in the EditTextViews in the ScrollView

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

3 Answers3

5

There is no direct way to find it, but i come up with a dirty solution and it work fine. The idea behind is to compare the height of two views your activity and keyboard.

boolean watcherState;
private void isShowing() {
    final RelativeLayout relative = (RelativeLayout) findViewById(R.id.parent);
    relative.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    // r will be populated with the coordinates of your view
                    // that area still visible.
                    relative.getWindowVisibleDisplayFrame(r);

                    int heightDiff = relative.getRootView().getHeight()
                            - (r.bottom - r.top);
                    Log.d("Height", heightDiff + "//");
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        watcherState = true;
                    } else
                        watcherState = false;
                }
            });

}
PiyushMishra
  • 5,743
  • 6
  • 37
  • 57
  • this works fine but since its a listener on global layout, it gets executed on every keypress. So .. please suggest something else.. – Purush Pawar Jun 05 '13 at 13:49
  • @Puru this method is to detect the keyboard situation whenever it appear and you can compare the things with complete screen of the device hence global layout. – PiyushMishra Jun 08 '13 at 09:20
  • @PiyushMishra Yes.That is correct but if you execute this code snippet you'll notice that for every keypress, theres a creation of new instance of 'Rect r' and 'int heightDiff'. So, if anyone declares any more diff kind of vars, its really not going to be memory efficient.. – Purush Pawar Jun 10 '13 at 06:40
  • @Puru create global one and rewrite on it, not a big deal. – PiyushMishra Jun 10 '13 at 08:02
  • For me it executes like crazy even when I don't touch the screen at all – Denys Kniazhev-Support Ukraine Sep 25 '14 at 16:17
0

You should probably use android:windowSoftInputMode attribute in the manifest file to describe how the screen layout should arrange when soft keyboard shows up (http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft)

  • android:windowSoftInputMode="adjustResize":

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

  • android:windowSoftInputMode="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.

sdabet
  • 18,360
  • 11
  • 89
  • 158
0

For handling softkepad detection, set the android:ConfigChanges attribute for your activity in the AndroidManifest.xml file:

<activity android:name="com.myapp.MyActivity"
          android:label="@string/app_name"
          android:configChanges="keyboard|keyboardHidden|orientation"
          >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Here, the keyboard|keyboardHidden flag in android:configChanges tells the system that the Activity wishes to invoke the onConfigurationChanged() method of your Activity instead, which allow you to handle the softkebaord changes.

Here’s an example onConfigurationChanged() which replaces the main content view for the currently running Activity:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Set a new layout - framework will automatically choose portrait or landscape as needed
    setContentView( R.layout.my_activity_layout );

    // Reconnect all listeners for controls in the layout
    a.findViewById( R.id.exit_button ).setOnClickListener( mListener );
}

Odds are, your onConfigurationChanged() method will need to do almost exactly the same kind of work as your Activity’s onCreate() method, so it’s probably a good idea to DRY out your code by factoring out your layout setup into a separate helper method that you invoke from both onCreate() and onConfigurationChanged().

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
  • but then, this method will be called even when i change the orientation, right? – Archie.bpgc Sep 11 '12 at 12:19
  • if you omit the orientation part it ll not, try with android:configChanges="keyboard|keyboardHidden" – Arun Padmanabhan Sep 11 '12 at 20:38
  • But i am already using android:configChanges="orientation" in my application. Actually in Activities i am not overriding onConfigurationChnaged(); But my intention is not to do anything when orientation is changed. But now that i will add 2 more parameters, how can track specific Configuration. Like if its orientation- do nothing, if keyboard- hide buttons. – Archie.bpgc Sep 12 '12 at 04:43
  • On adding android:configChanges="orientation" and not overriding onConfigurationChnaged() will do nothing, ie. no Activity restart. Similarly On adding android:configChanges="keyboard|keyboardHidden" There ll be no Activity restart, just the onConfigurationChnaged() ll be invoked. So for now try overriding onConfigurationChnaged() to set a layout when soft-keypad is triggered. I ll try out and check if we can specifically find what triggered it. – Arun Padmanabhan Sep 12 '12 at 05:28
  • 1
    But i Found out that KeyBoard events won't trigger onConfigurationChnaged. Its explained here http://stackoverflow.com/questions/6279771/onconfigurationchanged-not-firing-for-keyboardhidden#answers-header – Archie.bpgc Sep 12 '12 at 05:42
  • answer given by PiyushMishra is simple and works fine. Thanks for your effort – Archie.bpgc Sep 12 '12 at 06:00