Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make it so every time someone type something in the editText and click the button the text comes up which it does it my code but I also want to add a checkbox next to it. How Can I add the checkbox every time the button is pressed. 

I would also want to make it possible to add more than 1 right now it overwrites itself when you make 2.


final TextView textViewPrint;
  
Button btn_print;

final EditText editTextType;
     
final CheckBox checkbox;


        textViewPrint = findViewById(R.id.print_text);
        btn_print =findViewById(R.id.button_add);
         editTextType = findViewById(R.id.test_textEdit);
        checkbox = findViewById(R.id.checkBox);


        
         btn_print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                textViewPrint.setText(editTextType.getText().toString()) ;


                editTextType.setText("");
            }
        });


What I have tried:

I have tried looking for answers on google / stack overflow. I'm a beginner so I miss a lot of basic understandings.
Posted
Updated 5-Apr-19 1:45am
Comments
David Crow 2-Apr-19 21:28pm    
checkbox = findViewById(R.id.checkBox);

If this is returning a non-null value, then I assume you already have a Checkbox widget on the layout. if so, then what exactly are you wanting to do with the checkbox?
Sebassstian 3-Apr-19 6:10am    
I want to create a checkbox onClick so when the button is clicked I want it to create a checkbox.
David Crow 3-Apr-19 8:02am    
Have you tried something like:

Checkbox cb = new Checkbox();
cb.setId(###); // optional


If you want to display it, you'll also need to add it to a view.

Just out of curiosity, why create this control at runtime rather than in the layout file?
Sebassstian 3-Apr-19 14:43pm    
I want to make a to-do list so when a user type for example play piano in the editText my app Will add play piano and a checkbox that they can press to finish it. Im also having problems beeing able to add more than 1 task. My text need beeing overwritten by the last one. For example if i wrote play piano and added it then go the gym. The gym one would overwrite the last one. I’m not at a computer right now I am going to try if it works tomorrow thanks alot
David Crow 3-Apr-19 15:24pm    
You need to put a ListView in the main layout. Create a "row" layout for each row in that ListView. In this row layout, put a CheckBox control and a TextView control. Create a custom adapter for it (e.g., derived from ArrayAdapter). In the adapter's getView() method, inflate the row layout, and then assign values to the checkbox and text controls. In the button's onClick() handler, simply add a new item to the custom adapter, then call the adapter's notifyDataSetChanged() method for it to show up in the list.

This is a VERY common thing to do, so more detailed examples are plentiful on the 'net.

1 solution

LinearLayout l1 = (LinearLayout)findViewById(R.id.linearlayout);
btn_print.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          CheckBox checkbox = new CheckBox(this);
        checkbox.setId(0);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

        l1.addView(checkbox,params);
    }
});
 
Share this answer
 
Comments
Sebassstian 8-Apr-19 14:43pm    
Hello thanks for the answer. I have a question I can't seem to get it to work. I don't have an id of linearlayout and wanted to ask from what element should I pick the id from?
Pratishkumar Kushwaha 10-Apr-19 1:24am    
You can change the checkbox id in programming throw and this id is a not presenting the all program and you can access the below method
CheckBox c1 = (CheckBox)findViewById(0);
c1.getText();//This method to access the data of dynamically data access bro
Good Buy...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900