How do I check what the source is in an imageview? - android

I need to check on a button click if there is a drawing associated with the list or if its "invisible". I am doing the check by seeing if the source of the drawing is #drawable/invisible or something else. If its something else then change it to invisible, if invisible then change it to a color etc...
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View listview, int position,
long arg3) {
ImageView pincolor=(ImageView) listview.findViewById(R.id.ivimtrackingpin);
// here is where I want to check what the "source" is
if(pincolor.getResources().equals(R.drawable.invisible)
//do stuff
else
pincolor.setImageresource(R.drawable.invisible)
}
});
I don't want to just do setvisibility =invisible because there are different colors that each list can be and the switch function will go inside the if part. What method do I use to determine the imageviews source?
Thanks

Maybe you can make use of the ImageView's getDrawable() Method.
/**
* Return the view's drawable, or null if no drawable has been assigned.
*/
public Drawable getDrawable ()
like this:
ImageView pincolor=(ImageView) listview.findViewById(R.id.ivimtrackingpin);
Drawable drawable = pincolor.getDrawable();
if(drawable != null) {
//do stuff with the drawable
} else {
pincolor.setImageresource(R.drawable.invisible)
}

This will tell you if the view is invisible:
pincolor.getVisibility()==View.INVISIBLE

Related

RecyclerView uses wrong values

So, I use a RecyclerView for displaying a grid. In the onBindViewHolder method of its adapter I update the view content. I put a part of the update into another thread to keep the UI alive.
#Override
public void onBindViewHolder (final MovieHolder holder, final int position) {
// Blah, blah. Set movie details. Everything is fine.
// Generate a palette or load from the memory and adjust the background color of the view.
new Thread(new Runnable() {
#Override
public void run () {
Palette palette = getPalette();
final Palette.Swatch bestSwatch = findBestSwatch(palette)
if (bestSwatch != null) {
activity.runOnUiThread(new Runnable(
#Override
public void run() {
holder.layout.setBackgroundColor(bestSwatch.getRgb());
}
));
}
}
}).start();
}
It works well, except when no bestSwatch was found. Then for the first time, it displays a correct background color and when I scroll down and back it will have a random color from one of the views.
Why does RecyclerView changes the color of my view when I don't update it?
RecyclerView reuses its rows, the ViewHolders, hence its name. So your problem is that a recycled view has the background color it was set to at another recent position.
Solution to this is simple...
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
Palette.Swatch swatch = palette.getDarkVibrantSwatch();
if (swatch == null)
swatch = new Palette.Swatch(ContextCompat.getColor(context, R.color.fallback_background), 4);
holder.layout.setBackgroundColor(swatch.getRgb());
}
});
So, you always set a BackgroundColor. When there's no swatch generated, you create one using a fallback layout background color you define at colors.xml

Strange behaviour in customView with onClick event

I am using this class to draw TriangleShapeView over ImageView that changes its color and drawable image upon user click event.
in RecyclerView onBindViewHolder method, i check against
feedModel.isSubscribed() then set TriangleShapeView color and drawable image accordingly:
public void onBindViewHolder(FeedViewHolder holder, final int position) {
final FeedModel feedModel =this.feedCollection.get(position);
if (feedModel.isSubscribed()) {
holder.mTrView.setBackgroundColor(Color.RED);
holder.mTrView.setDrawable(holder.mTrView.getContext().getResources().getDrawable(R.drawable.ic_check));
} else {
holder.mTrView.setBackgroundColor(Color.BLACK);
holder.mTrView.setDrawable(holder.mTrView.getContext().getResources().getDrawable(R.drawable.ic_plus));
}
in setOnClickListener:
holder.itemView.setOnClickListener(v -> {
if (FeedAdapter.this.onItemClieckListener != null){
FeedAdapter.this.onItemClieckListener.onFeedItemClicked(feedModel);
if (feedModel.isSubscribed()) {
feedModel.setIsSubscribed(false);
notifyItemChanged(position);
} else {
feedModel.setIsSubscribed(true);
notifyItemChanged(position);
}
}
});
this works fine when items get loaded for first time but when user clickes:
- 1st & 2nd time: drawable image got changes as desired but the color
remains same.
- 3rd time both drawable image and color gets changes
i am using the following xml layout to inflate this custom view:
<cardView
<RelativeLayout
....
<com.xxx.TriangleShapeView
android:id="#+id/trView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:elevation="15dp"
TriangleShapeView:imgPadding="5dp"
TriangleShapeView:triangleBackground="#color/cardview_dark_background"
TriangleShapeView:img="#drawable/ic_plus"/>
i believe, the FeedModel gets updated once OnClick is called and checks are fine in onBindViewHoldr method. so i think the problem is in the class mentioned in the above link
What i am looking to achieve is:
if feedModel.isSubscribed then change the color to red and drawable to check-sign icon. else, keep the initial values as in the layout xml.
also react upon onClick and change the color and image
I think you're right and the error in this method.
public void setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
invalidate();
}
the paint color get assigned in the constructor and onDraw method.
try to add to this method before invalidate() the line with paint.setColor(backgroundColor);

Gridview image issue (Edge image not using CustomImageView)

I have an odd issue. Basically I use a custom image view which I overwrite the draw method. The draw method just writes the tag of the image object.
I have a grid of all these custom image views. The issue stems from the edge image that it renders. Basically the image does not display any text (just the image) but when I scroll out and back again the text reappears for the image. I am just asking whether there is any way to actually render the text of the edge image and not have to scroll off screen and back.
Here is the getView code for my image adaptor:
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
CustomImageView tmp;
if (arg1 == null) {
tmp = new CustomImageView("", cx, true);
} else {
tmp = (CustomImageView) arg1;
}
int Dimens = (int) getResources().getDimension(R.dimen.gridParam);
tmp.setPadding(3, 3, 3, 3);
tmp.setLayoutParams(new GridView.LayoutParams(Dimens, Dimens));
tmp.setTag("TESTIMG" + arg0);
tmp.setImageResource(mThumbIds[arg0]);
return tmp;
}
}
And here is the code for my gridview, its just a standard creation.
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdaptor(this));
Hopefully you can help me out. Just curious as to why this issue is occuring, maybe I left out something.
Any help would be appreciated. Thanks!

How to highlight item currently selected in a gallery?

I've come with a problem. I need to highlight the selected item within a Gallery. I've tried changing the appearance of the selected view through this method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
The second parameter of that view is the current selected view, and I'm trying in this case, increase the size of the text. However this doesn't work, not even if I call invalidate in the selected item or in the entire Gallery.
This is the code I use to change the textview text size
TextView textview = (TextView)view;
textview.setTextSize(50, TypedValue.COMPLEX_UNIT_SP);
textview.invalidate();
Do you know how to do this? Thanks
Your implementation works, but it does not return the text size to normal once the item becomes unselected - the text stays larger size.
This should fix it:
private View lastview;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (lastview != null) lastview.setBackgroundColor(0xFFFFFFFF);
lastview = arg1;
arg1.setBackgroundColor(0xFFFF0000);
}
You can set text size instead of color, or do whatever you want to the style of it.
Try StateListDrawable - looks apt for your situation.
UPDATE: You have reversed the parameters in setTextSize. It should be:
textview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);

Android: Animation in Gallery View?

When I use the Gallery widget, how do I get the images to say scale up & glow on being selected and scaled down & un-glow on being unselected?
All tutorials I've seen have this effect but I'm not able to see it...
Is there some kind of an animation that I have to attach to the Gallery?
Hope this is helpful. I manage to "simulate" the shrink/grow solution with the Gallery widget. Since they removed the getScale(), things get a little bit complicated. I think that this it's not the best solution at all, but at least I can live with it.
What I have found is that Gallery manages the focus EXTREMELY BAD. So, the first approach was to add a focus change listener on the ImageView displayed, but no luck there. Focus is a MESS there... in terms that, the selected image it's not the currently focused view. I have sent a mail to android-developers mailing list about some error on API doc (regarding the focusSearch()method and some focus contants).
Here's my solution to this problem:
Build an animation resource to 'grow' the image:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.10"
android:fromYScale="1.0"
android:toYScale="1.10"
android:duration="300"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fillAfter="false"/>
If you don't get what that means then you should proceed to read this
That will be our 'grow' effect, and you will need to save it in: res/anim/grow.xml or whatever name it suites you (but always in res/anim dir).
You can follow the resource guide from here to create a Gallery view. The ImageAdapter builds an ImageView every time that the Gallery object calls getView(). One workaround you could implement is adding a line to the getView() method that identifies a View with a position, this way:
...
i.setId(position);
...
With that line added to the getView() method of the ImageAdpater object, you can then unequivocally identify that view within a listener, for instance:
g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected (AdapterView<?> parent, View v, int position, long id) {
Animation grow = AnimationUtils.loadAnimation(YourActivity.this, R.anim.grow);
View sideView = parent.findViewById(position - 1);
if (sideView != null)
((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(150, 100));
sideView = parent.findViewById(position + 1);
if (sideView != null)
((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(150, 100));
v.startAnimation(grow);
v.setLayoutParams(new Gallery.LayoutParams(170, 150));
}
public void onNothingSelected (AdapterView<?> parent) {
System.out.println("NOTHING SELECTED");
}
});
NOTE: You may notice that all the values from animation and from layout parameters has been choosen by me at hand. This is because i'm not going to clean code for you. And, this is just a workaround the BAD focus issue with this widget or the android view system. If focus were OK, then, all you need to do is set a focus change listener that makes the gros/shrink when it got focus/unfocused.
I hope this may help you to find a way around for your problem,
Regards,
New EDIT: This is the listener I have set, I also added the line i.clearAnimation() in the getView() method:
private class SelectListener implements AdapterView.OnItemSelectedListener {
private Animation grow;
private int last;
public SelectListener() {
grow = AnimationUtils.loadAnimation(RouteGallery.this, R.anim.grow);
last = 0;
}
public void onItemSelected (AdapterView<?> parent, View v, int position, long id) {
View sideView = parent.findViewById(last);
if (sideView != null && last != position)
sideView.clearAnimation();
v.startAnimation(grow);
last = position;
}
public void onNothingSelected (AdapterView<?> parent) {
}
}
You need to use an ImageSwitcher. The ImageSwitcher has methods for setting the in and out animations (when image is selected and deselected, or selected and replaced).
The following link has a good tutorial on how to use it in conjunction with the Gallery.
I implemented a similar animation like this:
final Animation shrink = AnimationUtils.loadAnimation(activity, R.anim.shrink);
shrink.setFillAfter(true);
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// This iterates through the views which are currently displayed on screen.
for (int k=0; k<gallery.getChildCount(); k++){
// Do whatever else you want, here.
// This is how you get a particular element of a view
ImageView background = (ImageView) gallery.getChildAt(k).findViewById(R.id.menu_item_background);
//clear animation
gallery.getChildAt(k).clearAnimation();
}
// Scale the selected one
view.startAnimation(shrink);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});

Categories

Resources