How to hide multiple cursors when displaying an EditText within a ListView?
Asked Answered
E

3

6

I have created a ListView whose children consist of a single EditText. However, when I click on the EditText, giving it focus, then notifyDataSetChanged(), updating the ListView, then click on any of the EditTexts again, a cursor is drawn for every EditText in the list (Nexus 5, emulators, etc.)

On a Nexus 5Nexus 5

The following are my xml for the list and list item.

activity_main.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zlistfocus.MainActivity"
    tools:ignore="MergeRootFrame" />

listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:paddingBottom="4dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="4dp" />

Because I want the EditText to be able to gain focus, I followed some tips and pointers here: https://mcmap.net/q/145838/-focusable-edittext-inside-listview. Specifically, I added android:windowSoftInputMode="adjustNothing" to the Activity tag in AndroidManifest.xml so that list items can gain focus at all, creating this issue. Then I tried attaching the OnItemSelectedListener to the listview as the selected answer suggested. However, these ghost cursors still exists.

@AnswerBot: I am using an ArrayAdapter, which holds a list of integers.

public class MainActivity extends ActionBarActivity {

    private ListView lv;
    private ArrayAdapter<Integer> adapter;
    private List<Integer> data = new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);
        adapter = new ArrayAdapter<Integer>(this, R.layout.listview_item, R.id.tv, data);
        lv.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 0, "Refresh");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == 1) {
            getSomeData();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void getSomeData() {
        if (data.isEmpty()) {
            for (int i = 0; i < 20; i++) {
                data.add(i);
            }
        } else {
            int start = data.get(data.size() - 1) + 1;
            for (int i = 0; i < 20; i++) {
                data.set(i, start + i);
            }
        }
        adapter.notifyDataSetChanged();
    }
}
Emboly answered 31/3, 2014 at 22:18 Comment(2)
If you have used custom adapter please post itDeadradeadweight
I have updated my question with much clarity.Emboly
C
3

You can hide the cursor for an EditText by calling setCursorVisible(false) on the EditText(s) in question.

Edit: For this you will probably want to use a custom adapter (BaseAdapter), and in the getView method do something like this:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ...

    editText.setCursorVisible(editText.hasFocus());

    return convertView;
}
Char answered 6/5, 2014 at 18:25 Comment(1)
This solution solved the multi-cursor problem. But now, I do not see the selector at all. Is there a way to show the selector?Palatalized
M
0

You should clear the focus on all editTexts then request focus in the target editText that you want to set focus in it.

//clearList has All EditText
public void setEdFocus(EditText targetED) {
    for (EditText editText : clearList) {
        if (editText != targetED&& editText.hasFocus()) {
            editText.clearFocus();
        }
    }
    
    targetED.requestFocus();
}

It works with me in this way, the clearList is list has all EditText

Mingrelian answered 11/2, 2021 at 19:36 Comment(0)
F
0

Add This Command To The Desired Activity in Manifest.xml.

android:windowSoftInputMode="stateAlwaysHidden"
Fornof answered 1/8, 2021 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.