0

Looking for help on this. I have 2 EditTexts on an activity. When user get to screen the first EditText has focus for input from a scanner. User scans barcode and program validates it is a good read by finding corresponding item from database. If finds the item I am trying to get the second EditText to have focus for the next scan.

I am new to java (VB windows developer) and struggling to accomplish this. No matter what I have tried everytime I scan the second barcode the first EditText gets what is scanned.

I am having hard time figuring out how to change the listener to be focused on Second EditText.

Can someone please explain how to change the focus to second EditText after a valid first scan.

public void listener() {
        scanner.scannerListener = new ScannerListener() {
            @Override
        public void listener() {
           sChecker = mEntryText.getText().toString();

            if (sChecker.equals("")) ;
            {
                mScan = scanner.getScanResult();
                Timber.d("The barcode is " + mScan);
                if (!mScan.equals("")) {

                    int lmScan = mScan.length();
                    if (lmScan > 15) {
                        checkGs1Barcode();
                    }
                    String s = DatabaseMgr.VerifyItem(mScan);
                    if (s.length() > 1) {
                        if (s.equalsIgnoreCase("Unknown Item###")) {
                            Toast.makeText(getApplicationContext(), "Unknown Item", Toast.LENGTH_SHORT).show();
                        } else if (s.equalsIgnoreCase("Multiple Items###")) {
                            setupMultiItems(mScan);
                        } else {
                            String[] sArr = s.split("\\#");
                            String IsItem = sArr[0];
                            String IsItemDesc = sArr[1];
                            String isLevel = sArr[2];
                            String isLvlDesc = sArr[3];
                            Button ItemDescText = findViewById(R.id.btnDesc);
                            ItemDescText.setText(IsItemDesc);
                            TextView ItemUPCText = findViewById(R.id.editUPC);
                            ItemUPCText.setText(mScan);

                            Integer iQTY = ValidateQTYinArea(IsItem, "RTN", myArea, isLevel);
                            TextView mQty = findViewById(R.id.txtDetail);
                            String myResult = "RTN - " + myArea + " - " + isLvlDesc + " - " + iQTY;
                            mQty.setText(myResult);
                            mEntryText.setBackgroundColor(getResources().getColor(R.color.colorWhite));

                            mLoc.setBackgroundColor(getResources().getColor(R.color.colorYellow));
                            mLoc.requestFocus();
                        }
                    }
                } else {
                    mLoc.setText(scanner.getScanResult());
                }

            }

        }
    };
}

the mLoc.setText line is not getting hit

Tom
  • 151
  • 1
  • 12
  • 2
    You are mixing 3 things here: listener, focus, setting values. It's 3 different concepts. Basically what you want to do is to set a value. Post your code if you want us to help, as we can't guess what you've tried – Eselfar May 29 '18 at 15:58
  • create global static variable . – mah454 May 29 '18 at 15:59
  • Here is what I have for listener with my last attempt. – Tom May 29 '18 at 16:05
  • In the first listener, set focus on the second edittext, per this existing SO: https://stackoverflow.com/questions/3234607/how-to-set-focus-to-a-button-widget-programmatically – user1676075 May 29 '18 at 16:15

1 Answers1

1

So what you need to do is:

if (sChecker.equals("")){
   mEditText1.setText("text to be set");
}else{
   mEditText2.setText("text to be set");
}
Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • Eselfar that is what I have. but when I debug the first time throught sChecker does equal "' and works as expected. Second time sChecker = '075324864152' and the overwrites mEditText1 with new text. – Tom May 29 '18 at 16:16
  • 1
    Can you please post your entire code as the error should be elsewhere? – Eselfar May 29 '18 at 16:21
  • 1
    If you want `mLoc.setText(scanner.getScanResult());` to be executed when `sChecker.equals("")` you must put the else condition on the correct `if`. At the moment this code is executed when `!mScan.equals("")` – Eselfar May 29 '18 at 16:31
  • 1
    Press CTRL+ALT+L on Android Studio (on Windows) for your code to be automatically formatted. (Code → Reformat code, from the menu) – Eselfar May 29 '18 at 16:32
  • 1
    I see what you are saying. Playing with the brackets now. – Tom May 29 '18 at 17:08
  • It's generally a good practice to split your code in smaller functions instead of having a big block of code like your listener. It's more readable. Basically you do `if (A){ callMethod1(); } else if (B) {callMethod2();} else { do something else; }`. And it avoid this kind of issue, plus its easier when you want to write tests – Eselfar May 29 '18 at 17:12
  • Thank you. Finally figured it out. My background didn't use brackets. But I was able to get it. Thanks for the CTRL+ALT+L to reformat thecode – Tom May 29 '18 at 17:24