• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps View Control Become Selected Upon Button Trigger

beachdogvet

Newbie
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.
 
Need to see your code to answer this. Put code in [code] tags

@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);
}
}
}
}
 
a code tag is like this.. with out the quotes;

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

Example with out the quotes

Code:
 function user_use_code_tag()
{
console.log("success");
}
 
Code:
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);
}
}
}
}
 
To set the focus on your ListView, call
Code:
 lvBarcodeList.requestFocus()

(Credit to StackOverflow http://stackoverflow.com/questions/3168247/set-focus-on-any-item-of-listview-in-android)

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.
 
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.
 
Back
Top Bottom