简体   繁体   中英

how to make button color variable in xml/java (android studio)

I am very very sorry if this question is too broad to answer in specific code, but i am just starting out and want to know how to tackle a specific function. I have been trying to set a buttons xml color as a variable the same way that i am setting it below, where i set the button id in my layout to the variable Team1button then use that to set its text.

Team1button = findViewById(R.id.button);

String Teamname = "tree";

Team1button.setText(Teamname);

Essentially i want to do the same thing as .setText, but with color because i am wanting color to change through other events happening in my code. In the end i want it to be the user choosing the color of each button, but that is a different question i will answer myself after being able to make the buttons color changeable in either xml or java. If you know what type of function i would need to do in either my layout files or my .java files it would be greatly appreciated if you gave me any input on how to do this. This is for school project.

Check out Button class documentation : https://developer.android.com/reference/android/widget/Button.html

Basically, if I understend you correctly you want to call :

button.setBackgroundColor(int colorID);

eg

button.setBackgroundColor(Color.RED);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM