How to get drawable name from imageView - java

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.

Related

Easier way to get image in drawable using parameter?

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());

Android ID and ImageView

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

Getting drawable from id returns null or empty image depending on method

Note I'm referencing to some comments in the description of my problem but the comments are outside the code container, you have to scroll right to see the comments.
So, I'm trying to get a drawable (image) from R.drawable but has bumped into some issues. I started with attempt 1 (check code comment) and got an image with all bounds=0. But then i read here that getDrawable() might return a false scaling of the image so i followed the notice and tried attempt 2 (check code comment again) but in this case the drawable was null.
Here's the code:
private void setCurrentImage(){
TextView text = (TextView)profileView.findViewById(R.id.profile_image);
//mImage = "flower";
int id = R.drawable.flower;
if(mImage.equals("flower"))
id = R.drawable.flower;
if(mImage.equals("event_splash"))
id = R.drawable.event_splash;
if(mImage.equals("football"))
id = R.drawable.football;
if(mImage.equals("foxhead"))
id = R.drawable.foxhead;
if(mImage.equals("computer"))
id = R.drawable.computer;
//Drawable image = getResources().getDrawable(R.drawable.foxhead); //attempt 1
TypedArray attr = getActivity().obtainStyledAttributes(new int[]{id}); //attempt 2
Drawable image = attr.getDrawable(0); //attempt 2
Log.d("bounds", image.getBounds().toString());
text.setCompoundDrawables(image, null, null, null);
}
As you can see on the commented line mImage = "flower" I tried with 100% certainty the mImage was a valid, still didn't work.
from the TextView doc :
public void setCompoundDrawables (Drawable left, Drawable top,
Drawable right, Drawable bottom)
Sets the Drawables (if any) to appear to the left
of, above, to the right of, and below the text. Use null if you do not
want a Drawable there. The Drawables must already have had
setBounds(Rect) called.
So once you pulled your drawable, you have to call setBounds upon it to specify the bounds where you want your image to appear

android - Switching strings in a textView dynamically

I am trying to switch the android:text of a textview with various strings that are to be dynamically retrieved from my strings.xml. When the user presses the button on the grid, it will return a unique identifier. I want to use this identifier to dynamically load a string into the Pronoun textView.
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView Pronoun = (TextView) findViewById(R.id.Prounoun);
chosen = ((TextView) v).getText();
//Inserts the identifier of what button was pressed into the local string chosen".
Pronoun.setText("R.string.Pronoun_" + chosen);
}
}
When the button 'a' is pressed, 'chosen' holds the letter 'a', when joining this to the string naming format "Pronoun_", resulting in "Pronoun_a". The intention was to call upon the string at this location. In reality, the text is literally "R.string.Pronoun_a" instead of retrieving the actual string content of Pronoun_a. If the button 'b' is pressed, I want the string content of 'Pronoun_b' to show in the textView. I am new to java, merely trying to recreate a technique I learned in another language. Is there a way to do this?
Use this (with your actual package name):
String resKey = "your.apps.package:string/Pronoun_"+chosen;
int resId = getResources().getIdentifier(resKey, null, null);
Pronoun.setText(resId);
Also see the API Docs.
Note: As AND_DEV already mentioned, you'll have to use ActivityName.this.getResources().
instead of
Pronoun.setText("R.string.Pronoun_" + chosen);
try below line
Pronoun.setText(ActivityName.this.getString(R.string.Pronoun_) ++ chosen);
it will work in activity context, but in onclick i think you should use ActivityName.this. you can also use context variable if you already created. here ActivityName is you activity name of which ur code is written .

Applying a background image to listview with textview

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).

Categories

Resources