Android setTextColor not working - android

i'm trying to colorize text in textView using:
int color = getResources().getColor(R.color.green);
streetTv.setTextColor(color);
But this not working (i got null pointer exception)
But if i tried to:
textView.setBackgroundColor(Color.parseColor("#bdbdbd"));
This works.
Where can be problem?

just use something like this:
streetTv.setTextColor(Color.BLUE); //or any other color available
i don't think you actually need the resources in your case, because you didn't mention so.

Simply just set it directly.
streetTv.setTextColor(getResources().getColor(R.color.green));
Also as Raghunandan said, did you initialize streetTv?

have you declared
streetTv = findViewById(R.id.yourtextview);
before changing background color.

Related

How to assign drawable to ImageView in acitvity using Kotlin as language

I want to set and reset the Drawable resource of ImageView from activity, but the IDE is showing cannot assign a value to Val.
val is not possible to change. You can set image in image by directly calling the ImageView in your onCreate like this:
Imageviewid.background = resources.getDrawable(R.drawable.imageid)
Alternatively, you can to change your val to a var
Try by using the following code:
image_view.setImageResource(R.drawable.my_drawable_file)
For more information please refer the following link:
https://android--code.blogspot.com/2018/03/android-kotlin-imageview-set-image.html

An equivalent of text for the imageView

Hello I would like to know what is the equivalent of this :
textView!!.text = getResources().getString(R.string.text)
But in my case I have an ImageView and I want to change it in the Kotlin code I tried :
image!!.getResources().getString(R.drawable.image)
But it does not work...Could you help me please ?
Thank you !
You can set like this,
image!!.resources.getDrawable(R.drawable.image)
You can set an image with setImageDrawable() or setImageResource()
image?.setImageResource(R.drawable.image)
Or the equivalent, but longer version:
image?.setImageDrawable(getResources().getDrawable(R.drawable.image))
(Also note the question mark instead of double exclamation marks. Now, if image is null, it just skips this line instead of throwing an exception)

Is it possible to getText() and setTextColor() at the same time in android?

I want to ask, is there any textview api that we can use to getText() and setTextColor() at the same time? i mean, if in the code, it should look something like below:
TextView.getText().equals("Hehe").setTextColor(R.color.RED);
I appreciate any answer or suggestion here. thanks a lot!
No it is not possible. The function equals returns boolean not TextView.
You should try:
if (textView.getText().equals("Hehe")) {
textView.setTextColor(R.color.RED);
}
You can use ternary operator for this:
textview.gettext().equals("Hehe") ? textview.setTextColor(R.color.RED) : textview.setTextColor(R.color.Green)
Your code doesn't make sense.
tv.getText().equals("Hehe")
returns a boolean. You cannot SetTextColor on a boolean.
and also
TextView
is the class, not the object.

Xamarin: Setting text for a TextView programmatically

This is probably a mistake or lack of comprehension on my part, but I am quite confused right now. I'm trying to set a TextView in my Xamarin Android application programmatically. Here's my code:
TextView currentCharacterName =
FindViewById(Resource.Id.characterName);
currentCharacterName.SetText("test");
Unfortunately, this does not work, as I get the error "Argument 1: cannot convert from 'string' to 'int'". After reading in the available methods for SetText, I noticed the method I'm trying to call demands a ResId. I don't really understand why I would need a ResId to modify the text of a TextView.
I tried searching on Google for answers, and I came across this answer from 2014 that had the exact same problem as I do. The solution was to use the Text() method instead to set the TextView. Unfortunately, when I try this solution, I get the error "Non-invocable member 'TextView.Text' cannot be used like a method". When I try to check the Text method description, I see "string TextView {get/set} To be added."
Does this mean there's no implementation yet to set the text of a TextView? I am really reluctant to believe this, as it baffles me that such a big framework like Xamarin wouldn't even have get/set functions for something as simple as setting the text of TextView. I feel like there's a very simple solution for my problem, but I can't seem to find it.
TextView.SetText(X) allows you to set the text from a Resource id:
currentCharacterName.SetText(Resources.Id.MyString);
You are looking for the Text property:
currentCharacterName.Text = "test";
Xamarin: TextView class
Android.Widget.TextView.Text Property
Syntax:
public String Text { get; set; }
Test this code:
TextView currentCharacterName = FindViewById<TextView>(Resource.Id.characterName);
currentCharacterName.Text = "Your Text";

Android: Get Color object from Resource

Color color = new Color(context.getResources().getColor(R.color.bus_departures_hover));
As seen, I am attemping to create a Color object from a resource. This ain't working though!
What has worked for me is:
Color c = new Color(ContextCompat.getColor(context, R.color.yourColor));
Kotlin way (API REQUIRED 26);
Color.valueOf(ContextCompat.getColor(context, R.color.color_white))
It seems there's no API constructor that receives an int http://developer.android.com/reference/android/graphics/Color.html#Color()
You may use
int color= getResources().getColor(R.color.bus_departures_hover);
And use the color value in a setter.

Categories

Resources