how to get image name from drawable on an existing imageview - java

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

Related

How to check if an imageview has a specific type of image

I am downloading a file, by pressing on an imageview with a (download) icon. After downloading the icon changes to a (checked) icon.
How can I check if the imageview is showing the checked icon to prevent another download.
My current approach which hasn't worked:
if (holder.imgFileAttachment.getDrawable() == context.getResources().getDrawable(R.drawable.downloaded)) {
//Prevent another download
}
From further research, after using getDrawable() I was able to get the data that it contains using getConstantState() from the Drawable class.
Solution:
if (imageView.getDrawable().getConstantState() == context.getResources().getDrawable(R.drawable.a).getConstantState()) {
//contains image a
} else {
// contains another image
}

Recyclerview : how to mix some post with image and no image? [duplicate]

This question already has answers here:
How to create RecyclerView with multiple view types
(23 answers)
Closed 3 years ago.
I want to display in same recycle-view which is some post have image and some post does not have images.
I can retrieve all the post with image and non-image,
but i want to change the size of the post when the user only post text(no image).
I expect the output like twitter feed..some post with image and without image have their own size.
Simple way to achieve this scenario is, All you have to do is create a view with both image and text, in recycler adapter check if image data is available make visibility of image visible else Image visibility gone.
Second Approach for this to make multiple view for RecyclerView.
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder called");
ContentItem item = mContentItems.get(position);
if(item.getName()!=null){
holder.textName.setVisibility(View.Visible);
holder.textName.setText(item.getName());
}else{
holder.textName.setVisibility(View.GONE);
}
if(item.getPreviewImageDefault()!=null){
holder.imageIcon.setVisibility(View.Visible)
Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon);
}else{
holder.imageIcon.setVisibility(View.GONE)
}
}
Another possible solution is create 2 xml layouts and use ViewType in your RecyclerView.
look this How to create RecyclerView with multiple view type?
If you want to hide the image when it is ic_launcher you could do that (suppposing that data.getImage() returns the id of the drawable as integer):
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
if(mItems!=null){
AdapterData data = mItems.get(i);
viewHolder.text.setText(data.getText());
viewHolder.image.setImageResource(data.getImage());
if(TextUtils.isEmpty(data.getText())){
viewHolder.text.setVisibility(View.GONE);
}else{
viewHolder.text.setVisibility(View.VISIBLE);
}
if(data.getImage()==R.drawable.ic_launcher){
viewHolder.image.setVisibility(View.GONE);
}else{
viewHolder.image.setVisibility(View.VISIBLE);
}
}
}
One possible solution, like some people have already said, is to hide/show the ImageView.
You could do that in the ViewHolder that you use for your RecyclerView.
class OptionalImageViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView text;
// any other views you have
public OptionalImageViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.yourImageViewIdHere);
text = itemView.findViewById(R.id.yourTextViewIdHere);
// same for any other views you have
}
public void bindView(Tweet tweet) {
// This is where the magic happens
// Note: I make the assumption that you have a class called "Tweet"
// that has a field for "text", a field for "image" (that can be
// null if there's no image), and any other necessary fields.
text.setText(tweet.getTweetText());
if (tweet.hasImage() /* function that returns whether or not there is an image */) {
image.setVisibility(View.VISIBLE);
image.setImageBitmap(tweet.getImage()); // or however you are setting the image
} else {
// else just make the image invisible
image.setVisibility(View.GONE);
}
}
}
Hopefully this gives you an idea.
RecyclerView supports different viewTypes (layouts) which is the proper way in such scenario. E.g.,
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
override fun getViewTypes(position:Int) =
if (mydata[position].hasImage) return R.layout.mylayout_with_image
else R.layout.mylayout_no_image;
override fun onCreateViewHolder(viewType:Int, parent:ViewGroup) : MyViewHolder =
// here viewType = layout id
MyViewHolder(layoutInflater.inflate(viewType, parent))
override fun onBindViewHolder(viewHolder:MyViewHolder, position:Int) {
// guaranteed viewHolder.itemView is the view you want for that position
}
}

How to get ID of images in drawable

I'm relatively new to android and I'm working on a horizontal scroll view with drag and drop functionality. Right now everything is working perfectly fine I can drag the image and drop it in the drop zone with count of failed and successful drops. For now the image that is being dragged and its shadow looks the same. What I want is that when I drag an image the shadow that appears for that image is an image from drawables that I select.
Here is the code for shadow builder:
void dragAndDropImage(int imageId)
{
// int drawableid = getResources().getIdentifier("drawable/a1", "id", getPackageName());
final ImageView drag = (ImageView)findViewById(imageId);
drag.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);//I want the drawable id here View.DragShadowBuilder(drawableid)
v.startDrag(data, shadow, null, 0);
return false;
}
});
I want to get the id of one particular image from drawables and then I can use it in View.DragShadowBuilder(id of the image from drawables) to change the image of the shadow. Any help in this regard is appreciated.
It will be something like:
R.drawable.resourcename
Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).
If that doesn't work, you can always use a context's getResources method ...
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
Where this.context is intialised as an Activity, Service or any other Context subclass.
Update:
If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?
Update 2:
The Resources class has this method:
public int getIdentifier (String name, String defType, String defPackage)
Which returns the integer of the specified resource name, type & package.
(I have been seeing this post getting more views every time I log in and I solved this a long time ago but I'm going to post the solution in hopes that it helps someone else.)
In my case I had multiple images in Drawable that I wanted to use therefore
for (int j=1; j<10; j++)
{
setImages("drawable/img" +j);
// SetPopup(drag);
}
void setImages(final String draw)
{
int id12 = getResources().getIdentifier(draw, "id", "your_package_name");
drag1.setImageResource(id12);
I just posted the part of code that helped me access different images directly from drawables and use them as drag images on multiple imageViews.
String uri = "#drawable/yourdrawable";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Can you try this?
Try this,
final ImageView drag = (ImageView)findViewById(R.id.imageId);
drag.setImageResource(R.drawable.a1);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.
The following steps shows how to get a resource ID based on the resource name:
// Name of resource
//Type
// Package of the app
int identifier = getResources().getIdentifier("pic1", "drawable", "android.demo");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(identifier);

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!!

How to check image resource of an imageview programmatically?

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.

Categories