Search code examples
androidbuttoncolorstextview

change the color of TextView when clicking on Button


I have a text box with the title of a book There is a button next. How can I change the color in the textbox when the button is clicked?


Solution

  • In the onClick method of the button you are using you can something like this:

    TextView title1 = findViewById(R.id.myTextView);
    //Your "onclick" method
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            
            title1.setTextColor(Color.MyColor); //changing text color
        }
    });
    

    Here instead of myTextView, use your TextView id. Instead of MyColor choose your color, you either have to use the built in colors(i.e. WHITE, BLACK) or add a color of your own in values\colors.xml file with your desired name and use that here.