Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Return cursor to starting position

Posted on November 14, 2022

Solution 1 :

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 –

enter image description here

Happy Coding !

Problem :

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.

READ  [ANSWERED] android - Could not GET 'http://jcenter.bintray.com/com/google/jimfs/jimfs/1.1/jimfs-1.1.pom'. Received status code 403 from server: Forbidden
Powered by Inline Related Posts

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);

  }

}

Starting the application

Data populated on listview

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>

Comments

Comment posted by Derick Brown

It is still not displaying the cursor on the editText

READ  [ANSWERED] java - How to get the relative Enum from a value as param?
Powered by Inline Related Posts

Comment posted by Nikhil Sharma

strange … put etRfidNo.setText(“”); just before calling requestFocus()

Comment posted by Nikhil Sharma

I did a little repro and for me EditText was not loosing focus untill I use back button to close soft keyboard, how your edit text is loosing focus at first place .. ?

Comment posted by Derick Brown

It is losing focus as soon as I scan a tag and the data is displayed in the listview.

Comment posted by Nikhil Sharma

I found issue with your xml file, please check my edit on answer

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress