imageView get Drawable use getDrawble() - android

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.

Related

android kotlin click listener loses its "click methods" after some time

Hi I'm implementing click listeners in the following way but after some time the methods and variables inside the listener's closure get the wrong values or something. Let me explain the implementation of the listener a little better a for loop creates the listener for a set of image views then later in the program the for loop is called a second time and it resets the listener methods and variables to different values. Everything works great for about 30 minutes but then for some reason, the listener's methods and variables start having the wrong values. Has anybody ever heard of this behavior or can tell me where I've gone wrong with the code? Keep in mind that the listener I'm about to paste here is just a small piece of a 1014 line class. I'm hoping somebody can spot How I'm implementing the listener wrongly and can give me some advice on how to "reset" the listener so that it's variables and values stay over time. Hopefully you can read the code without putting it in an editor but feel free to copy it for readability's sake Here is the code for the image view listener with comments.
//image views are held in an array
//set an image view in its imageview container
imgArr0[slotId1].invalidate()
imgArr0[slotId1].setImageDrawable(null)
//drw is not defined in this example
imgArr0[slotId1].setImageDrawable(drw)
/*if video or image id is set to image then set a listener for the image
*/
/*slotId1 is not defined in this example but it is simply a counter to iterate over the ImageView array
*/
if (videoOrImageId0[slotId1] == "image") {
//null any listeners that might be attached to the image view
imgArr0[slotId1].setOnClickListener(null)
//set or reset the listener
imgArr0[slotId1].setOnClickListener() {
`enter code here`//if the current config is portrait then set a new image image
if (currentConfig0 == "portrait0") {
act0.lrgImage0.invalidate()
act0.lrgImage0.setImageDrawable(null)
/*drw is not defined in this example but works fine in the production script
*/
act0.lrgImage0.setImageDrawable(drw)
}
--calmchess
ccc tv application with problem.
(https://i.stack.imgur.com/PjdbN.jpg)![enter image description here](https://i.stack.imgur.com/FaMnc.
I was able to partially solve this question by destroying all the image views and their associated click listeners then rebuilding those... However I don't consider this issue completely solved so if anybody can provide a better solution I'd love to hear it because rebuilding the images every few minutes has to be using a lot of unnecessary hardware resources.
--calmchess

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);

Android: reference to a dynamic View is no longer valid after screen rotation

Context:
i have n dynamically created ImageView(s) in an activity.
when an imageview is touched:
1) an IV object reference is stored vis the onTouch event.
2) a dialog opens; allowing the user to edit the image.
when the user has finished making edits, the changes are applied to the object which we have a stored reference.
however, if the user rotates the device while the Dialog is open, the activity is recreated; and the reference to the selected ImageView is no longer valid.
Issue:
So, the problem is that the recreated ImageView, produced from the rotation event, has a different reference to the reference that i have stored, meaning that i have no idea which ImageView to apply my edits to..
This seems like a common issue that could easily be solved, but i don't know how..
because the ImageView(s) are dynamically created, and there could be n quantity, i can't find view by ID.
i can't set a tag, as it is lost when the IV is recreated.
i read something about retainInstanceState, but this appears to be for fragments; not activities
Assign ids to your generated ImageViews. When dialog is shown you pass this id to dialog, when dialog finishes it returns your values and this id, then you just get ImageView by id, be it a new one, or old doesn't matter since the couple 'id - iv at position' are the same no matter how many times you recreate activities.
As example:
for (int i=0;i<20;i++) {
ImageView iv = new ImageView(context);
iv.setId(1337 + i); // Spice things up a little :D
addView(iv);
}
Then just pass required id iv.getId() to dialog, and when dialog finishes get iv by calling findViewById(id);
The easiest way to fix this is to add android:configChanges="orientation|<whatever other flags you need>" to your <activity> in AnroidManifest.xml, so the activity won't be recreated when orientation changes. However, using this method is officially discouraged:
Note: Using this attribute should be avoided and used only as a last resort
Another way would be to store in the dialog not the reference to ImageView, but its id instead.

TextView.setMaxLines not working?

In my app I have a screen where I display some text and then a photo. The text is variable in length (sometimes none at all, sometimes a lot), so I wanted to have it set up so the text never takes up more than a few lines (but can be scrolled) leaving enough room for the image below.
My view component for this part is created programatically, and I've adjusted the code to have the following (currently in my text-setting method, but the same thing happens if it's in the initial view-create code)
public void SetDescription(String description)
{
mTxtDescription.setText(Html.fromHtml(description));
mTxtDescription.setClickable(true);
mTxtDescription.setMaxLines(5);
mTxtDescription.setLines(5); //this makes no difference either!
mTxtDescription.setSingleLine(false);
mTxtDescription.setScrollbarFadingEnabled(true);
mTxtDescription.setScrollBarStyle(VERTICAL);
mTxtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
mTxtDescription.invalidate(); //adding this made no difference...
}
However it doesn't work- long text still fills the whole screen and the image has vanished due to being pushed down to a height of 0. How can I get the text to never be more than 5 lines?
Try removing the call to setSingleLine. And use setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE). It'd also put this call before the setMaxLines and setLines call to be sure.
Note: setLines overrides the settings of setMaxLines and setMinLines.
The TextView has many issues surrounding the various calls to how it should display multiple, ellipses, etc.
The setSingleLine(false) seemes to reset the setMaxLines command. Try to move the setSingleLine command before the setText. That worked for me.
The below code is working fine for me
txt = (TextView)findViewById(R.id.textview);
txt.setMaxLines(5);
txt.setMovementMethod(new ScrollingMovementMethod());
txt.setScrollContainer(true);
txt.setText("Example Text");
txt.setTextColor(Color.WHITE);
txt.setScrollbarFadingEnabled(true);
in xml inside textview
android:scrollbars="vertical"

Categories

Resources