Reputation:
Trying to create notes application. I am trying to add dynamic text view after button click and it should be added to layout, one after another. In my code, only one text view is getting added successfully but from second time, app got crashed. Please help
XML Code:
<EditText
android:id="@+id/text_thingsToDo"
android:layout_width="@dimen/ThingsToDo_EditText_Width"
android:layout_height="@dimen/ThingsToDo_EditText_Height"
android:layout_margin="@dimen/Standardize_Margin"
android:background="@color/ThingsToDo_EditText_Color"/>
<Button
android:id="@+id/save_thingsToDo"
android:layout_toRightOf="@+id/text_thingsToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ThingsToDo_Save_Button_Text"
android:layout_marginRight="@dimen/Standardize_Margin"
android:layout_marginTop="@dimen/Standardize_Margin"
android:layout_marginBottom="@dimen/Standardize_Margin"
android:background="@color/ThingsToDo_Save_Button"
android:textColor="@color/White"
android:layout_centerVertical="true"/>
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_things_to_do);
final EditText editText = (EditText) findViewById(R.id.text_thingsToDo);
Button button = (Button) findViewById(R.id.save_thingsToDo);
final TextView notesView = new TextView(this);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rootView_ThingsToDo);
final ArrayList<String> arrayList = new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
}
Upvotes: 0
Views: 68
Reputation: 646
Perhaps create a new TextView every time you press the button? Try to declare the TextView as a class member and then in your OnClickListener
do this:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notesView = new TextView(this);
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
Hopefully this will fix your problem!
Upvotes: 1