I'm currently using something like: TextView.SetBackgroundColor(Color.WHITE); in my java code. I'd like to be able to add some transparancy to the textview through the java... This is easy to do in the XML via #AARRGGBB format, but I have not found a way to accomplish this programmatically.
TextView.SetBackgroundColor(Color.argb(a_int, r_int, g_int, b_int));
Or:
TextView.SetBackgroundColor(Color.parseColor("#AARRGGBB"));
You can use
TextView.SetBackgroundColor(Color.parseColor("#AARRGGBB"));
Related
I tried this code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.setBackgroundTintList(context.getResources().getColorStateList(Color.parseColor("#ff8800")));
But I'm getting an error: android.content.res.Resources$NotFoundException
I'm getting the color hex from external source so I can't embed it in colors.xml.
Also I want to change the tint, not the background so setBackground is not an option.
I figured I can't use getColorStateList() so I searched for another way to do it.
At the end I was able to set color tint using the following code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
This worked as if I changed the backgroundTint property in the xml file, so it's perfect for my problem.
I was able to manage using the following line. change it to your circumstances.
myView.getBackground().setTint(currentView.getResources().getColor(R.color.colorAccent));
For Kotlin ,
I modified #Krestek answer :
someLayout.background.setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP)
You can't do this like that because getColorStateList method expect int id of resource, and you are passing RGB color int. You should create color state list following this link
and then set it like this:
.getColorStateList(R.color.your_xml_name)
i want to set the below xml code programmatically. i know about textView.setBackgroundResource(R.drawable...) but i am not able to set textView background to #android:drawable/dialog_holo_light_frame. Please suggest me suitable method.
android:background="#android:drawable/dialog_holo_light_frame"
Try to use the pattern of the code below:
textView.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
If you want to use and of the standard android resources you have to use android.R. folder rather than just R. . R. is for your own project. So do it like below -
textView.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
You can do like below:
yourTextView.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
What I would like to achieve is an effect that looks like this with a TextView:
Basically having a background, but keeping the space between the lines. The only solution I came up with was using one TextView for each line of text, but I would prefer a cleaner one using only one multiline TextView.
Any ideas?
Use spanned text for each line. Read the Spannable API.
please refer to this answer here as it describes how to implement spacing between multiple lines in a TextView , using the following properties :
android:lineSpacingMultiplier
android:lineSpacingExtra
Hope that Helps .
Regards
What you can do is use mark tag in html to textview.
myTextView.setText(Html.fromHtml("<mark>heading highlighted</mark>"));
I've got an ImageView and programmatically I can change its color using imageView.setColorFilter(Color.RED).
There is something similar using xml??
Yes, it's android:tint. Here you have more info.
I have a custom component that I want to give the same colors as a TextView.
That is, I don't want to copy its colors, I want to get the default background and foreground colors, if there's such a concept on android.
[Edit]
The following seems to yield the text color of my TextView. But is it just luck? It's not intuitive to me that a default TextView would use android.R.attr.textColorSecondary? And why does not resolveAttribute return the color directly?
TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.textColorSecondary, tv, true);
Color holyColor = getResources().getColor(tv.resourceId);
[Edit]
I found the source code of TextView at android.git.kernel.org, but it seemed to contain a lot of referrences to com.android.internal.R, which I don't think I should use in my own code. I'm currently looking for some kind of evidence that TextView uses android.R.attr.textColorSecondary.
[Edit]
I found some kind of evidence at developer.android.com, in styles.xml that TextView uses android.R.attr.textAppearanceSmall. textAppearanceSmall is documented to default to "secondary text color".
I guess I was lucky after all, but I still don't like that little code snippet of mine.
What you're looking for are attributes. Attributes link widgets to styles. For example, android:background is what you'd set for a particular view, but there's attributes like android:panelBackground and android:windowBackground that you can use override to affect the system as a whole.
You should look through R.attr and then link to those attributes in your widget. There should be a number of them that are linked to TextView; it would make sense to download the Android source code and see which attributes are used.