I am trying to add a dynamic view inside my LinearLayout and trying to implement something like to catch every spinner value corresponding to time. Can anyone over here can guide me that how it can be done. I am trying it but it's not working as there can be infinite rows with spinners based on the click on the "ADD FILED" button. I need to collect value from each spinner and need to create an array then.
Here is my code :
public class MainActivity extends Activity {
private LinearLayout parentLinearLayout;
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
parentLinearLayout=(LinearLayout) findViewById(R.id.parent_linear_layout);
}
public void onAddField(View v) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView=inflater.inflate(R.layout.container_item, null);
final EditText text1 = (EditText) rowView.findViewById(R.id.number_edit_text);
final EditText text2 = (EditText) rowView.findViewById(R.id.number_edit_text2);
final EditText text3 = (EditText) rowView.findViewById(R.id.number_edit_text3);
text1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() > 0) {
text3.setText(text1.getText().toString());
Toast.makeText(MainActivity.this,""+s.toString(),Toast.LENGTH_LONG).show();
}
}
});
text2.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() > 0) {
int val = Integer.parseInt(text1.getText().toString());
int val2 = Integer.parseInt(text2.getText().toString());
if(val2>val)
{
}else{
int finalval=val-val2;
text3.setText(""+finalval);
}
Toast.makeText(MainActivity.this,""+s.toString(),Toast.LENGTH_LONG).show();
}
}
});
text3.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() > 0) {
Toast.makeText(MainActivity.this,""+s.toString(),Toast.LENGTH_LONG).show();
}
}
});
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
public void onGetField(View v){
int count = parentLinearLayout.getChildCount();
for (int i = 0; i < count; i++) {
final View row = parentLinearLayout.getChildAt(i);
EditText textOut = (EditText) row.findViewById(R.id.number_edit_text);
String data = textOut.getText().toString();
Toast.makeText(this,""+data,Toast.LENGTH_LONG).show();
}
}
}
Dynamic view XML :
<?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="50dp"
android:orientation="horizontal">
<EditText
android:id="@+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:inputType="phone"/>
<EditText
android:id="@+id/number_edit_text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:inputType="phone"/>
<EditText
android:id="@+id/number_edit_text3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:inputType="phone"/>
<Spinner
android:id="@+id/type_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:entries="@array/types"
android:gravity="right" />
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight=".1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
Main XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/parent_linear_layout"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#555"
android:layout_gravity="center"
android:onClick="onAddField"
android:textColor="#FFF"
android:text="Add Field"
android:paddingLeft="5dp"/>
<Button
android:id="@+id/get_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#555"
android:layout_gravity="center"
android:onClick="onGetField"
android:textColor="#FFF"
android:text="Get Field"
android:paddingLeft="5dp"/>
</LinearLayout>
Please let me know your valuable suggestions on this. I have been stuck since the last day.
Here is the attached image and in the image you can see that the 3rd edit text is empty which I need to fill by using text watcher on 1st two edit-text like 2(first edit-text)-1(second edit-text) = 1 (on thrid edit-text).
Regards
Below is the second approach using Recyclerview:
MainActivity:
public class MainActivity extends AppCompatActivity {
int position=0;
List<Container_Add_model> main_modelList = new ArrayList<>();
RecyclerView recyclerView_container;
TextView Tv_add_container;
Container_Add_Adapter rvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView_container = findViewById(R.id.recyclerView_container);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView_container.setLayoutManager(layoutManager);
Tv_add_container = findViewById(R.id.Tv_add_container);
rvAdapter = new Container_Add_Adapter(MainActivity.this, main_modelList,this, new Container_Add_Adapter.Onclick() {
@Override
public void onEvent(Container_Add_model model, int pos) {
}
});
recyclerView_container.setAdapter(rvAdapter);
Tv_add_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rvAdapter.addClick();
}
});
}
Adapter Class :
public class Container_Add_Adapter extends RecyclerView.Adapter<Container_Add_Adapter.RvViewHolder> {
Activity context;
List<Container_Add_model> models;
Onclick onclick;
View view;
EditTextAdded editTextAdded;
private int container_number_pending, container_number_taken, container_number_given;
public Container_Add_Adapter(Activity context, List<Container_Add_model> models,EditTextAdded editTextAdded, Onclick onclick) {
this.context = context;
this.models = models;
this.editTextAdded = editTextAdded;
this.onclick = onclick;
}
@Override
public Container_Add_Adapter.RvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.from(parent.getContext()).inflate(R.layout.container_item, parent, false);
RvViewHolder rvViewHolder = new RvViewHolder(view);
return rvViewHolder;
}
@Override
public void onBindViewHolder(final Container_Add_Adapter.RvViewHolder holder, final int position) {
final Container_Add_model model = models.get(position);
holder.Edttxt_given_container.setText(models.get(position).container_given+"");
holder.Edttxt_given_container.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() > 0) {
// models.get(position).container_given = Integer.valueOf(s.toString());
}
}
});
holder.Edttxt_taken_container.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() != 0) {
if (!holder.Edttxt_given_container.getText().toString().isEmpty()) {
container_number_given = Integer.parseInt(holder.Edttxt_given_container.getText().toString());
int number = container_number_given + container_number_pending;
int input_num = Integer.parseInt(s.toString());
if (number >= input_num) {
container_number_taken = Integer.parseInt(holder.Edttxt_taken_container.getText().toString());
int final_num = number - container_number_taken;
holder.Edttxt_Pending_container.setText("" + final_num);
} else {
holder.Edttxt_taken_container.setText("");
}
} else {
holder.Edttxt_taken_container.setText("");
}
} else {
if (!holder.Edttxt_given_container.getText().toString().isEmpty()) {
container_number_given = Integer.parseInt(holder.Edttxt_given_container.getText().toString());
if (container_number_given > 0) {
int finalnum = container_number_given + container_number_pending;
holder.Edttxt_Pending_container.setText("" + finalnum);
} else {
holder.Edttxt_Pending_container.setText("" + container_number_pending);
}
} else {
holder.Edttxt_Pending_container.setText("" + container_number_pending);
}
}
}
});
holder.Iv_delete_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
models.remove(position);
notifyItemChanged(position);
notifyItemRangeChanged (position, models.size());
}
});
}
public void addClick(){
Container_Add_model container_main_model = new Container_Add_model();
container_main_model.setContainer_given(0);
container_main_model.setContainer_pending(0);
container_main_model.setContainer_taken(0);
container_main_model.setContainer_id(models.size());
container_main_model.setContainer_name("");
models.add(container_main_model);
//notifyDataSetChanged(models);
notifyItemInserted(models.size() + 1);
}
@Override
public int getItemCount() {
return models.size();
}
public interface Onclick {
void onEvent(Container_Add_model model, int pos);
}
public class RvViewHolder extends RecyclerView.ViewHolder {
TextView Tv_container_type;
EditText Edttxt_given_container, Edttxt_taken_container, Edttxt_Pending_container;
LinearLayout llItem;
ImageView Iv_delete_container;
public RvViewHolder(View itemView) {
super(itemView);
Tv_container_type = itemView.findViewById(R.id.Tv_container_type);
Iv_delete_container = itemView.findViewById(R.id.Iv_delete_container);
Edttxt_given_container = itemView.findViewById(R.id.Edttxt_given_container);
Edttxt_Pending_container = itemView.findViewById(R.id.Edttxt_Pending_container);
Edttxt_taken_container = itemView.findViewById(R.id.Edttxt_taken_container);
}
}
}
Here is what you were missing :
Datalist Model :