How can I get image's name? - android

I want to make a function which debug when image's name equal "aaa". So I think that I should get image's name.
for example, there is a ImageView.
ImageView img1 = (ImageView) findViewById(R.drawable.button);
public void passMSg(){
if(name == "button"){ // "name" means image's name. = button
Toast. ~
}
I find other questions. But I can't understand and I think that is not answer which I want. Who can help me?

if your image is a file like 'aaa.jpg/png..'
try to use the APIs of String such as subString().indexOf()
and your code may be like this:
int index = name.indexOf(".");
String name = name.subString(name,0,index);

If you assigned a tag for your ImageView then you can get it like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:tag="aaa" />
ImageView v = (ImageView)findViewbyId(R.id.img);
String backgroundImageName = String.valueOf(v.getTag());

Related

Pass ImageView from one activity to another - Intent - Android

I have 170 images in the drawable folder in my android app. I have one activity displaying all of them. What is want to do is to pass the clicked imageview to another activity (Zoom_activity) where the user can zoom it and play around with it. How do I achieve it?
All the images are 500x500px. So I can't think of decoding them into Bitmaps and passing Btmaps via Intent. Please suggest a better and simple way to do it! I have already had a look at the other answers here on SO but none of them solved my problem.
Here is my code:
Activity_1.java
Intent startzoomactivity = new Intent(Activity_one.this, Zoom_Image.class);
String img_name = name.getText().toString().toLowerCase(); //name is a textview which is in refrence to the imageview.
startzoomactivity.putExtra("getimage", img_name);
startActivity(startzoomactivity);
Zoom_Activity.java
Intent startzoomactivity = getIntent();
String img_res = getIntent().getStringExtra("getimage");
String img_fin = "R.drawable."+img_res;
img.setImageResource(Integer.parseInt(img_fin));
Error: App force closes
Please help me solve this problem!
Thanks!
Integer.parseInt() only works for strings like "1" or "123" that really contain just the string representation of an Integer.
What you need is find a drawable resource by its name.
This can be done using reflection:
String name = "image_0";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier():
String name = "image_0";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
What you are trying is wrong. You can not convert "R.drawable.name" with Integer.parseInt. Integer.parseInt is expecting something like "100". You should use
getIdentifier(img_fin, "drawable", getPackageName());
to retrieve the resources id you are looking for
Use getResources().getIdentifier to load image from Drawable in ImageView as:
int img_id = getResources().getIdentifier(img_res, "drawable", getPackageName());
img.setImageResource(img_id);

How can I get the associated Id of a View as a string?

When you create a layout, you define an id for your views like this:
<ImageView
android:id="#+id/myImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/main01" />
and in your code you have:
ImageView img = (ImageView) findViewById(R.id.myImage);
now you have the object of img.
How can I get the id of img ("myImage") as a string? I need something like this:
String strId = getStringId(img);
// now strId is "myImage"
Try this.
Log.i("============ Id","::"+getResources().getResourceEntryName(img.getId()));
Output : ============ Id :: myImage
Is this what you're looking for?
String value = getResources().getString(R.id.myImage);

Displaying image based on string chosen?

I have a code rite now that just generates a random drink combination from an array, what I need to do is have a different image assigned to each choice and have it display that image.
Here is my code for a random drink:
if(Vodka.equals(true)){
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(array_city.Vodka.length);
text2.setText(array_city.Vodka[randomIndex]);
}
Say this code spits out "Smirnof" then i display a picture of the bottle, it spits out "Sky" then changes to a picture of that bottle. How would i do this without making an if statement for each option, my arrays are very long and that would be alot of if statements i was just Hoping that there is an easier way to do it?
Thanks anybody for your help! it is very much appreciated I have been stuck on this for a while.
===============================================================
#Joan
Here is what i am trying to put together using your code:
//Run option Vodka
if(Vodka.equals(true)){
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(array_city.Vodka.length);
text2.setText(array_city.Vodka[randomIndex]);
final ImageView image = (ImageView) findViewById(R.id.imageView1);
int Cimage = getResources().getIdentifier(array_city.Vodka[randomIndex], null, "com.famousmods.what.should.i.drink");
image.setImageResource(Cimage);
}
Here is what my array looks like (a smaller example):
public static final String[] Vodka = {"Absolut Vodka","Finlandia","Ketel One","Polmos Krakow","Skyy","smirnoff vodka",
"Stolichnaya","Fleischmann's","Gilbey's","Gordon's","Wolfschmitt","Five-O-Clock"};
I have put the file "smirnoff_vodka.png" into my res/drawables as an example but it doesnt work?
You can use getResources().getIdentifier("image_name", null, "your_application_package"); on your context to retrieve the image id. Then you can use this id as you would use R.id.image_name.
EDIT: It needs to be "drawable" instead of null. See below.

Android, reference things in R.drawable. using variables?

say I want to dynamically load an image file in R.drawable.* based on the value of a string
Is there a way to do this? It seems like I need to statically refer to anything in R.
Have you declared the id for the image in XML file? If you did, you can use the following method:
Let's say you have picture.png in your res/drawable folder.
In your activity, you can set your image resource in the main.xml file
<ImageView android:id="#+id/imageId" android:src="#drawable/picture"></ImageView>
In FirstActivity
//to retrieve image using id set in xml.
String imageString = "imageId"
int resID = getResources().getIdentifier(imageString , "id", "package.name");
ImageView image = (ImageView) findViewById(resID);
imageString is the dynamic name. After which you can get the identifier of the dynamic resource.
Another method, you can do this:
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
You will be able to reference your image resource and set your ImageView to it.
int drawableId = getResources().getIdentifier(drawablename, "drawable", getPackageName());
imageview.setImageResource(drawableId);
Try this. This should work.
Class res = R.string.class;
Field field = res.getField("x" + pos);
headerId = field.getInt(null);
header.setText(headerId);
this works with drawables as well, just edit the string. the header part is not required, it's just an example pulled from something I wrote a while ago.

Android : ImageView getID(); returning integer

I've set up an onTouch class to determine when one of my 40 buttons is pressed.
The problem I am facing is determining which button was pressed.
If I use:
int ID = iv.getId();
When I click on button "widgetA1"
I receive the following ID:
2131099684
I would like it to return the string ID "widgetA1"
from:game.xml
<ImageView android:layout_margin="1dip" android:id="#+id/widgetA1" android:src="#drawable/image" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
from:game.java
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView)v;
int ID = iv.getId();
String strID = new Integer(ID).toString();
Log.d(TAG,strID);
//.... etc
}
+-+-+-+-+-+-
I other wise works fine, it knows what button you are pressing. I am quite new to this Android JAVA. Let me know if you guys can help me.
Edit - TL;DR:
View v; // handle to your view
String idString = v.getResources().getResourceEntryName(v.getId()); // widgetA1
Original:
I know it's been a while since you posted, but I was dealing with a similar problem and I think I found a solution by looking at the Android source code for the View class.
I noticed that when you print a View (implicitly calling toString()), the data printed includes the ID String used in layout files (the one you want) instead of the integer returned by getId(). So I looked at the source code for View's toString() to see how Android was getting that info, and it's actually not too complicated. Try this:
View v; // handle to your view
// -- get your View --
int id = v.getId(); // get integer id of view
String idString = "no id";
if(id != View.NO_ID) { // make sure id is valid
Resources res = v.getResources(); // get resources
if(res != null)
idString = res.getResourceEntryName(id); // get id string entry
}
// do whatever you want with the string. it will
// still be "no id" if something went wrong
Log.d("ID", idString);
In the source code, Android also uses getResourcePackageName(id) and getResourceTypeName(id) to build the full string:
String idString = res.getResourcePackageName(id) + ":" + res.getResourceTypeName(id)
+ "/" + res.getResourceEntryName(id);
This results in something to the effect of android:id/widgetA1.
Hope that helps!
You cannot get that widgetA1 string...You will always get an integer. But that integer is unique to that control.
so you can do this to check, which button is pressed
int ID = iv.getId();
if(ID == R.id.widgetA1) // your R file will have all your id's in the literal form.
{
}
Xml
android:tag="buttonA"
Src
Button.getTag().toString();
Of course you can get widgetA1, simply do:
ImageView iv = (ImageView)v;
int id = iv.getId();
String idStr = getResources().getResourceName(id);
It should give you your.package.name:id/widgetA1, then do what you want with that string.
We will get the Integer value corresponds to the views id. For finding on which button you are clicking, better to use tags. you can set tag in the layout xml file and can compare which item you have clicked using.
<Button
android:id="#+id/btnTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:tag="btntestTag"/>
This is sample usage.
i think that you can use this
try it
ImageView imgView = new ImageView(this);
String imgName = "img"//in your case widgetA1
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
For the implementation you could use the hint attribute to store the button name, which is accessible. For example in the button code (.xml file) use the following line:
android:hint="buttonName"
Then in the onClicked function (.java file), write the following to get the hint:
String hint = ((Button)v).getHint().toString(); //v is the view passed onClick

Categories

Resources