venkat venkat - 5 months ago 9
Android Question

How to add text field with button when click on Add(+) button, again click new added button add one more fields in android?

i am suffering from last one week with this problem. I have one row with EditText text field, setTime button, and Addmore button. Now when i click setTime button open dialod and set time, it will displayed in text field. After this click on Addmore button then create new row same like above and when click setTime button again add time into text field. Now click on Addmore button it will create another row like above with same functionality. I did when click on setTime button it will add to text Field. now i want to click Addmore button it will create new row like above with same fuctionality with the set time and add more button.
I did set time code.

public class MainActivity extends ActionBarActivity implements OnClickListener
{
Button SetTime,addFields;
EditText timeTxt;
SetTime = (Button) findViewById(R.id.SetTime);
SetTime.setOnClickListener(this);

if (v == SetTime) {

// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMin = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
timeTxt.setText(hourOfDay + ":" + minute);
}
}, mHour, mMin, false);
tpd.show();
}

}


XML File

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">

<EditText
android:layout_width="240dp"
android:layout_height="65sp"
android:id="@+id/txtTime"
android:textSize="27sp">

<requestFocus>
</requestFocus>

</EditText>
<Button
android:id="@+id/SetTime"
android:layout_marginLeft="25sp"
android:layout_width="200sp"
android:layout_height="65sp"
android:textSize="30sp"
android:text="SetTime" >
</Button>
<Button
android:id="@+id/AddTimings"
android:layout_marginLeft="10sp"
android:layout_width="140sp"
android:layout_height="65sp"
android:textSize="30sp"
android:text="ADD(+)" >
</Button>
</LinearLayout>

Answer

i found very good example...this resolved my problem

http://android-er.blogspot.in/2013/05/add-and-remove-view-dynamically.html
Comments