I have hundreds of picture in my drawable folder, is there an easier way to get this pictures using an int parameter.
Currently all I can think of is creating a loop with if statements, by creating a string = p + "i";
How can I return the drawable picture?
If you have string id for the drawable you can get drawable id with
String stringId = "p" + Integer.toString(i);
context.getResources().getIdentifier(stringId, "drawable", context.getPackageName());
context depends from where you call above code. if you call it from activity, then activity is your context and you can omit context and just use
getResources().getIdentifier(stringId, "drawable", getPackageName());
Related
I have an app that create a random image when the activity opens. There are like 30 images in the random image array. The image is generated, but what I want is to write the drawable name into a textview. It looks like this:
ImageView RImage= (ImageView) findViewById(R.id.image);
RImage.setImageResource(generator());
Drawable myDrawable = RImage.getDrawable();
TextView writeID = (TextView) findViewById(R.id.idtext);
writeID.setText(String.valueOf(generator()));
}
private int generator() {
TypedArray imgs = getResources().obtainTypedArray(R.array.list3);
int imgid = imgs.getResourceId(new Random().nextInt(imgs.length()), -1);
imgs.recycle();
return imgid;
}
Currently I receive numbers, and I cant figure out how can I convert the int to a string, or how can I use the generator to generate a string, display in the textview and then convert it to int to display it in the imageview.
I created an another activity where I can pick the image I want to see from a spinner, it simply send the selected option with string to the next activity on button press, and in the other activity i remove the .png and res/drawable/ from the string, and convert the string into an int to display the image.
Bundle extras = getIntent().getExtras();
String transportItemChosen = extras.getString("SpinnerValue");
transportItemChosen = transportItemChosen.replace(".png", "");
transportItemChosen = transportItemChosen.replace("res/drawable/", "");
String uri = ("#drawable/" + transportItemChosen);
int id = getResources().getIdentifier(uri, null, getPackageName());
ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.selectedimage);
mImageView.setImageResource(id);
So I need the generator to generate to string and then convert it to int. Thanks!
Change your code by converting your int ids into String names by Using the method
getResourceEntryName(int id);
Change the line:
writeID.setText(String.valueOf(generator()));
To Something like:
writeID.setText(getResources().getResourceEntryName(generator()));
And for Viceversa:
And if you are having a String lets assume this String you get from a textView or using above method to convert it back to an int id you do the following:
int id= getResources().getIdentifier("your_string_here", "drawable", getPackageName());
And if you are calling the code from a Fragment not an Activity remember to put getActivity() before calling getResources()or before calling getPackageName().
I have not tested the code as I am typing, but I am sure it will work (may be a typo) for more information consider visting the Documentation on Resources from this official link.
If I understand you correctly, what you need is to take a look at getResourceName(int) method - it returns resource name by its id. Also you can use getIdentifier(String,String,String) method to retrieve resource id by its name.
If you want to display name of the randomly picked image, you should save the generated resource id into the variable and get corresponding resource name for it instead of calling generator() method twice as you'll get two different ids.
I was wondering if what i want is even possible.
Trying some coding for Android but i have a little issue and i hope someone can help me with this.
I have
ImageView img1 = (ImageView) findViewById(R.id.circleselect1);
Now what is want is the Integer at the end of my resource id incremented with one
Like
ImageView img1 = (ImageView) findViewById(R.id.circleselect+integer);
I know this is not working but is there a way to accomplice what i want?
I saw your comment I have a counter, and i have 4 R.id.circleselect's like R.id.circleselect1, R.id.circleselect2, R.id.circleselect3. R.id.circleselect4 i want to get the right source if the counter hits the next intand the solution you are asking for is a bad idea.
Instead try this one out. Create an array of the ID's that you want and then use them as you want.
int[] array = new int[]{R.id.circleselect1,R.id.circleselect2,R.id.circleselect3,R.id.circleselect4};
IDs as they appear in the R class (more precisely, in the id class inside R) are just static fields.
You can access them dynamically by using
R.id.class.getDeclaredField("circleselect" + i).getInt(null);
where i is the integer at the end of the name.
This is not advised as this is very un-OOP and will break if the field does not exist.
Something Like this?
String circle = "circleselect" + integer; // or integer.toString()
int resID = getResources().getIdentifier(circle, "id", getPackageName());
ImageView img = (ImageView) findViewById(resID);
I am making an Android app of Checkers based on XML widget (like imageview).
I made a table of 8X8 of imageviews and call their id like so (i00 , i01 etc).
I have two questions:
I've tried to make a reference of the imageviews in the code like this:
ImageView img = (ImageView) findViewById(R.id.i00);
and it gives me an error:
"i00 cannot be resolved or is not a field"
Is there any way of getting id by a string?
For example I have a imageview which id is i01, can I get it by:
String str = "i01"
and then give str as a parameter for some kind of method?
1: That call is correct, check this thread which talks further about that error: “id cannot be resolved or is not a field” error
2: Something like this should work:
String str = "i02";
int id = getResources().getIdentifier(str, "id", getPackageName());
ImageView img = (ImageView) findViewById(id);
Check this link for more information findViewById with String
I want to load images each one for each gridview element but it loads me only last image for all elements of gridview. How can I solve it ? My example images are like a50.jpg
Here is my code:
for (int i = 0; i < items.size(); i++) {
String s = Integer.valueOf(items.get(position).id_of_field).toString();
int resourceId = context.getResources().getIdentifier("a" + s + i,
"drawable", context.getPackageName());
view.ivTowar.setImageResource(resourceId);
}
position is already giving a position of cell which is being viewed. Remove your loop. For code and detail about custom gridview follow this
or google for good tutorial
In a getView method you are creating View for a specific element, not for all elements at all. So the correct code shouldn't contain for loop.
EDIT The correct code should be like that, without for loop (and you are trying to parse a string to integer and then get a string again):
String s = items.get(position).id_of_field;
int resourceId = context.getResources().getIdentifier("a" + s + position,
"drawable", context.getPackageName());
view.ivTowar.setImageResource(resourceId);
I'm trying to create a listview with a static background image (i.e. not contained within the individual list cells, but fills the display and does not move when list is scrolled). I've found a couple references here and am trying to implement, but no joy. The first is;
Set a background for a listview
In particular, I'm trying this;
//set background to Drawable
listView.setBackgroundDrawable(myDrawable);
To create the "myDrawable" variable, I'm using a suggestion from;
How can I access an android drawable by a variable
Again, the particular code I'm trying is;
String icon="logo" + cnt;
int resID = getResources().getIdentifier(icon, "drawable", getPackageName());
logo.setImageResource(resID);
Here is the code I created;
ListView lv = getListView();
lv.setTextFilterEnabled(true);
String bg="football_turf_subtle";
int resID = getResources().getIdentifier(bg, "drawable", getPackageName());
myDrawable.setImageResource(resID);
lv.setBackgroundDrawable(myDrawable);
The problem I'm running into in my code is "myDrawable cannot be resolved"? If it's not obvious, I'm new to Android/Java.
Thanks in advance for any help!
Just do lv.setBackgroundResource(R.drawable.football_turf_subtle).