I have an Activity which has variable position that comes from another activity. I handle this variable with Intent like below. (Actually position is listView's position from another activity.) and values of position variable are 0 to 200
I took my Extra, then assigned it to a string than I parsed it to an Integer. These are normal and easy for me and working.
Intent i = getIntent();
String position = i.getStringExtra("POSITION");
int position_to_int = Integer.parseInt(position);
But my problem is about resources.
Simply I want to put an image to my LinearLayout dynamically . I have some country flags and they named in format like 0.gif, 1.gif, 2.gif, 3.gif ...
My idea is using position variable's value for this purpose.
but when I try to use position_to_int variable for name of flag, Eclipse returning error normally. Because R.drawble.XXX values storing in R.java
ImageView imageView = new ImageView(this);
// setting image resource
imageView.setImageResource(R.drawable.position_to_int);
How can I use position_to_int variable to show an ImageView dynamically?
You can't access it directly. You'll have to get the resource using its name:
private Drawable getDrawableResourceByName(int name) {
String packageName = getPackageName();
int resId = getResources().getIdentifier("character_you_prefixed" + String.valueOf(name), "drawable", packageName);
return getResources().getDrawable(resId);
}
Note that your resource names must begin with a letter, not a number, or else your project will not compile. Try adding a character to the beginning of each file name.
Related
Using intents to click on an element from the RecyclerView, with a small image on each element, to go to another screen/activity which will show the enlarge picture.
For example, Textview has setText and getText.
How about ImageView? They have setImageResource but I am trying to now get the image resource
There is no method that will give you ImageResource.
You can check ImageView source code. They just convert your ImageResource to Drawable and use it. So you have a getter method for Drawable imageView.getDrawable();. This is only what you can get. There is no getter for getting resource id from Drawable object.
if (mResource != 0) {
try {
d = mContext.getDrawable(mResource);
} catch (Exception e) {
Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
// Don't try again.
mResource = 0;
}
}
Solution 1 From ref
If you want pass Image to another Activity, you can do this.
When you set ImageView resource in Adapter then set a Tag on this.
imageView0.setTag(R.drawable.apple);
Now when you want get that id when user click, then only do
private int getDrawableId(ImageView iv) {
return (Integer) iv.getTag();
}
Solution 2
If you are one setting ImageView resource, then you would have this resource integer in your List?, so when user click then get resource again like
yourList.get(getAdapterPosition).getImageId();
You can programmatically set your first ImageView's tag to the resource id, and then retrieve it later with getTag() to set as the resource id of the new ImageView.
For instance:
//First set your original image tag...
ImageView original; //The original ImageVew
Integer resId = R.drawable.resource; //The original resource
original.setTag(resId); //Add your resource id to the tag
//Then, to get the resource id...
Integer resId = original.getTag();
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);
I have 10 images on /resources/drawable folder as following names;
pr1.gif, pr2.gif, ... pr10.gif
And I can easily set an image like this;
imgview1.setImageResource(R.drawable.pr5);
But I can't set it like following;
int pr = getpr(dm);
imgview1.setImageResource(R.drawable.pr + pr);
Is there a way to set it like this or should I use switch case?
You can get the id of a drawable by name using getIdentifier():
String prefix = "pr";
int suffix = getpr(dm); // I assume this returns the image number
Resources res = getResources();
int resId = res.getIdentifier(prefix + suffix, "drawable", "my.package.name");
imgview1.setImageResource(resId);
Note you can use this for any type of resource, whether it's drawable, string, array, etc. Just make sure you change the second parameter from "drawable" to the appropriate type.
I have an imageView that I want to display a little icon of the country that you are currently in. I can get the country code, but problem is I can't dynamically change the imageView resource. My image files are all lowercase (Example: country code=US, image file=us)
My code (countryCode is the current countryCode in uppercase letters):
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
Now, of course this will not work because setImageResource wants an int, so how can I do this?
One easy way to map that country name that you have to an int to be used in the setImageResource method is:
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
But you should really try to use different folders resources for the countries that you want to support.
This is how to set an image into ImageView using the setImageResource() method:
ImageView myImageView = (ImageView)v.findViewById(R.id.img_play);
// supossing to have an image called ic_play inside my drawables.
myImageView.setImageResource(R.drawable.ic_play);
you use that code
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);
You can use this code:
// Create an array that matches any country to its id (as String):
String[][] countriesId = new String[NUMBER_OF_COUNTRIES_SUPPORTED][];
// Initialize the array, where the first column will be the country's name (in uppercase) and the second column will be its id (as String):
countriesId[0] = new String[] {"US", String.valueOf(R.drawable.us)};
countriesId[1] = new String[] {"FR", String.valueOf(R.drawable.fr)};
// and so on...
// And after you get the variable "countryCode":
int i;
for(i = 0; i<countriesId.length; i++) {
if(countriesId[i][0].equals(countryCode))
break;
}
// Now "i" is the index of the country
img.setImageResource(Integer.parseInt(countriesId[i][1]));
you may try this:-
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.image_name));
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.