0
I am developing the following application in android Studio-
    **user on entering todo  and click add button, it Should add to linear layout with checkbox which 
    is helpful to delete todos.**

my idea: I created a new XML file with textView and CheckBox in single_item.xml  and inflate it to  
         linearlayout which is present in main xml  as shown in 
          below.............................................

problem:  when I clicked the add button , app crashes. 
           I found that this issue is because I added textview.setText(text)  in  java code.
            if I had not added this line , then sample single_element.xml is getting added 
                 LinearLayout.But I needed to content of textview to be set to input 
                 that is given by user in editText as shown.



     I had deleted Most of the code in xml file because Stackoverflow is showing me "It looks like your post is mostly code; please add some more details." ""...............................................
          ..................................................................................................................................................................................................................................................................................................................................................................................

[activity_main.xml][1]
[single_element.xml][2]


[1]: https://i.stack.imgur.com/UhtGJ.png
[2]: https://i.stack.imgur.com/Pcu1O.png

In activity_main.xml I had LinearLayout,editText View,Add Button and delete button.

LinearLayout  -> id="@+id/linearLayout"
    

EditText        -> android:id="@+id/edit"

<Button        ->android:id="@+id/add_button"
    

single_element.xml

TextView     ->id="@+id/textView"
   
CheckBox    ->id="@+id/checkBox"
    

MainActivity.java

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
            EditText editText = (EditText) findViewById(R.id.edit);
            String text = editText.getText().toString().trim();
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText(text);



            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.single_element,null);
            linearLayout.addView(view);
        }
    });
   

Thanks in Advance.................................................................................................................................................................................................................................................................................................................................................................................................

Asif Sk
  • 3
  • 1

1 Answers1

0

You have to put the textbox and the Checkbox, all in the activity_main.xml, no need to create another layout file (single_element.xml) to add them since you need to display the text and the checkbox in the MainActivity class. Otherwise, you'll have to create another activity for single_element.xml that can be called SingleElementActivity for example then you pass the data entered in the editText from MainActivity to this SingleElementActivity using an intent.

See here how you can pass data from an activity to another activity with an Intent: How do I pass data between Activities in Android application?