B brentc Newbie Jan 15, 2022 #1 Can't change the button background and onClick to execute the Java code doesn't work
B brentc Newbie Jan 15, 2022 #2 I got the onClick to work. I didn't have the word android: to the left of onClick - silly me. But I still can't change the background color.
I got the onClick to work. I didn't have the word android: to the left of onClick - silly me. But I still can't change the background color.
GameTheory Android Expert Jan 18, 2022 #3 Using onClick in xml has been deprecated. You should be creating your buttons in Java. Something like this in your onCreate. Java: Button btn = findViewById(R.id.myBtn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do some stuff } }); Add the id to your button xml layout instead of having onClick. XML: android:id="@+id/myBtn" As for the background color, I don't remember too well since I replaced buttons with the CardView which allows you to do fancier stuff. In CardView you choose your background like so. XML: card_view:cardBackgroundColor="?attr/colorPrimary" or... XML: card_view:cardBackgroundColor="#FFFFFF" So with Buttons it should be something similar. XML: android:BackgroundColor="?attr/colorPrimary" or... XML: android:BackgroundColor="#FFFFFF" Cheers.
Using onClick in xml has been deprecated. You should be creating your buttons in Java. Something like this in your onCreate. Java: Button btn = findViewById(R.id.myBtn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do some stuff } }); Add the id to your button xml layout instead of having onClick. XML: android:id="@+id/myBtn" As for the background color, I don't remember too well since I replaced buttons with the CardView which allows you to do fancier stuff. In CardView you choose your background like so. XML: card_view:cardBackgroundColor="?attr/colorPrimary" or... XML: card_view:cardBackgroundColor="#FFFFFF" So with Buttons it should be something similar. XML: android:BackgroundColor="?attr/colorPrimary" or... XML: android:BackgroundColor="#FFFFFF" Cheers.