Change image after click on ImageView - android

everyone. After clicking on an ImageView, I would like to change it depending on what kind of drawable is currently displayed. To do this, I compare the currently set image in the ImageView with one from the drawable folder. Unfortunately, it is currently the case that only the else branch is executed. The image changes after the first click, but then it no longer switches. What am I doing wrong?
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
var draw = bookmark_site.drawable.constantState
if(draw == ContextCompat.getDrawable(this,R.drawable.ic_baseline_bookmark_filled_24)?.constantState)
{
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_outline_bookmark_border_outline_24))
}
else{
//Only this two is ever executed
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_bookmark_filled_24))
}
}

you can't base on constantState, its not desired to compare drawables as there is a different instance for every drawable, thus your if isn't true never ever. in fact there is no way for getting resource of drawable already set for ImageView. most convenient way would be to keep some boolean flag outside onClick method informing that image is switched or not. optionally you may keep this flag "inside" ImageView using setTag(...) method
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
val switched = bookmark_site.tag != null
bookmark_site.setImageResource(
if (switched) R.drawable.ic_outline_bookmark_border_outline_24
else R.drawable.ic_baseline_bookmark_filled_24
)
bookmark_site.tag = if (switched) null else "SWITCHED"
}

you should keep category or tag along with the image in the model class and then change the image on the basis of that tag or category.

Related

Not Getting Thumb with Genres - Universal Music Player

I am using UMP example provided by Google, I have not made any change in my code, even I have not tried anything out of the box, I just imported your project into my work-space and checked it on my device, and found that I am not getting Thumb with Genres (Songs by genre) and List of Genres...
Whereas I supposed to get Thumb from our JSON, here is what I have tried (but no success) -
holder.mImageView.setImageBitmap(description.getIconBitmap());
UPDATE # 1 AS PER SUGGESTED BY #NageshSusarla here
holder.mTitleView.setText(description.getTitle());
holder.mDescriptionView.setText(description.getSubtitle());
AlbumArtCache cache = AlbumArtCache.getInstance();
Bitmap art = cache.getIconImage(url);
if (art == null) {
cache.fetch(url, new AlbumArtCache.FetchListener() {
#Override
public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
if (artUrl.equals(url)) {
holder.mImageView.setImageBitmap(icon);
}
}
});
} else {
holder.mImageView.setImageBitmap(bitmap);
}
holder.mImageView.setImageBitmap(description.getIconBitmap());
and getting Cannot resolve symbol 'url'
The icon bitmap may not have been set. It's best to use the AlbumartCache to fetch the icon and then set it on the imageView. The url to be passed to AlbumArtCache.getInstance().fetch(url,..) is description.getIconUri().toString()
The reason you may not be seeing it in uAmp is because of the tint being applied to it. You can remove the tint from media_list_item.xml to try out the changes.
Aside: This is indeed by design and the icon is only shown at the bottom when a user selects the item to be played.
This is by design. To keep the navigation cleaner, we decided to not
show the MediaItem icon on the local browsing UI. Other browsing UI's
may show it, like Android Auto and Android Wear.
I think you should check it in issues of android-UniversalMusicPlayer. Take a look at comment given by mangini here.
If you want to change uAmp to show the MediaDescription.getIconUri,
set the holder.mImageView at this.

WebView appears as plain white box after the second time it's been initialised

EDIT: tl;dr: WebView appears as white box, even though I appear to be setting it up correctly, and indeed it does work the first two times, but fails subsequently)
EDIT: Video showing the problem in action...
I have the following bit of code which inflates a view (Which contains a WebView) from the xml which defines it:
private void createCard(ViewGroup cvFrame, Card card) {
//... setup vairables...
cvFrame.clearDisappearingChildren();
cvFrame.clearAnimation();
try {
View cv = LayoutInflater.from(getBaseContext()).inflate(R.layout.card_back_view,
cvFrame, true);
cv.setBackgroundDrawable(Drawable.createFromStream(mngr.open(deckName + "_Card_back.png"), deckName));
TextView suit = (TextView)cv.findViewWithTag("card_back_suit");
//...setup text view for suit, this code works fine every time...
WebView title = (WebView)cv.findViewWithTag("card_back_title");
//This WebView doesn't appear to be the one which actually appears on screen (I can change settings till I'm blue in the face, with no effect)
if (title != null) {
title.setBackgroundColor(0x00000000);
title.loadData(titleText, "text/html", "UTF-8");
} else {
Log.e("CardView", "Error can't find title WebView");
}
} catch (IOException e) {
Log.e("CardView", "Error making cards: ", e);
}
}
When this method is called as part of the onCreate method in my Activity, the WebView contains the correct code, and is suitably transparent.
I have a gesture listener which replaces the contents of the ViewGroup with different content (It animates the top card off to the left, replaces the contents of the top card with card 2, puts the top card back, then replaces card 2 with card 3)
//Gesture listener event
ViewGroup cvFrame = (ViewGroup)findViewById(R.id.firstCard);
cardLoc++
cvFrame.startAnimation(slideLeft);
(onAnimationEnd code)
public void onAnimationEnd(Animation animation) {
if (animation == slideLeft) {
ViewGroup cvFrameOldFront = (ViewGroup)findViewById(R.id.firstCard);
ViewGroup cvFrameNewFront = (ViewGroup)findViewById(R.id.secondCard);
createCard(cvFrameOldFront, cards.get((cardLoc)%cards.size()));
createCard(cvFrameNewFront, cards.get((cardLoc+1)%cards.size()));
TranslateAnimation slideBack = new TranslateAnimation(0,0,0,0);
slideBack.setDuration(1);
slideBack.setFillAfter(true);
cvFrameOldFront.startAnimation(slideBack);
}
}
When the animation has happened and I replace the contents of the cards, the TextView suit is replaced fine and the code definitely passes through the code to replace the WebView contents, but for some reason I end up with a white rectangle the size and shape of the WebView, no content, no transparency.
If I change the WebView to a TextView, it's contents is replaced fine, so it's an issue that occurs only with the WebView control :S
Can anyone tell me why / suggest a fix?
It turns out the WebView doesn't get cleared down when using the LayoutInflater to replace the contents of a ViewGroup. The other controls all seem to get removed (or at least the findViewWithTag() returns the right reference for every other control). I've just added in the line cvFrame.removeAllViews() immediately before the LayoutInflater does it's stuff and that fixed the issue.
If anyone has any better explanation for this I'll throw the points their way otherwise they will just go into the ether...
By calling findViewById, you are getting a reference on the previously loaded webview do you ?
so the loadData call that fails is the second one you make on a single webview instance.
you may want to check this :
Android WebView - 1st LoadData() works fine, subsequent calls do not update display
It appears that loadData() won't load data twice... you may want to try WebView.loadDataWithBaseUri()
Hope that helps.
I had a similar problem loading several WebViews content.
It was because of a misusing of the pauseTimers function
The situation was : the first webView weren't needed anymore, conscientiously I wanted to pause it before to release it. Calling onPause() and pauseTimers()
pauseTimers being common to any web views, it broke every use of webviews occuring after that, there were displaying only white rectangles.
Maybe its not your problem here, but it's worth checking your not calling WebView.pauseTimers() somewhere.
To confirm your answer, the source code for LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot) does in fact internally calls root.addView() which attaches the newly inflated view at the end of the root's children instead of replacing them.
So the mystery now is why did your call to findViewWithTag() is returning the expected objects for your other widgets (which would be the top, most recently created instances), but for your WebView it was returning something else.
Is it possible that there is another object in your layout XML which shares the same "card_back_title" tag?
I was also wondering why you didn't use the more common findViewById() instead, but I am not sure whether it would make a difference.

Android :getVisibility() error

i have alistview when i click the list view it goes to login screen, when login is successful it comes back to the listview with a icon ..so far its working good, problem is when the icon appear again if i click on the listview the loginscreen is comming ..i dnt want the login screen to load again once the icon is visible..i tried the following but it is giving errors
if (img.getVisibility() == 8) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
Any help is appreciated.
use
if (img.getVisibility() != View.Visible)
Dont use hardcode values.
Change your condition as:
if (img.getVisibility() == View.Visible)
EDIT : or better way you can use View.isShown() for checking View or it's child's are Visible or not
You should not really on your UI state for app logic. You would better use startActivityForResult, then in onActivityResult set a flag and use this flag in your click listener, use it also to make the icon visible or not.
You should also consider setting the flag in the shared preferences for persistence if you leave your activity.
what kind of error you face ?? can you show it to use ? you should not use a hard code value to compare the visibility use one of these View.GONE or View.INVISIBLE or VIEW.VISIBLE
like this
if(img.getVisibility != View.VISIBLE){
// do something
}
Use
if (img.getVisibility() != View.Visible)
instead of
if (img.getVisibility() == 8)
check http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29

Partial Screen Refresh in Android

I have a Activity which has 2 components
Image view
Text view
Here the image in the image view is changed using a timer. This works fine. The text is scrolling text is marquee. Individually these works fine.
My problem is when the image gets changed it also reset the marquee string.
Is there any way to avoid this?
Is there any way to refresh the display partially in android.
Edit
File imgFolder = new File(config.AppDataDir+config.MainImagesDir);
if(imgFolder.isDirectory()) {
File[] imageList = imgFolder.listFiles();
if(imageList!=null) {
if(imageList.length>0) {
if(imageList[currentImageNum].exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imageList[currentImageNum++].getAbsolutePath());
ImageHolder.setImageBitmap(myBitmap);
}
if(currentImageNum>=imageList.length)
currentImageNum =0;
}
}
}
Thanks
Normally, changing the source of an ImageView should not have any implications on other views. Most probably when changing the image you are doing something that is reflected on the TextView too.

Android: Any idea why cannot load drawable object?

There are several custom-made graphic objects (.png files) included in the project inside res/drawable map.
All elements are normally loaded and displayed in the user interface except two icons and so far haven't figured out what causes the problem.
The code which doesn't affect the user interface as it should is the following:
if (settings.isMute()) {
muteIcon.setIconImage(R.drawable.ic_volume_off_small);
} else {
muteIcon.setIconImage(R.drawable.ic_volume_small);
}
In both cases there is only ic_volume_small displayed on the screen and the Variables window in the IDE displays the following:
R.drawable.ic_volume_small = Class not
loaded : net.client.android.R$drawable
R.drawable.ic_volume_of_small = Class
not loaded :
net.client.android.R$drawable
The method (member of IconImage class) which should change the icon image is the following:
public void setIconImage(int imageFromResources) {
iconImage = BitmapFactory.decodeResource(getResources(), imageFromResources);
iconWidth = iconImage.getWidth();
iconHeight = iconImage.getHeight();
invalidate();
}
Does anyone know what could cause the described problem?
Thanks!
Assuming what you want is the drawable from the Android framework and not your own, you'd want to use:
android.R.drawable.ic_volumne_off_small
rather than
R.drawable.ic_volume_off_small
(notice the android prefix).
If you recently added file, please refresh (press F5) the drawable directory and R is going to be generated again. Once R is regenerated, you can use the newly added resource.

Categories

Resources