-3

checkbox should be created in red zone when button is clicked

I have tried this:

public void newcheckbox() {
  LinearLayout my_layout = (LinearLayout)findViewById(R.id.my_layout);

  CheckBox checkBox = new CheckBox(getApplicationContext());
  checkBox.setText(""+ETnewcheckbox.getText().toString());
  my_layout.addView(checkBox);
}
bica lab
  • 1
  • 1

2 Answers2

0

in your XML:

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:paddingBottom="8dp"
     android:paddingEnd="16dp"
     android:paddingStart="16dp"
     android:paddingTop="8dp">

           <Button
               android:id="@+id/btCalculator"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="click" />

           <CheckBox
               android:id="@+id/cbVar"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:visibility="gone" />

 </LinearLayout>

in your Java Class: before OnCreate:

Button btCalculator;
CheckBox cbVar;

in OnCreate:

cbVar= findViewById(R.id.cbVar);

btCalculator = findViewById(R.id.btCalculator);

btCalculator.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cbVar.setVisibility(View.VISIBLE);
        }
Shahriyar Aghajani
  • 431
  • 1
  • 6
  • 22
  • actually i want to add as many checkboxes as the user want. When user inputs Text in EditText, new checkbox with that text should be added to the view – bica lab Apr 04 '18 at 11:08
0

You should use Recycleview. Create a basic model for your checkbox like

class CheckboxModel{
 private  String text;
 private  boolean value;
...
}

you should create your CheckboxAdapter with ViewHolder and update your adapter when user click the button. adapter.setItems(list); (Do not forget to tell that notifyDataSetChanged)

ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80