2

I have the following the layout file

<?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/com.company" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC"
        android:orientation="horizontal" 
        android:id="@+id/main">

        <com.company.DrawView
            android:id="@+id/draw_view" 
            android:layout_width="900dp"
            android:layout_height="500dp"
            android:layout_alignParentLeft="true"
            android:background="#CCCC00" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/draw_view"
            android:orientation="horizontal">
        <Button
            android:id="@+id/btn1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="10dp"
            android:clickable="true"
            android:onClick="buttonClicked"
            android:text="button" />

        <EditText 
            android:id="@+id/text1"
            android:textSize="5dp"
            android:inputType="text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:maxWidth="5dp"
            android:visibility="invisible"
            android:gravity="bottom"
            />
        </LinearLayout>
    </RelativeLayout>

Whenever I set the focus on the EditText, it is expanding and filling up the top half of the screen and the soft keyboard is filling up the bottom half of the screen. What should I do retain the size of the EditText and show the soft keyboard?

The intent is to capture the key strokes (on the soft keyboard) of the user and use it on another part of the application.

Edit 1: I'm using sdk-version 15 and I am on macbook if that helps. Adding the manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ChalkrText"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustNothing" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Srisa
  • 967
  • 1
  • 7
  • 17

3 Answers3

0

//in your manifiest.xml write the below code in your activity

android:windowSoftInputMode="adjustNothing" 
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • try this http://stackoverflow.com/questions/7940765/how-to-hide-the-soft-keyboard-from-inside-a-fragment – Padma Kumar Dec 28 '11 at 11:17
  • Not sure how that helps. I want the keyboard to be visible but the EditText should not expand. – Srisa Dec 28 '11 at 11:19
  • this "adjustNothing" will work if your are not using scrollview. can you share your manifest code for this activity where you had written this android:windowSoftInputMode. – Padma Kumar Dec 28 '11 at 11:28
  • Ru doing it for Tablet. why you hardcoded this com.company.DrawView android:layout_width="900dp", android:layout_height="500dp". I am confused about this DrawView!!?. – Padma Kumar Dec 28 '11 at 11:45
  • `adjustNothing` is undocumented and therefore may not exist for all past and future versions of Android. – CommonsWare Apr 24 '12 at 14:28
0

This is standart behavior for EditText in landscape orientation of device. You can't change it.

Jin35
  • 8,602
  • 3
  • 32
  • 52
  • 1
    Any reference? More importantly, is there any other way to accomplish what I want to achieve? – Srisa Dec 28 '11 at 13:28
0

I found a work around by showing the soft keyboard on clicking a button and overriding the dispatchKeyEvent in the Activity to capture the key strokes. I didn't need the EditText and it wasn't required to show the soft keyboard.

This is to show the soft keyboard:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

This is the dispatchKeyEvent:

public boolean dispatchKeyEvent(KeyEvent event) {
/**
 * ACTION_MULTIPLE is for keys with special characters like &copy;
 * British Pound sign etc. These keys do not generate the ACTION_DOWN
 * or ACTION_UP events.
 * 
 * Only the ACTION_DOWN is handled. ACTION_UP is ignored to avoid
 * duplication of the letters that the user wants to type.
 */
if (event.getAction() == KeyEvent.ACTION_DOWN ||
    event.getAction() == KeyEvent.ACTION_MULTIPLE) {

    DrawView dview = (DrawView) findViewById(R.id.draw_view);
        Log.v(LOG_TAG, "c = " + event.getKeyCode() + 
                    ", l = " + event.getDisplayLabel() + 
                    ", u = " + event.getUnicodeChar() +
                    ", uc = " + (char)event.getUnicodeChar() + 
                    ", chars = " + event.getCharacters()
            );
        dview.addText(event);
}
return true;
}

This is the addText method where the concerned event is handled:

public void addText(KeyEvent event) {
    int unicode = event.getUnicodeChar();
    int keycode = event.getKeyCode();
    String characters = event.getCharacters();

    if (unicode == 0) {
        switch (keycode) {
        case 0:
            if (characters != null) {
                text += characters;
            }
            break;
        case KeyEvent.KEYCODE_DEL:
            text = text.substring(0, text.length()-1);
            break;
        case KeyEvent.KEYCODE_SHIFT_LEFT:
        case KeyEvent.KEYCODE_SHIFT_RIGHT:
            // ignore, nothing to do
            break;
        }
    }
    else {
        text = text + (char)unicode;
    }

    invalidate();
}

These are the posts that set me in the right direction: How to Capture soft keyboard input in a View? Can I use the soft keyboard without an EditText?

Community
  • 1
  • 1
Srisa
  • 967
  • 1
  • 7
  • 17