1

I would like the keyboard to open when my Activity starts. I've tried all the methods/answers with the questions below with no luck.

I believe the issue is when the hardware keyboard is available, the default behavior is for the soft keyboard to not be displayed. Can this be overridden? What happens if the hardware keyboard is hidden?

I've read the following questions with no luck. The closest to the problem I'm experiencing is here: Question 2712822

Others Include:
Question 3379403
Question 2479504

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
         <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
            <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/testText" android:focusable="true" android:focusableInTouchMode="true" android:hint="Input here"></EditText>
        </LinearLayout>
        <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
            <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="and here"></EditText>
        </LinearLayout>  
    </LinearLayout>
 </ScrollView>

My Main Activity code looks like this:

package com.example.example3;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class example3 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText edit = (EditText)this.findViewById(R.id.testText);
    edit.requestFocus();

    /*       // Below Doesn't work
    InputMethodManager imm = (InputMethodManager)
    example3.this.getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm != null){/          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    }

    //Below Doesn't work
   // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    */    
    }
}

Is this a lost cause? Can someone test this on a phone with hardware keyboard that is closed and tell me what happens?

Community
  • 1
  • 1
hydrox467
  • 1,705
  • 1
  • 12
  • 15

3 Answers3

0

This definitley works, i use it all the time, dont call it from onCreate, call it from onStart or onResume.

public static void showKeyboard(Activity act,EditText t){
            InputMethodManager imm = (InputMethodManager)act.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(t, 0);
}
siliconeagle
  • 7,379
  • 3
  • 29
  • 41
  • In addition to my comment above, in your testing of this method, do you specifically have to setup the emulator with touch enabled, and hardware keyboard disabled for this to work? I've tried it on my droid inc, and was successful, but not with the emulator. – hydrox467 May 30 '11 at 03:42
0

The softinputMode related answers should be working. If you're having no luck with setting it in the manifest you might try setting it in onCreate() to dictate how the keyboard is shown when navigating to the activity.

To show it anytime the activity gets focus use:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
jqpubliq
  • 11,874
  • 2
  • 34
  • 26
  • I've tried this in my code... it's commented out currently. It wasn't successful. Do you know if the emulator / real device currently has a hardware keyboard open, will that override any attempt to open the keyboard via programming? – hydrox467 May 30 '11 at 03:41
  • The software keyboard shouldn't open when there is a hardware keyboard exposed – jqpubliq Jun 01 '11 at 17:59
0

Try this

 edtReceiver.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus)
                    {

                //  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        if(imm != null)
                        {
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
                        }
                    }

                }
            });

This works for me.

Vibhuti
  • 702
  • 1
  • 7
  • 21