lundi 29 juin 2015

create CheckBox on button press

i am trying to create a checkbox, when a button is clicked, but i always get this error:

Error:(30, 52) error: incompatible types: <anonymous OnClickListener> cannot be converted to Context

this is how i am trying it

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button myButton = (Button) findViewById(R.id.myButton);

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

            LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);

            CheckBox myCheckBox = new CheckBox(this);
            myCheckBox.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));

            myLayout.addView(myCheckBox);
        }
    });

}

please help! thanks




Aucun commentaire:

Enregistrer un commentaire