android TextView: setting the background color dynamically doesn't work
Use et.setBackgroundResource(R.color.white);
dynamically setting textview background color
yourView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.your_color));
or
yourView.setBackgroundColor(getResources().getColor(R.color.your_color));
or
yourView.setBackgroundColor(Color.parseColor("#ffffff"));
I believe what you want is in the onClickListener. You have already the view, just change that view clicked. For example:
@Override
public void onClick(View v) {
ViewGroup row = (ViewGroup) v.getParent();
for (int itemPos = 0; itemPos < row.getChildCount(); itemPos++) {
View view = row.getChildAt(itemPos);
if (view instanceof TextView) {
textView = (TextView) view;
textView.setBackgroundColor(Color.LTGRAY);
}
}
Toast.makeText(getContext(), "Item on aisle select/deselect", Toast.LENGTH_SHORT).show();
}
How to change TextView's background color dynamically?
You can change TextView background color in many ways like:
textView.setBackgroundColor(Color.parseColor("#f44336"));
or
textView.setBackgroundColor(Color.RED);
or
textView.setBackgroundColor(Color.rgb(255, 0, 0));
or
textView.setBackgroundColor(getColor(R.color.red_color));
and many other ways too...
Edit:
If you want to change your TextView background color that was defined in your drawable file, do it like this:
GradientDrawable:
GradientDrawable tvBackground = (GradientDrawable) textView.getBackground();
tvBackground.setColor(Color.parseColor("#f44336"));
StateListDrawable:
StateListDrawable tvBackground = (StateListDrawable) textView.getBackground();
tvBackground.setColorFilter(Color.parseColor("#f44336"), PorterDuff.Mode.SRC_ATOP);
But if you don't want to set a color filter, you can get the drawable of each state separately by following the answer in this link.
Add background color to TextView in Android studio
You can use setBackgroundResource
for setting up the background color. Here is a snippet.
TextView textView = new TextView(activity);
textView.setBackgroundResource(R.color.red);
Refer to this official doc
TextView background color not changing in android (kotlin) on radio button selection
if (checked) {
textView.setBackgroundColor(Color.parseColor("#FFFFFF"))
}
try to use it as
How to dynamically set background color in android app?
The ColorDrawable would be the solution for your case. Assume that the colorPickerColor is an integer color code to draw, the background may set with the following code:
mainLayout.setBackgroundDrawable(new ColorDrawable(colorPickerColor));
and if the colorPickerColor is a color resource id,
mainLayout.setBackgroundDrawable(new ColorDrawable(getResources().getColor(colorPickerColor)));
Kindly note that the setBackgroundDrawable method is deprecated, you may call setBackground instead.
Change the color in dynamic textview for every click in android
You can iterate through all the child in the layout and set the color as white and then the selected color as orange like this in below example.
LinearLayout layout;
private void setAllTextColorAsWhite() {
if(layout == null) {
return;
}
int childCount = layout.getChildCount();
for (int i = 0; i < childCount; i++) {
TextView textView = (TextView) layout.getChildAt(i);
textView.setTextColor(getResources().getColor(R.color.white));
}
}
public void setTextViews() {
layout = (LinearLayout) getView().findViewById(R.id.pagination);
layout.removeAllViews();
for (int i = 1; i <= n; i++) {
final TextView mPageNumber = new TextView(getActivity());
mPageNumber.setText("" + i);
mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
mPageNumber.setPadding(60, 30, 60, 30);
final int id_ = mPageNumber.getId();
layout.setBackgroundResource(R.color.colorPrimary);
layout.addView(mPageNumber);
mPageNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (toastMessage!= null) {
toastMessage.cancel();
}
toastMessage = Toast.makeText(getActivity().getApplicationContext(), "Button with id =" + id_ +
" is clicked",Toast.LENGTH_SHORT);
current = id_;
toastMessage.show();
setAllTextColorAsWhite();
mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
}
});
}
}
Android: How to change the color of the drawable background of TextView dynamically in the java code
You can do this like this:
Drawable drawable = getResources().getDrawable(R.drawable.bg_rounded_solid);
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
yourTextView.setBackground(drawable);
This should work (not tested, just from my mind)!
Just reset the drawable once you change the color of it.
Related Topics
Position of Dialogfragment in Android
Android How to Programmatically Hide Launcher Icon
Difference Between a Clickable Imageview and Imagebutton
Android Gridview Draw Dividers
Offline Mode for Android App Using the Google Maps API
Rate Google Play Application Directly in App
Android- How to Convert Android.Net.Uri Object to Java.Net.Uri Object
How to Change the Font Size in a Whole Application Programmatically, Android
How to Print Stacktrace for an Exception Android
How to Build a Native (Command Line) Executable to Run on Android
Android: Adb Pull File on Desktop
Parcelable Encountered Ioexception Writing Serializable Object Getactivity()
How to Use Google Login API with Cordova/Phonegap