I have made an simple android activity,In that i am having a plus button and a minus button,when i click plus button a view(row) with some EditTexts will be inflated into LinearLayout,as many as times add button clicked,So i am done it successfully,Now I want to get each EditText
value,but I have searched a lot and am stuck in this,So Please help me in this,My code is as below:
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hiddenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:id="@+id/ll_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_del"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:weightSum="5" >
<TextView
android:id="@+id/lblb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Item name/ID :"
android:textSize="12dp" />
<EditText
android:id="@+id/et_item_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/item_id_txt"
android:inputType="number"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="12dp" />
</LinearLayout>
<TextView
android:id="@+id/lblz"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_a"
android:text="Description :"
android:textSize="12dp" />
<EditText
android:id="@+id/et_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lblz"
android:layout_marginTop="5dp"
android:background="@drawable/desrciption"
android:gravity="center_vertical"
android:hint="Description"
android:inputType="text"
android:padding="2dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="14dp" />
<TextView
android:id="@+id/tv_qt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_desc"
android:layout_marginLeft="15dp"
android:text="Quantity"
android:textSize="12dp" />
<EditText
android:id="@+id/et_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_qt"
android:background="@drawable/amount_txt"
android:ems="5"
android:inputType="number"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="12dp" />
<TextView
android:id="@+id/tv_ut_prz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_desc"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/et_qty"
android:text="Unit Price"
android:textSize="12dp" />
<EditText
android:id="@+id/et_unit_prize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_ut_prz"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/et_qty"
android:background="@drawable/amount_txt"
android:ems="5"
android:inputType="number"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="12dp" />
<TextView
android:id="@+id/tv_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_desc"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@+id/tv_ut_prz"
android:text="Amount"
android:textSize="12dp" />
<EditText
android:id="@+id/et_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv_amt"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/et_unit_prize"
android:background="@drawable/amount_txt"
android:ems="5"
android:inputType="number"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="12dp" />
<ImageView
android:id="@+id/iv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:background="@drawable/minus" />
<View
android:layout_width="fill_parent"
android:layout_height="0.75dp"
android:background="#cecece"
android:layout_marginTop="10dp"
android:layout_below="@+id/et_amt"
/>
</RelativeLayout>
Code:
//code to adding layout on buttonClick
case R.id.iv_add:
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.raw_descs, null);
ImageView buttonRemove = (ImageView) addView.findViewById(R.id.iv_del);
et_item_id = (EditText) addView.findViewById(R.id.et_item_id);
et_desc = (EditText) addView.findViewById(R.id.et_desc);
et_qty = (EditText) addView.findViewById(R.id.et_qty);
et_unit_prize = (EditText) addView.findViewById(R.id.et_unit_prize);
et_amt = (EditText) addView.findViewById(R.id.et_amt);
et_qty.addTextChangedListener(textwatcher);
et_unit_prize.addTextChangedListener(textwatcher);
buttonRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
((LinearLayout) addView.getParent()).removeView(addView);
calculateInvoice();
if (cnt >= 0) {
cnt = cnt - 1;
}
}
});
cnt = cnt + 1;
listitems.setTag(cnt);
listitems.addView(addView);
calculateInvoice();
break;
private void calculateInvoice() {
double QuantyInt = 1;
double PriceInt = 0;
double discountInt = 0;
double shipInt = 0;
for (int i = 0; i < listitems.getChildCount(); i++) {
et_qty = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(4);
et_unit_prize = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(6);
if (et_qty != null) {
QuantyInt = Double.parseDouble(!et_qty.getText().toString().equals("") ? et_qty.getText().toString() : "0");
}
if (et_unit_prize != null) {
PriceInt = Double.parseDouble(!et_unit_prize.getText().toString().equals("") ? et_unit_prize.getText().toString() : "0");
}
subtotal = subtotal + (QuantyInt * PriceInt);
}
double textResult = subtotal;
System.out.println("::::::::::::MY TOATAL PRICE::::::::::::::::>>>>>>>>>>>>>>>>" + subtotal);
et_amt.setText("$" + textResult + "");
tv_pre.setText("$" + textResult + "");
if (et_dis != null) {
discountInt = Double.parseDouble(!et_dis.getText().toString().equals("") ? et_dis.getText().toString() : "0");
}
discount = ((subtotal * discountInt) / 100);
double txtdiscount = discount;
tv_dis.setText("$" + txtdiscount + "");
double totl1 = subtotal - discount;
tv_total.setText("$" + totl1 + "");
if (et_ship != null) {
shipInt = Double.parseDouble(!et_ship.getText().toString().equals("") ? et_ship.getText().toString() : "0");
}
tv_ship.setText("$" + et_ship.getText().toString());
double finaltotal = (shipInt + totl1);
tv_total.setText("$" + finaltotal + "");
}
TextWatcher textwatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
calculateInvoice();
}
};
This way i am inflating layouts,Please help me how to get edittexts values from each inflated layouts?