Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:07:37+00:00 2026-06-09T04:07:37+00:00

I have a EditText that reads a 13 digit barcode. What I want to

  • 0

I have a EditText that reads a 13 digit barcode. What I want to do is to keep the virtual keyboard shown on the screen and the EditText to always have focus. The following code let me write the barcode and search for a product when enter key is pressed, and it works well. But if I type a barcode with less then 13 digits or the barcode typed does not exist in my database, I want to show the user a Toast, informing him of it. After displaying the Toast, I want the EditText to gain focus again automatically, letting the user just type the barcode again. After showing the Toast, I tried the requestFocus() method, but it didn’t work. The soft keyboard is always shown, but after the Toast, I can’t type in the EditText again unless I touch on the EditText. How can I do this?

    final EditText procura_codbar = (EditText)
                findViewById(R.id.procurar_produto_codbar);
    procura_codbar.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                String codbar = procura_codbar.getText().toString();
                if (codbar.length()<13){   
                    Toast.makeText(MainActivity.this,
                           "type a 13 digit barcode",
                           Toast.LENGTH_LONG).show();
                }
                else{
                    if (bdh!=null){
                        bdh.closedb(); bdh.close();
                    }
                    bdh = new DBHelper(MainActivity.this);
                    Log.i("CODBAR", codbar);
                    produto prod_ = bdh.getProduto(codbar);
                    if (prod_!=null){
                        showDialogPreco(prod_);
                        procura_codbar.setText("");
                    }else{
                        Toast.makeText(MainActivity.this,
                                                     "Product not found",
                                                     Toast.LENGTH_SHORT).show();
                        procura_codbar.setSelection(codbar.length());
                    }
                }
                procura_codbar.requestFocus();
                procura_codbar.setSelection(codbar.length());
            }

            return false;
        }
    });

And here is the XML:

<EditText
    android:id="@+id/procurar_produto_codbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:inputType="textNoSuggestions|number"
    android:textSize="22sp"
    android:maxLength="13"
    android:layout_toRightOf="@+id/tv_procura_codbar" >

    <requestFocus />
</EditText>

Thanks in advance.

EDIT: Messing with this, I found the problem. Heres the solution:

return true;

Now it works…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:07:38+00:00Added an answer on June 9, 2026 at 4:07 am

    Put this code right next to that Toast Message:

    Toast.makeText(MainActivity.this,
                           "type a 13 digit barcode",
                           Toast.LENGTH_LONG).show();
    
    procura_codbar.setFocusableInTouchMode(true);
    procura_codbar.setFocusable(true);
    procura_codbar.requestFocus();
    procura_codbar.setSelection(codbar.length());
    

    This worked for me. Also you need to remove the *procura_codbar.requestFocus();* at the bottom of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an EditText and I want to attach to it a KeyListener that
I have an editText that brings up the number keyboard. When you enter a
I have an EditText that shows time. After user clicks the EditText I want
I have an EditText that I want to respond to a user pressing the
I have an EditText input that I would like to only allow numbers 1
I have a custom EditText MomentumEditText that I have in a layout in xml
I have an edittext, and a textwatcher that watches if SPACE arrived or not.
To use as an example: lets say that I have 2 EditText s and
I have a UI that has 6 EditText boxes. 3 of those EditText boxes
I have an activity that shows some TextView s and EditText s, as well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.