I want to be able to scan a tag and then populate this data on the listview. I have managed to do this successfully.
However I do not want to keep tapping the editText in order to return the cursor back. The scanning is setup in emulator mode, so when you scan the tag the tag's info gets populated where ever the cursor is.
The problem I am having is once I scan the tag, the cursor does not return back to the editText automatically which means when I want to read another tag I have to keep presses the editText in order for the cursor to return to this place.
The listview gets the data from the editText. The code is shown below with pics of what I have now. From the pics you see that the cursor fails to go back to the editText automatically.
public class MainActivity extends AppCompatActivity {
EditText etRfidNo;
TextView textView;
private Set<String> epc = new HashSet<>();
ArrayAdapter<String> contactAdapter;
String single_epc;
Button scan;
ListView listView;
boolean set = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
etRfidNo = (EditText) findViewById(R.id.etRfidNo);
listView = (ListView) findViewById(R.id.listviewID);
TextView textV = (TextView)findViewById(R.id.textView);
etRfidNo.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
single_epc = String.valueOf(s);
if(s.length() == 10)
{
Date currentDate = new Date();
epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
display();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public void display() {
contactAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<>(epc));
listView.setAdapter(contactAdapter);
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.name.myapplication.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="201dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:cursorVisible="true"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.83" />
<EditText
android:id="@+id/etRfidNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contextClickable="false"
android:ems="10"
android:inputType="text"
android:selectAllOnFocus="false"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.017" />
<Button
android:id="@+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="scan"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<ListView
android:id="@+id/listviewID"
android:layout_width="368dp"
android:layout_height="282dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276"
tools:layout_editor_absoluteX="8dp" />
</android.support.constraint.ConstraintLayout>
You need to put focus on your editText View after scanning of each tag, so cursor will be shown without user input
editText.requestFocus();
Code as follows -
public void onTextChanged(CharSequence s, int start, int before, int count) {
single_epc = String.valueOf(s);
if(s.length() == 10)
{
Date currentDate = new Date();
epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
display();
etRfidNo.requestFocus(); //Putting focus back
}
}
Edit (Fix Issue with XML)
There are issues with your xml file. TextView
is getting overlapped by EditText
which is further overlapped by ListView
and there is no horizontal constraint set on your ListView
as well. I will suggest you to go through Constraint Layout Guide to get more info.
So in simple word, when you are attaching adapter to your ListView
, it is getting focus atomically because then ListView
become the first child of View Hierarchy in your xml. I have updated your layout to use simple linear layout, code as follows -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="201dp"
android:layout_height="53dp"
android:layout_margin="8dp"
android:text="TextView"
android:textAlignment="center"/>
<EditText
android:id="@+id/etRfidNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:contextClickable="false"
android:ems="10"
android:inputType="text"
android:selectAllOnFocus="false"
android:textAlignment="center"/>
<Button
android:id="@+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="scan"
android:text="Button"/>
<ListView
android:id="@+id/listviewID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
</LinearLayout>
Above xml result as follows -
Happy Coding !