Samantha Withanage
Samantha Withanage

Reputation: 3851

onKey listner works properly on emulator, but not working on android device

I'm working on a android project in which i need to change a table layout according to the changes on a edit text view. The code work properly on emulator. But it doesn't works on actual device. Also in this case I'm using fragments. My onKey listener is as below.

EditProduct.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            Editable prx=EditProduct.getText();

            String new_prx=prx.toString();

            //mini_productList
            int count=0;
            if (new_prx.equals("")) {

                loadtableProducts(productList);

            }else {

                for(int i=0;i<productList.size();i++){

                    if (productList.get(i).getDescription().toString().substring(0, (new_prx.length())).equalsIgnoreCase(new_prx)) {

                        mini_productList.add(productList.get(i));
                        count++;

                    }

                }

                loadtableProducts(mini_productList);

                //Toast.makeText(getActivity(), "No of products "+count, Toast.LENGTH_SHORT).show();
            }


            return false;
        }
    });

Also the edit text view in my layout is as below

 <EditText
                android:id="@+id/edit_product"
                android:layout_width="@dimen/txtWidth"
                android:layout_height="@dimen/txtHeight"
                android:background="@color/transparent"
                android:hint="Enter the product name"
                android:singleLine="true"
                android:textColor="@color/black"
                android:textSize="@dimen/boldtext" />

Also i added a Toast to make sure its work on the device and it shown on the entering of a key. So can someone help me in this matter. Thank you!

Upvotes: 1

Views: 232

Answers (1)

NigelK
NigelK

Reputation: 8490

The documentation for EditText.setOnKeyListener states:

"Register a callback to be invoked when a hardware key is pressed in this view. Key presses in software input methods will generally not trigger the methods of this listener."

The emulator is using a hardware keyboard whereas your device is using a software keyboard. I suggest you use an "addTextChangedListener" as an alternative.

However I note that you say that your Toast does indicate the entering of a key. So are you actually entering your OnKeyListener when using the actual device? If so, I will delete my answer.

Upvotes: 2

Related Questions