Edittext In Listview Android Example

Velmurugan Murugesan
Sep 2 · 2 min read

I have written a simple step to create edittext in listview. Please follow the step to do this task.

Image for post
Image for post

Steps to create edittext in listview android

  • Create Listview with EditText
  • Setup data into Listview
  • Configure the Edittext
  • Add Textwatcher to Edittext

Create listview with editText

Create simple main.xml with listview and set listview properties for the fast listview scrolling.

<ListView android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true"
>
</ListView>

Create listview_adapter.xml adapter layout with edittext.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:padding="5dp"
>

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Enter Random Number"
android:gravity="center_vertical"
android:layout_weight="0.5"
/>

<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:layout_weight="1.5"
android:ems="10"
/>

</LinearLayout>

Setup data into listview

Create ArrayList add data into the ArrayList.

lv=(ListView)findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
for(int i=0;i>=30;i++){
list.add(i);
}

Configure the edittext

  1. create an adapter for the listview and set the position as a tag for the edittext.
  2. Normally, when scrolling the item position will change. So, you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.
holder.caption = (EditText) convertView
.findViewById(R.id.editText12);
holder.caption.setTag(position);
holder.caption.setText(list.get(position).toString());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);

Add textwatcher to edittext

Finally, add the text watcher to the edittext and store the changes into the correct position in the list.

holder.caption.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
final int position2 = holder.caption.getId();
final EditText Caption = (EditText) holder.caption;
if(Caption.getText().toString().length()&amp;amp;amp;amp;amp;amp;amp;gt;0){
list.set(position2,Integer.parseInt(Caption.getText().toString()));
}else{
Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {

}

});

Screenshot

Image for post
Image for post

Download this example in GITHUB.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store