I set textview background equal transparent, and now I want change it's background in code. when click on mybtn (this is a button) change textview background, how do it?
code:
Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setBackgroundColor(color.transparent);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView txt = (TextView) findViewById(R.id.txt);
txt.setBackgroundColor(??????);
Toast.makeText(getBaseContext(), "this test is ok!", Toast.LENGTH_SHORT).show();
}
});
To change the background color of TextView widget, set the background attribute with required Color value. You can specify color in rgb , argb , rrggbb , or aarrggbb formats. The background color is applied along the width and height of the TextView.
Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
Dont use setBackgroundDrawable
but use::
@Override
public void onClick(View v) {
TextView txt = (TextView) findViewById(R.id.txt);
txt.setBackgroundResource(R.drawable.textview_logo);
Toast.makeText(getBaseContext(), "this test is ok!", Toast.LENGTH_SHORT).show();
}
});
Make sure textview_logo
are stay in drawable folder
for set background :
txt.setBackgroundColor(Color.RED);
You can set any color using this:
txt.setBackgroundColor(Color.parseColor("#BABABA")); // set any custom color as background color
or
txt.setBackgroundColor(Color.RED); // set default RED color as background color
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With