This tutorial will help you to enter and get text in recycler view.
Steps to use Edittext in list in your project.
Steps to use Edittext in list in your project.
- Crate a project(EdittextInListDemo) in android studio.
- Create a class MainActivity.Java and update it-
package com.pankaj.gsolc.edittextinadaptersdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; import java.util.Calendar; import java.util.List; public class MainActivity extends AppCompatActivity { //create objects private List<Data> data = new ArrayList<>(); private MyAdapter myAdapter; RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initialize recyclerView = findViewById(R.id.recyclerView); //add one data Data data = new Data(); data.setPosition(0); data.setString(""); this.data.add(data); //initialize adapter myAdapter = new MyAdapter(this, this.data); //add layout manager to recycler view recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); //add adapter recyclerView.setAdapter(myAdapter); } public void addOneMore(View view) { //Check if more than one data available, Than previous edittext should not be empty. if (data.size() > 0) if (data.get(0).getString().isEmpty()){ Toast.makeText(this, "Please fill the empty fields first.", Toast.LENGTH_SHORT).show(); return; } //Create and add one blank data to entry Data data = new Data(); data.setPosition(this.data.size()); data.setString(""); this.data.add(0, data); //notify adapter myAdapter.notifyDataSetChanged(); } public void showData(View view) { //check if no data available, than return from here if (data.size() < 1) return; String enteredData = ""; for (Data data : data) { enteredData += data.getString() + "\n"; } Toast.makeText(this, enteredData, Toast.LENGTH_SHORT).show(); } }
- Create a class Data.java to store data.
package com.pankaj.gsolc.edittextinadaptersdemo; /** * Created by gsolc on 8/11/17. */ public class Data { int position; String string; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public String getString() { return string; } public void setString(String string) { this.string = string; } }
- Create MyAdapter.java and update it-
package com.pankaj.gsolc.edittextinadaptersdemo; import android.app.Activity; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import java.util.List; /** * Created by gsolc on 7/11/17. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<Data> data; private Activity activity; public MyAdapter(Activity activity, List<Data> data) { this.activity = activity; this.data = data; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.row_with_edit_text, parent, false); return new ViewHolder(itemView); } @Override public void onBindViewHolder(final MyAdapter.ViewHolder holder, int position) { //set positions to listeners holder.textWatcher.setPosition(position); holder.clickListener.setPosition(position); //set previous data on refreshing if (data.get(position).getString() != null) holder.editText.setText(data.get(position).getString()); else holder.editText.setText(""); } @Override public int getItemCount() { return data.size(); } public class ViewHolder extends RecyclerView.ViewHolder { //create objects final public EditText editText; MyTextWatcher textWatcher; MyClickListener clickListener; public Button btnDelete; public ViewHolder(View itemView) { super(itemView); //initialize objects editText = itemView.findViewById(R.id.editText); btnDelete = itemView.findViewById(R.id.btnDelete); //initialize listeners textWatcher = new MyTextWatcher(editText); clickListener = new MyClickListener(); //add listeners editText.addTextChangedListener(textWatcher); btnDelete.setOnClickListener(clickListener); } } //create a text change listener public class MyTextWatcher implements TextWatcher { private EditText editText; private int position; public void setPosition(int position) { this.position = position; } public MyTextWatcher(EditText editText) { this.editText = editText; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //add data into data list data.get(position).setString(s + ""); } @Override public void afterTextChanged(Editable s) { } } //Create a listener to remove any edittext public class MyClickListener implements View.OnClickListener { private int position; public void setPosition(int position) { this.position = position; } @Override public void onClick(View view) { //remove and notify data.remove(position); notifyDataSetChanged(); } } }
Create row of adapter row_with_edit_text.xml file and update it-<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Enter Text " android:inputType="text" /> <Button android:id="@+id/btnDelete" android:layout_width="40dp" android:layout_height="match_parent" android:background="@android:drawable/ic_delete" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
Open your main_activity.xml file and update it-<?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.pankaj.gsolc.edittextinadaptersdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="addOneMore" android:text="Add One More" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:scrollbars="vertical" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showData" android:text="Show entered data" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
- All the application development process with Edittext in list has completed, Now run the app and look the screen.
- Good bye, Thanks to read this blog.
No comments:
Post a Comment