tags:

views:

97

answers:

4

In an Android app, whenever the activity launches, the textbox gets the focus and the soft keyboard pops up automatically. I have tried to stop this by using following line in onCreate method, but it does not work.

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(EditText.getWindowToken(), 0);
+1  A: 

Does the following work?

// Find editor
EditText editWindowInstance = this.findViewById(R.id.MyEditWindow);

// close soft keyboard 
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editWindowInstance.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
Thorsten Dittmar
No, it does not work.
How about using "hideSoftInputFromWindow(editWindowInstance, ...)" instead of "editWindowInstance.getWindowToken()"?
Thorsten Dittmar
+2  A: 

You can use the following line of code to make sure the keyboard only pops up when a user clicks into an EditText

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

And

You need to add

android:windowSoftInputMode="adjustResize"

to your activity tag in the AndroidManifest.xml file.

Donal Rafferty
This works, but when I click the text box for typing, the keyboard pops up over the text box an I can't see what I am typing. The text box is at the bottom of the screen.
You need to add android:windowSoftInputMode="adjustResize" to your tag in the AndroidManifest.xml file.
Donal Rafferty
android:windowSoftInputMode="adjustResize" is set in AndroidManifest.xml but still same problem.There is a button in same screen too. I tried to move focus to the button by using:btn.setFocusable(true);btn.requestFocus();But still the focus is on TextView. I don't know what I am doing wrong.
Strange, can you post your code? The activity tag from the manifest and the code from your UI Activity?
Donal Rafferty
I am going to post the code as an answer of this question, as code formatting is not available in comments.
Edit your Question
Donal Rafferty
Have you looked at the code?
A: 

The following code works for me

((InputMethodManager) iClockActivity
                    .getSystemService(Context.INPUT_METHOD_SERVICE))
                    .showSoftInput(textView, 0);
Amith GC
I tried this, but still the same problem.
A: 

From Manifest file:

 <activity android:name=".ChatActivity"
                    android:label="@string/app_name"
                    android:configChanges="orientation"
                    android:windowSoftInputMode="adjustResize"               
                    android:launchMode="singleTop"></activity>

UI Activity File:

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/AbsoluteLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    
    android:background="#ECEFF5"
    >

    <include
        layout="@layout/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"         
    />  

    <include 
        android:id="@+id/top_bar" 
        layout="@layout/im_top_bar"
        android:layout_width="fill_parent"                      
    />  

    <ListView android:id="@+id/listConv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"              
              android:scrollbars="horizontal"
              android:layout_below="@id/top_bar"
              android:divider="#00000000"
              android:cacheColorHint="@android:color/transparent"              
              android:layout_marginBottom="95px"              
            />

    <RelativeLayout     
        android:id="@+id/sendMessageLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:background="@drawable/chat_box_bg"  
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="35px"
        android:gravity="center_vertical" 
    >

    <EditText android:id="@+id/sendText"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:singleLine="true"
              android:textSize="16sp"
              android:autoText="false"
              android:capitalize="none"
              android:scrollHorizontally="true"
              android:layout_marginRight="65px"                            
              />

    <Button android:id="@+id/sendBtn"
            android:layout_width="60px"
            android:layout_height="45px"            
            android:text="Send"
            android:layout_alignParentRight="true"            

          />

    </RelativeLayout>

    <include layout="@layout/footer_nav" />

</RelativeLayout>

Java file:

package com.web;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class ChatActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.chat);

        super.onCreate(savedInstanceState);
    }

    }
I meant your activities Java code :)
Donal Rafferty
I haven't done much in Activity file. The code has been pasted above.
Any idea how to fix it?