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)
Related
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
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";
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.
How do ImageView get Drawable using getDrawable()? Why it different from getting image using getBaseContext().getResources().getDrawable()?
Like drawable:R.drawable.l, first set ImageView iv.setImageResource(R.drawable.l); then use iv.getDrawable() and getBaseContext().getResources().getDrawable(R.drawable.l).but why it get the different drawable? Use “== “or equals , it is not right. Thanks in advice.
iv.setImageResource(R.drawable.l) will also use getResources().getDrawable(R.drawable.l),
you invoke getDrawable(R.drawable.l) twice time, will get two different object becase of twice
new ImageDrawable().
you could use Drawable.getConstantState() to compare them. But it will also sometimes fail.
Let me explain:
I need to show the name of any building block either it is imagebutton, edittext in my textview field depending upon which of above written will be hover over by the user.
So that my textview could behave like some dynamic display plate.
Any help will highly be appreciated.
mrana..
So something that you can do. Since there it no "setText" for imageviews, you can do something like
String name = "imageview";
imageView.setTag(name);
Then in your onFocusedChangedListener call the following method
void displayInTextView(View selectedView) {
String viewName = (String) selectedView.getTag();
mDisplayText.setText(viewName);
}
Since this is a touch device, "hovering" will not be possible. One solution is to show the name on when long-press. See this solution https://stackoverflow.com/a/4433441/1227692
EDI: Thanks Frank and mrana for pointing out. I agree and take back my comment.