Reputation: 935
I have a custom edittext that acts like a form. It has a label, edit text and a button. I am using the tag to include it in a layout so I can use the form repeatedly. The function I want the button to have is to reproduce the form when it is clicked. This is possible for me on the first click of the first form. However when I click the button of the new form there is no action. However if I click the first forms button a new form is created. This is my xml: that I include.
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/form_textview"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="14sp" android:text="Number:" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/form_edittext"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton android:id="@+id/form_button"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:src="@drawable/ic_menu_example" android:paddingLeft="1dp"
android:paddingRight="1dp" />
</LinearLayout>
And this is how I am trying to dynamically add new forms:
public final void onClick(final View view)
{
if (view == findViewById(R.id.form_button))
{
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tempView = vi.inflate(R.layout.form, null);
numberEntryViews.add(tempView);
layout.addView(tempView);
ImageButton btn = (ImageButton) tempView.findViewById(R.id.form_button);
btn.setOnClickListener(this);
}
catch (Exception e)
{
e.printStackTrace();
Log.d(TAG, "Failed to inflate");
}
}
}
I have tried various ways in implementing this but I am having no luck. Any help or suggestions would be great thanks.
Upvotes: 0
Views: 1093
Reputation: 935
I figured I had to make a class level button and view to update the function of the button from the new form.
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
LinearLayout layout2 = (LinearLayout) layout.findViewById(R.id.numbersll);
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tempView = vi.inflate(R.layout.form, null);
numberEntryViews.add(tempView);
layout2.addView(tempView);
btn = (ImageButton) tempView.findViewById(R.id.form_button);
//TODO: Work from here. Ask on Stack Overflow.
btn.setOnClickListener(this);
}
catch (Exception e)
{
e.printStackTrace();
Log.d(TAG, "Failed to inflate");
}
btn is class level and is updated on every click. Removes function from the last button which makes sense in my case.
Upvotes: 0
Reputation: 3055
this line will not be true if you click the newly created button
if (view == findViewById(R.id.form_button))
you could assign a fixed id to the newly created button, or try working with offsets
Upvotes: 1