• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SoftKeyboard without any EditText causes strange behavior

I had an activity that opens already with the keyboard opened because I use the attribute android:windowSoftInputMode="stateAlwaysVisible" at AndroidManifest. But in this activity I don't have any Edit Text (only one button) and I need to read each character typed (once per time) by user, but I really just need one character and my app will auto complete the word. So I had overridden the dispatchKeyEvent to read each character.

The problem is that since the keyboard is being showed and there is no one Edit Text, when click in any character Android OS kind selects the button (or any other view) on screen. This selection is kind I was using a D-pad. And if the back button is pressed it will "select" any other view from back activity.

I think that since there is no Edit Text to works with keyboard the activity does not know how to handle the characters typed.

I had attached the project on thread and at Tiny server a simple project with two activities that can be used as sample to reproduce the issue: http://s000.tinyupload.com/download.php?file_id=70317553185010262971&t=7031755318501026297148955

Also had attached a screenshot at TinyPic:

Below also are all my codes:

AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"package="com.example.testbug"android:versionCode="1"android:versionName="1.0">

<uses-sdkandroid:minSdkVersion="14"android:targetSdkVersion="19"/>

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="[USER=696546]@String[/USER]/app_name"android:theme="[USER=19691]@Style[/USER]/AppTheme"><activityandroid:name="com.example.testbug.MainActivity"android:label="[USER=696546]@String[/USER]/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity>

<activityandroid:name="com.example.testbug.TestActivity"android:windowSoftInputMode="stateAlwaysVisible"/></application>

</manifest>

MainActivity.java (1º activity)

Code:
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;

publicclassMainActivityextendsActivity{

Button btnStart;

@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnStart =(Button) findViewById(R.id.btnStart);

btnStart.setOnClickListener(newOnClickListener(){@Overridepublicvoid onClick(View v){Intent i =newIntent(getApplicationContext(),TestActivity.class);
startActivity(i);}});}}

TestActivity.java (2º activity)

Code:
import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.inputmethod.InputMethodManager;import android.widget.Button;import android.widget.TextView;

publicclassTestActivityextendsActivity{

TextView tvType;Button testButton;int count =0;

@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count);

tvType =(TextView) findViewById(R.id.tv_type);
testButton =(Button) findViewById(R.id.btnTest);}

@Overridepublicboolean dispatchKeyEvent(KeyEventevent){

count++;if(count==6){
hideKeyboard();}

returnsuper.dispatchKeyEvent(event);}

privatevoid hideKeyboard(){InputMethodManager inputManager =(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);}}

Layouts:

activity_main.xml
Code:
<LinearLayoutxmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"android:layout_width="fill_parent"android:layout_height="fill_parent"android:eek:rientation="vertical"android:gravity="center">

<Buttonandroid:id="@+id/btnStart"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Start"/>

</LinearLayout>
activity_count.xml
Code:
<LinearLayoutxmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"android:layout_width="fill_parent"android:layout_height="fill_parent"android:eek:rientation="vertical"android:gravity="center_horizontal">

<Buttonandroid:id="@+id/btnTest"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TestButton"/>

<TextViewandroid:id="@+id/tv_type"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceMedium"android:text="Press any key 3 times to Hide keyboard\n Then Press Back button. The same behavior of selection of button will happens on back activity"/>

</LinearLayout>
 

Attachments

  • Test Bug Project2.zip
    1.5 MB · Views: 28
Last edited:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones