1. Are you ready for the Galaxy S20? Here is everything we know so far!

View Control Become Selected Upon Button Trigger

Discussion in 'Android Development' started by beachdogvet, Jan 8, 2016.

  1. beachdogvet

    beachdogvet Newbie
    Thread Starter

    I developed an Android application that scans barcodes using Intents for a Motorola TC70 Touch Computer. The application works and all in that I can scan barcodes and put the scanned data (barcode) to a ListView.

    The problem I'm having is that when I trigger a scan using the device's button, the first element in the activity_main gets selected. So for example, I have Menu options. When I push the scan button on the scanner, the options menu becomes selected and the first menu option is displayed.
    If I put a Button above the ListView and scan something, then the Button (that's above the Listview) is then selected. I've tried setting focus to the ListView but that didn't work, or I implemented it wrong.

    When I remove the Menu Options and button, and have nothing but the ListView in the View, it all works like I would expect it to. It's just when I start putting other controls onto the View that it starts acting goofy by the control becoming selected.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Thread moved to forum dedicated to discussing development.

    ... Thom
     
  3. Deleted User

    Deleted User Guest

    Need to see your code to answer this. Put code in [code] tags
     
    Unforgiven and Thom like this.
  4. beachdogvet

    beachdogvet Newbie
    Thread Starter

    Will have to wait until I get home.
     
  5. beachdogvet

    beachdogvet Newbie
    Thread Starter

    @override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // The EMDKManager object will be created and returned in the callback.
    EMDKResults results = EMDKManager.getEMDKManager(getApplicationContext(), this);

    //Check the return status of getEMDKManager
    if(results.statusCode == STATUS_CODE.FAILURE)
    {
    //Failed to create EMDKManager object
    }

    //Get the textViewBarcode
    // textViewBarcode = (TextView) findViewById(R.id.textViewBarcode);

    //In case we have been launched by the DataWedge intent plug-in
    Intent i = getIntent();
    handleDecodeData(i);

    scanBeep = MediaPlayer.create(this, R.raw.beep);
    alBarCodeList = new ArrayList<>();
    // alBarCodeList.add("A friggen barcode");
    arrayListAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,alBarCodeList);
    lvBarcodeList = (ListView) findViewById(R.id.lvBarcodeList);
    lvBarcodeList.setAdapter(arrayListAdapter);
    }

    @override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.menu_main, menu);

    menu.add(0,0,0, "Exit");

    return super.onCreateOptionsMenu(menu);

    }

    @override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case 0:

    if (emdkManager != null) {
    emdkManager.release();
    emdkManager = null;
    }
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(0);
    break;
    }
    //return true;
    return super.onOptionsItemSelected(item);
    }

    @override
    public void onNewIntent(Intent i) {
    handleDecodeData(i);
    }

    private void handleDecodeData(Intent i)
    {
    //Check the intent action is for us
    if (i.getAction().contentEquals("martin.lockheed.com.scannertc70.RECVR") ) {
    //Get the source of the data
    String source = i.getStringExtra("com.motorolasolutions.emdk.datawedge.source");

    //Check if the data has come from the barcode scanner
    if(source.equalsIgnoreCase("scanner"))
    {
    //Get the data from the intent
    String data = i.getStringExtra("com.motorolasolutions.emdk.datawedge.data_string");

    //Check that we have received data
    if(data != null && data.length() > 0)
    {
    //Display the data to the text view
    // textViewBarcode.setText("Data = " + data);
    scanBeep.start();
    alBarCodeList.add(data);
    arrayListAdapter.notifyDataSetChanged();
    // lvBarcodeList.setSelection(0);
    }
    }
    }
    }
     
  6. Robpuk38

    Robpuk38 Newbie

    a code tag is like this.. with out the quotes;

    "["code"]" your code is in here "["/code"]"

    Example with out the quotes

    Code (Text):
    1.  function user_use_code_tag()
    2. {
    3. console.log("success");
    4. }
     
  7. beachdogvet

    beachdogvet Newbie
    Thread Starter

    Code (Text):
    1.  
    2. protected void onCreate(Bundle savedInstanceState) {
    3.  
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.activity_main);
    6.  
    7. // The EMDKManager object will be created and returned in the callback.
    8. EMDKResults results = EMDKManager.getEMDKManager(getApplicationContext(), this);
    9.  
    10. //Check the return status of getEMDKManager
    11. if(results.statusCode == STATUS_CODE.FAILURE)
    12. {
    13. //Failed to create EMDKManager object
    14. }
    15.  
    16. //Get the textViewBarcode
    17. // textViewBarcode = (TextView) findViewById(R.id.textViewBarcode);
    18.  
    19. //In case we have been launched by the DataWedge intent plug-in
    20. Intent i = getIntent();
    21. handleDecodeData(i);
    22.  
    23. scanBeep = MediaPlayer.create(this, R.raw.beep);
    24. alBarCodeList = new ArrayList<>();
    25. // alBarCodeList.add("A friggen barcode");
    26. arrayListAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,alBarCodeList);
    27. lvBarcodeList = (ListView) findViewById(R.id.lvBarcodeList);
    28. lvBarcodeList.setAdapter(arrayListAdapter);
    29. }
    30.  
    31. @override
    32. public boolean onCreateOptionsMenu(Menu menu) {
    33. // Inflate the menu; this adds items to the action bar if it is present.
    34. // getMenuInflater().inflate(R.menu.menu_main, menu);
    35.  
    36. menu.add(0,0,0, "Exit");
    37.  
    38. return super.onCreateOptionsMenu(menu);
    39.  
    40. }
    41.  
    42. @override
    43. public boolean onOptionsItemSelected(MenuItem item) {
    44. switch(item.getItemId()) {
    45. case 0:
    46.  
    47. if (emdkManager != null) {
    48. emdkManager.release();
    49. emdkManager = null;
    50. }
    51. android.os.Process.killProcess(android.os.Process.myPid());
    52. System.exit(0);
    53. break;
    54. }
    55. //return true;
    56. return super.onOptionsItemSelected(item);
    57. }
    58.  
    59. @override
    60. public void onNewIntent(Intent i) {
    61. handleDecodeData(i);
    62. }
    63.  
    64. private void handleDecodeData(Intent i)
    65. {
    66. //Check the intent action is for us
    67. if (i.getAction().contentEquals("martin.lockheed.com.scannertc70.RECVR") ) {
    68. //Get the source of the data
    69. String source = i.getStringExtra("com.motorolasolutions.emdk.datawedge.source");
    70.  
    71. //Check if the data has come from the barcode scanner
    72. if(source.equalsIgnoreCase("scanner"))
    73. {
    74. //Get the data from the intent
    75. String data = i.getStringExtra("com.motorolasolutions.emdk.datawedge.data_string");
    76.  
    77. //Check that we have received data
    78. if(data != null && data.length() > 0)
    79. {
    80. //Display the data to the text view
    81. // textViewBarcode.setText("Data = " + data);
    82. scanBeep.start();
    83. alBarCodeList.add(data);
    84. arrayListAdapter.notifyDataSetChanged();
    85. // lvBarcodeList.setSelection(0);
    86. }
    87. }
    88. }
    89. }
    90.  
     
  8. Deleted User

    Deleted User Guest

  9. beachdogvet

    beachdogvet Newbie
    Thread Starter

    That works sometimes. If I run in debug mode and place a breakpoint at the end of the handleDecodeData(Intent i) method, it works. But then if I just run the app from the scanner, it still selects the Menu first.

    I did notice in debug mode, before I push the "Continue to Run" green arrow the Menu is highlighted. Then after I push the green arrow, focus is then set to the ListView...as expected. However, as mentioned, once I run the App outside of Android Studio, that setFocus for the ListView doesn't happen.

    I'm guessing there's some aysnc processing executing with the EMDK library that I have little or no control of that for whatever reason is doing this.
     
  10. beachdogvet

    beachdogvet Newbie
    Thread Starter

    Came up with a solution. It's kinda of a hack, but it works. When the app first starts out, a blank entry is made to the Listview. Then focus is set to the Listview and the first item is highlighted. Scan a barcode, replace the blank entry with the newly read barcode to the list. Subsequent reads are added to the list as expected. It works and is good enough for me.
     
    Deleted User likes this.
Loading...

Share This Page

Loading...