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

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

Related

try to make meme generate apps in android but see below error

It is Java Problem or something I need to change? when I deleted this line ImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
after Apps runs but upload image does not work. All functions are working but not uploading image.
Because you are trying to set image to ImageView class instead of ImageView instance
Your code should looks like this:
ImageView imageView = findViewById(R.id.myImageViewName);
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
Of course your ImageView should exist in your MainActivity layout.
btw copy your code to description of your post and learn about static methods
You can not apply an image to the ImageView class directly without having specified ImageView by its ID.
First, call the ImageView variable, cast it by ID, then you can apply the BitmapFactory.decodeFile(imagePath) to it like this;
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));

Can't dynamically change ImageView by code

I would like to understand, why I can't dynamically change the drawable resource from my imageView.
I have different use cases, and in function of these use cases I have to change my imageView. In my code I tried these solutions below, but I can't get my imageView refresh. It keeps the default drawable resource defined first. Here what I tried:
int imgRes = activity.getResources().getIdentifier("packageName:drawable/"+"my_drawable_name", null, null);
OR
int imgRes = activity.getResources().getIdentifier("drawable/"+"my_drawable_name", "drawable", activity.getPackageName());
imageIcon.setImageResource(imgRes);
imageIcon.invalidate();
And also:
imageIcon.setImageDrawable(null);
imageIcon.setImageDrawable(activity.getResources().getDrawable(R.drawable.my_drawable_name));
imageIcon.invalidate();
I'm in one adapter and I passed this ImageView from my activity to my adapter. And I'm doing this operation from this adapter which go the instance of my imageView.
So I tried to change this resource my_drawable_name but in both cases above, my imageView is never updated/refresh, and its image resource doesn't change. I have no error it just doesn't work. Am I doing something wrong ? What's the best practice to dynamically change a resource from an imageview by code?
No need to append drawable directory. It is the default behavior of android to get best suited drawable according to device.
Try using following code:
int imgRes = activity.getResources().getIdentifier("my_drawable_name", "drawable", activity.getPackageName());
try imageIcon.setImageResource(R.drawable.my_drawable_name);

How to use Preference.setLayoutResource() using object

I am attempting to set a Preference layout in code using setLayoutResource() . But this method requires an int id. I have a preset RelativeLayout that I want to pass as an argument, but I get an error saying Preference cannot be applied to RelativeLayout. I suspect it is because I am not passing an R.layout.id. I need this to work because I will addPreferences dynamically using instances of the same layout with different attribute setting. How can I make this work? Thank you. Sample code below.
rLay=(RelativeLayout)View.inflate(Context,R.layout.account_item, null);
nameView =(TextView) rLay.findViewById(R.id.account_name);
numberView =(TextView) rLay.findViewById(R.id.number_name);
pView = new Preference(Context);
pView.setLayoutResource(rLay); ///ERROR HAPPENS HERE///
If you use same layout with dynamically-changing data,
you can create AwesomePreference extends Preference,
then override onBindView(View) to changing TextView's text or something like that.
This is working like ListView's adapter#getView().
Also don't forget to call Preference#notifyChanged() if you changed bound data.
hope this will help.

Android setTextColor not working

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.

imageView get Drawable use getDrawble()

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.

Categories

Resources