Wednesday, April 11, 2012

how to capture events from dynamically added layouts in Android

In my android project, I need to add controls dynamically into my main activity screen. I created one xml (row.xml) which is added on button click on main screen. I want to capture events from the controls (button) given in row.xml.



Can anybody help me where and how to capture onClick events from newly added layouts?



Also, I want to add many child layout elements, do I need to write separate onClick methods for all the child views added dynamically?



row.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_Time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:orientation="horizontal" >


<EditText
android:id="@+id/editText_FromTime"
android:layout_width="216dp"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:hint="@string/hintFromTime" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal" >

<Button
android:id="@+id/button_Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnDelete" />
</LinearLayout>

</LinearLayout>


So, when I click on Add Time slot button, I get a newly created row with two elements.



I want to delete this row when I click on Delete button. Do I need to have a viewID also to delete this newly-added-view?





No comments:

Post a Comment