Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText in Android doesn't show text when typing while using the on-screen keyboard

I have an activity consisting of 3 EditText fields (amongst other things like TextViews and a few buttons). I also have an AutoCompleteTextView with a list of String's using an ArrayAdapter.

Whenever I test the app in an emulator, I can type when the keyboard is up but it doesn't show the text (it still gives predictions) and the text only appears in the EditText box once the keyboard is closed down. This happens when I test it on my phone, too. However, it works and shows up as you type on the emulator if the on-screen keyboard isn't up and you're just typing normally.

I have no idea why!

Here is my Activity XML (where the EditText's are the top 3 blocks)

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/l"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_x="3dp"
    android:layout_y="5dp"
    android:background="@drawable/gymbg" >


   <AutoCompleteTextView android:id="@+id/inputExercise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
        android:inputType="text"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/timeSet"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:width="200dp" />  

    <EditText
        android:id="@+id/inputWeight"
        android:inputType="number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/inputExercise"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:width="200dp" >
    </EditText>

    <EditText
        android:id="@+id/inputReps"
        android:inputType="number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/inputWeight"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:width="200dp" >
    </EditText>

    <TextView
        android:id="@+id/timeMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/timeMain"
        android:textSize="32sp" 
        android:textColor="#0F293B"/>

    <TextView
        android:id="@+id/timeSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timeMain"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="@string/timeSet" 
        android:layout_marginBottom="50dp"
        android:textColor="#0F293B"/>

    <TextView
        android:id="@+id/labExercise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/addbutton"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/timeSet"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:layout_marginLeft="15dp"
        android:text="@string/labExercise"   
        android:layout_toLeftOf="@+id/inputExercise"
        android:textColor="#ffffff"/>

    <Button
        android:id="@+id/addbutton"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:text="@string/add" />


    <Button
    android:id="@+id/startStop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_above="@+id/addbutton" 
    android:layout_alignParentLeft="true"
    android:layout_margin="10dp"
    android:text="@string/startStop" />

    <TextView
        android:id="@+id/labWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/inputExercise"
        android:layout_margin="10dp"
        android:text="@string/labWeight" 
        android:textColor="#ffffff"/>

    <TextView
        android:id="@+id/labReps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/inputReps"
        android:layout_margin="10dp"
        android:text="@string/labReps" 
        android:textColor="#ffffff"/>

    <TextView
        android:id="@+id/seePrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/inputExercise"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"
        android:text="@string/tapToViewPrevious" 
        android:textColor="#505050"/>

</RelativeLayout>

and here is the code I used in my activity: (I have stripped out unneccesary code)

public class MyWorkoutDiary1Activity extends Activity implements OnClickListener, TextWatcher 
{

    TextView seePrevious;
    DatabaseHandler db;

    AutoCompleteTextView myAutoComplete;

    ArrayList<String> exerciseType = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        exercise = new EditText(this); 
        exercise = (EditText)findViewById(R.id.inputExercise); 

        db = new DatabaseHandler(this);
        exerciseType = db.getUniqueExercises();

        myAutoComplete = (AutoCompleteTextView)findViewById(R.id.inputExercise);

        myAutoComplete.addTextChangedListener(this);
        myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, exerciseType));


        weight = new EditText(this); 
        weight = (EditText)findViewById(R.id.inputWeight); 


        reps = new EditText(this); 
        reps = (EditText)findViewById(R.id.inputReps); 

    }

}

Thanks for reading

like image 676
Smittey Avatar asked Feb 11 '14 19:02

Smittey


People also ask

How do I get the keyboard to show in EditText?

edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears. This open the keyboard at Activity creation.

How to show soft keyboard Android?

By default, the soft keyboard may not appear on the emulator. If you want to test with the soft keyboard, be sure to open up the Android Virtual Device Manager ( Tools => Android => AVD Manager ) and uncheck "Enable Keyboard Input" for your emulator.

How to auto hide keyboard in Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

What is Android soft keyboard?

The Android system shows an on-screen keyboard — known as a soft input method — when a text field in your UI receives focus. The keyboard takes about half the screen; in other words, you only have half of the screen to display any information.


2 Answers

It works for me may be helpful to others as well, open your manifest file and set hardwareAccelerated="true".

 <application
    android:allowBackup="true"
    .....
    android:hardwareAccelerated="true">

For more about HardwareAccelerated https://developer.android.com/guide/topics/graphics/hardware-accel.html

like image 193
Naeem Ibrahim Avatar answered Nov 15 '22 07:11

Naeem Ibrahim


In fact your text is being typped, but that's a little bug that makes your text color be the same as your background, so you don't see it. This can be easily fixed by doing 2 things:

1) Simply change the textColor of your EditText, either defining it in the layout:

android:textColor="..."

or dynamically:

EditText et = (EditText) findViewById(R.id.your_edittext);
et.setTextColor(Color.RED);

2) Change the extended theme in your manifest:

<application android:theme="@style/Theme.Light.NoTitleBar.Workaround" ... >

3) Create the new theme at res/values/themes.xml which uses fixed styles:

<style name="Theme.Light.NoTitleBar.Workaround" parent="@android:style/Theme.Light.NoTitleBar">
  <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewLight</item>
  <item name="android:dropDownItemStyle">@style/Widget.DropDownItemLight</item>
</style>

4) Now these styles, located at res/values/styles.xml should fix the color:

<style name="AutoCompleteTextViewLight" parent="@android:style/Widget.AutoCompleteTextView">
  <item name="android:textColor">@android:color/primary_text_light</item>
</style>
<style name="Widget.DropDownItemLight" parent="@android:style/Widget.DropDownItem">
  <item name="android:textColor">@android:color/primary_text_light</item>
</style>

I know it's a mess, but try it and if it works, try finding a combination of those attributes that fit to your layout.

like image 32
nKn Avatar answered Nov 15 '22 07:11

nKn