vishalkin
vishalkin

Reputation: 1235

Android: Giving ID's to Dynamically created Edit Texts

I have a following layout that is generated dynamically on Add Button

rowdetail.xml

        <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:layout_gravity="left"
                android:id="@+id/editText"/>

        <View android:layout_width="0dp" android:layout_height="wrap_content"
              android:layout_gravity="center" android:layout_weight="0.2">

        </View>
        <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:layout_gravity="right"
                android:id="@+id/editText1"/>

    </LinearLayout>
</LinearLayout>

OnClick()

final LinearLayout newView = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.rowdetail, null);
                newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                linearLayoutForm.addView(newView);

How do I give ID's to the Edit Texts? All the EditText on left side will have one set of id and the right side will havee another set of id.

Example

 EditTextLeft1     EditTextRight1 
 EditTextLeft2     EditTextRight2 
 EditTextLeft3     EditTextRight3 
 EditTextLeft4     EditTextRight4 

Upvotes: 0

Views: 71

Answers (1)

Sabeer
Sabeer

Reputation: 4110

You can't use dynamic object names ... try this way

for(int i=0;i<buttonCount;i++){
    EditText editText = new EditText(context);
    editText.setId(i+1);
}

later you can use editText.getId() to identify the instance...

Upvotes: 2

Related Questions