Till S
Till S

Reputation: 465

EditText in ListView hidden by keyboard when focused

I have a ListView containing rows with EditText's.

When I click on an EditText and it's not already focused it receives focus, the keyboard appears and the EditText is moved above the CandidateView (desired behaviour).

However when I make a key press, the EditText maintains focus and receives the input but moves down and is obscured by the keyboard (the previous movement is reversed).

When I click on the EditText when it is already selected without the keyboard shown, the keyboard pops up but the EditText is not moved to the right position (above the CandidateView). It still receives the input.

I add a header view containing EditText's and there everything works correctly.

This is the code of my ArrayAdapter where the row view is created:

View view = inflater.inflate(R.layout.row_profile_entry_text, null);


final String question = getItem(position);

TextView textViewQuestion = (TextView) view.findViewById(R.id.rpeq_TextViewQuestion);
textViewQuestion.setText(question);

final EditText editTextAnswer = (EditText) view.findViewById(R.id.rpeq_EditTextAnswer);

editTextAnswer.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        mAnswers.put(question, s.toString());
    }
});

if (mAnswers.containsKey(question)) {
    editTextAnswer.setText(mAnswers.get(question));
}

return view;

I would also like to emphasize that I already added
android:windowSoftInputMode="adjustPan" to the Manifest and
android:descendantFocusability="beforeDescendants" to the ListView as most of the answers to other questions suggest.

Without adjustPan the EditText is not able to receive focus at all but it does not solve the issue entirely.

Does someone have an idea what I am doing wrong?

Upvotes: 10

Views: 6223

Answers (2)

Krinet
Krinet

Reputation: 1

after many hours spent on this problem this is my solution (for Android < 4.2):

1) Manifest => android:windowSoftInputMode="adjustPan"

2) Activity=> create OnTouchListner and pass to Adapter

    private OnTouchListener exampleOnTouchListener = new OnTouchListener() {

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_UP == event.getAction()) {
            int position = (Integer) v.getTag();
            myListView.setSelection(position);
        }
        return false;
    }
    };

3) Adapter

        if (exampleOnTouchListener!= null) {
        myEditText.setTag(position);
        myEditText.setOnTouchListener(exampleOnTouchListener);

        // if last element set padding bottom for spacing
        if (position == items.size() - 1) {
            LinearLayout myLinearLayout = (LinearLayout) convertView
                    .findViewById(R.id.ticketcontolProposedRealvalueLinearLayout);
            myLinearLayout.setPadding(0, 0, 0,SET_PADDING_BOTTOM);
        }
        }

Upvotes: 0

Archie.bpgc
Archie.bpgc

Reputation: 24012

Try this:

<activity name="YourActivity"
    android:windowSoftInputMode="stateVisible|adjustResize|adjustPan">
</activity>

in the manifest file.

Most probably, adjustResize must work, if you are using a ScrollView.

Upvotes: 1

Related Questions