How to check image resource of an imageview programmatically? - java

I want to check background of my imageview and do sth if it equals to some drawable in my drawable folder. How can I do that? I have try this code but no answer:
layoutRoot.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (view.getBackground() == R.drawable.grid_single_bg_selected) {
//Do sth
}
}
});

When you set the background drawable also do set a tag to the imageview
imageView.setTag(R.drawable.demo);
And then compare
if (((Integer) imageView.getTag()) == R.drawable.demo){
// Do stg
}

If you are setting the imageView programmatically, while setting, also set a tag to this imageView. Now, when you want to find out the drawable, you can do it by retrieving the tag of this imageView.

Related

Change the color of the icon in the ImageView in the java class

I showed the icons in the program via ImageView. The yellow icons should turn red when the button is pressed. I tried all the methods, but none of them worked. ...Backgroundcolor... He turned ImageView red in full square shape. I just want the icon to change color. This is very difficult‚ Please help. Thank you.
My code:
ImageView Test = (ImageView) findViewById(R.id.TestImageIcon);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here
Test.setcolor(R.id.red);
}
});
you change the tint of drawable like that:
imageView.setColorFilter(context.getResources().getColor(R.color.yourColor), PorterDuff.Mode.SRC_IN);
It can be done using setColorFilter(color).
Try to change color of icon programatically with
Test.setColorFilter(ContextCompat.getColor(YourActivity.this, R.color.red));

how to get image name from drawable on an existing imageview

like the title said, i really want to get a drawable name from my current image view
Imageview img = findViewById(R.id.IMG);
and it set with image from drawable named "img001.png"
and I have a button that will change the image to "img002.png", and when clicked again it should be changed the image again to "img001".
i use code like this
if (myimg == "img001"){
myimg= "img002"
}else if (myimg == "img002"){
myimg= "img001"}
it not work since "myimg" is null at start, so i want "myimg" to check which image is already in use.
i ve googled some info how to get resource name but all I got is how to get resource id from its name.
Get the image name like is explained here:
Android: How to get Drawable Image name
Then compare the name by using equals():
if("img001".equals(imageName)) {
...
}
I would suggest using custom tags instead of finding resource name, changing tag for that imageview and checking that in if-else condition instead of taking image names. When you apply resource to image view, assign that resource id as tag to image view.
if(imgView != null) {
imgView.setImageResource(resourceId_1);
imgView.setTag(resourceId_1);
imgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(imgView.getTag != null) {
if(imgView.getTag() = resourceId_1) {
imgView.setImageResource(resourceId_2);
imgView.setTag(resourceId_2);
} else if(imgView.getTag() = resourceId_2) {
imgView.setImageResource(resourceId_1);
imgView.setTag(resourceId_1);
}
} else {
Log.v(TAG, "ImageView tag is null");
}
}
}
}

how to set up a wallpaper app using firebase recyclerview?

I created a wallpaper app ,I'm using Firebase to upload images to a database and shown it to RecyclerView.
I can see the images that I uploaded in Firebase through the RecyclerView and I can also pass that image to a another activity.
but my problem is, I can't set the image as wallpaper, when I set the button the image is disappearing.
my code to set wallpaper:
img = (ImageView) findViewById(R.id.images);
Intent intent = getIntent();
String webUrl = intent.getStringExtra("URL");
Picasso.get().load(webUrl).into(img);
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaper = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaper.setResource(+ R.drawable.pug); //by using this code i can set a image in directory a wallpaper
//wallpaper.setResource(+ R.id.images); //i tried this one it doesn't work it just crashes the app
}catch (IOException e){
e.printStackTrace();
}
}
});
what I want is to set any image shown in the ImageView
is there any way
I'm assuming you already have the permission in place for this:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
I'm not sure what you are trying to achieve, but I am assuming you want the user to choose an image from the list and set it as wallpaper.
You can only pass a Bitmap or Resource ID to WallpaperManager. In this case, fetch the image as a Bitmap first, and then load into the ImageView.
Since you are already using Picasso, you can do this:
Picasso.with(this).load(webUrl).centerCrop().into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
wallpaper.setBitmap(bitmap);
}
}

ImageView on again click

I want my ImageView to change it's drawable resource as it is pressed. The problem occurs when ImageView is pressed for the second time.
Let me explain, if ImageView is pressed first time, I want it to change from drawable A to drawable B. If ImageView is pressed again I want it to change from drawable B to drawable A.
That pressed again part is not working..
Here's my code:
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
if (view == imageViewBiljeskeNaListiCheckMark){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
Remove this from the method....
You need to init the object once in the onCreate...
imageViewBiljeskeNaListiCheckMark = (ImageView) findViewById(R.id.imageViewBiljeskeNaListiCheckMark);
Then add a boolean variable to control the state of the view. .
public void imageViewBiljeskeNaListiCheckMarkMetoda(View view){
flag =!flag;
if (view == imageViewBiljeskeNaListiCheckMark){
if (flag) {imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
} else {
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}
}
I sugggest to use the "tag" of the view, and keep in the tag the information you need (e.g. if the view is pressed or not)
http://developer.android.com/intl/es/reference/android/view/View.html#setTag(java.lang.Object)
Can't you just use a toggle method like this?
private void toggleDrawableOnClick(){
/* now you can check to see if the set drawable is A using its id */
if(visible drawable is A){
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_nije_obavljeno);
}else{
imageViewBiljeskeNaListiCheckMark.setImageResource(R.drawable.ic_biljeske_obavljeno);
}
}
This should be easier I believe!!

in android, how would you extract the image from a imagebutton?

I have two imagebutton and I want to switch the image of each other so button a will have button b's image while button b will have button a's image. I tried to do this in my code, but it does not work
Bitmap temmp = a1.getDrawingCache();
a1.setImageBitmap(a2.getDrawingCache());
a2.setImageBitmap(temmp);
follow like this
buttona.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton ib = (ImageButton)v;
Drawable d11 = ib.getDrawable(); // this is the image u can get from that button
}
You need to enable the drawing cache before calling the getDrawingCache() with setDrawingCacheEnabled(true).

Categories