Edittext in Listview android

user1206620 picture user1206620 · Feb 14, 2012 · Viewed 44.4k times · Source

I have Listview with editext and textview.

When i touch on edittext then edittext lost focus!

I resolved this problem by setting android:windowSoftInputMode="adjustPan"(AndroidManifest.xml). Now i touch on edittext than editext get focus but application label and some raw of listview disappear(top part).

I want to get focus when user touch on edittext without loss application label and some raw of listview.

Code that i have implemented :

Below coding get focus when user touch on edittext but application label and some raw of listview disappear when soft keypad pop up.I want to get focus when user touch on edittext without loss application label and some raw of listview.

1)AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyListViewDemoActivity"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustPan"
                  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

</application>

2) raw_layout.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <EditText android:id="@+id/mEditText"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />  
</LinearLayout>

3) main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

4) MyListViewDemoActivity

public class MyListViewDemoActivity extends Activity {
    private ListView mListView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView=(ListView)findViewById(R.id.mListView);
        mListView.setAdapter(new MyAdapter(this));
    }
}

class MyAdapter extends BaseAdapter {

    private Activity mContext;
    private String character[]={"a","b","c","d","e","f","g","h","i","j"};
    public MyAdapter(Activity context)
    {
        mContext=context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return character.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
private class Holder
{
    EditText mEditText;
}
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final Holder holder;
        if (convertView == null) {
            holder = new Holder();
            LayoutInflater inflater =mContext.getLayoutInflater();
            convertView = inflater.inflate(R.layout.raw_layout, null);
            holder.mEditText = (EditText) convertView
                    .findViewById(R.id.mEditText);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.mEditText.setText(character[position]);
        holder.mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (!hasFocus){
                    final EditText etxt = (EditText) v;
                    holder.mEditText.setText(etxt.getText().toString());
                }

            }
        });
        return convertView;
    }

}

Answer

Cyrille picture Cyrille · Feb 6, 2016

I was having the same problem. My numberic keyboard would momentarily appear before being replaced by the qwerty keyboard and the EditText losing focus.

The problem is that the keyboard appearing makes your EditText lose focus. To prevent this put the following in your AndroidManifest.xml for the appropriate Activity (or Activities):

android:windowSoftInputMode="adjustPan"

See Android documentation:

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's <activity> element with one of the "adjust" values.

For example, to ensure that the system resizes your layout to the available space—which ensures that all of your layout content is accessible (even though it probably requires scrolling)—use "adjustResize"