I am trying to make a toast appear when I click a button. I have already set the button’s on click to be the ‘cli function’. However when I press the button nothing appears. Could you please help me.
3
In xml,make sure you declared onClick in your Button attribute
onClick
Button
android:onClick="cli"
Also, in onCreate, declare your Button id
onCreate
Button button = (Button) findViewById(R.id.button);
Don’t use onClick on XML, as it’s buggy and may not work on all versions:
Buttons onClick Force closes app on Android 4.1 device
Better do it in code:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // code here } });
Don’t use onClick on XML.
Use OnClickListeners. Here is the code:
OnClickListeners
Button button = findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { cli(v); } });
The above code will help you with detecting clicks on the button. Prefer not using android:onClick in XML.
android:onClick
Click here to cancel reply.
3
Answers
In xml,make sure you declared
onClick
in yourButton
attributeAlso, in
onCreate
, declare yourButton
idDon’t use
onClick
on XML, as it’s buggy and may not work on all versions:Buttons onClick Force closes app on Android 4.1 device
Better do it in code:
Don’t use
onClick
on XML.Use
OnClickListeners
. Here is the code:The above code will help you with detecting clicks on the button. Prefer not using
android:onClick
in XML.