Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - AutoCompleteTextView onClickListener works after second click

Tags:

android

I have four AutoCompleteTextViews (ACTV) and they're working just fine. I created an onClickListener() for them to know when a user selects/presses any of the four at any point in time. However, upon close inspection, I noticed that I had to click an ACTV twice before it was triggered.

Here is my code:

In my onCreate(), I initialize the actv and set the onClickListener():

brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand);
itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName);
partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart);
barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode);
    
brandACTV.setOnClickListener(actvClicked);
itemACTV.setOnClickListener(actvClicked);
partACTV.setOnClickListener(actvClicked);
barcodeACTV.setOnClickListener(actvClicked);

and the onClickListener() is defined like so:

OnClickListener actvClicked = new OnClickListener(){
    
    @Override
    public void onClick(View view){
        
        Log.d("Hi", "onClickActivated");

        switch (view.getId()) {
            case R.id.actvBrand:
                Log.d("Hi", "Brand pressed");
                break;
            
            case R.id.actvItemName:
                Log.d("Hi", "Item name pressed");
                break;
            
            case R.id.actvPart:
                Log.d("Hi", "Part pressed");
                break;
                
            case R.id.actvBarcode:
                Log.d("Hi", "Barcode pressed");
                break;
        }
    }
};

However, only the initial click is detected properly. All subsequent clicks only register once the user clicks the same actv twice.

How do I fix this?

like image 345
Razgriz Avatar asked Dec 06 '25 14:12

Razgriz


1 Answers

Used onFocusChangedListener instead. Like so:

    brandACTV.setOnFocusChangeListener(focusChanged);
    itemACTV.setOnFocusChangeListener(focusChanged);
    partACTV.setOnFocusChangeListener(focusChanged);
    barcodeACTV.setOnFocusChangeListener(focusChanged);

and then the listener is defined as such:

OnFocusChangeListener focusChanged = new OnFocusChangeListener() {

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

        Log.d("Hi", "Id is " + v.getId() + ". Focus is " + hasFocus);

        // TODO Auto-generated method stub
        switch( v.getId() ){
            case R.id.actvBrand:
                Log.d("Hi", "Brand focus is + " + hasFocus);
                break;

            case R.id.actvItemName:
                Log.d("Hi", "ItemName focus is + " + hasFocus);
                break;

            case R.id.actvPart:
                Log.d("Hi", "Part focus is + " + hasFocus);
                break;

            case R.id.actvBarcode:
                Log.d("Hi", "Barcode focus is + " + hasFocus);
                break;

        }
    }
};
like image 119
Razgriz Avatar answered Dec 08 '25 04:12

Razgriz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!