Android ID and ImageView - java

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

Related

How to get drawable name from imageView

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.

How to add a count to resource id's

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

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

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

unable to get text of other textview in android?

I am having a dynamic table in which I have created lots of textview dynamically... actually I having one row in which I have 3 textview... in this one of them contains unique id but other's textview value repeats around the table... therefore on the click of other textview I need text from unique textview.... so please suggest some thing like getting text from the neighbor textview... line through which I am able to get text from the textview is following.
((TextView)v).getText().toString();
When creating dynamic TextViews assign a reference to the unique TextView as a tag using setTag to each of them.
TextView uniqueTextView = (TextView)findViewById(R.id.unique_id);
TextView neighbourView = new TextView();
neighbourView.setTag(uniqueTextView);
You can later get the reference back using getTag
public void onTextViewClick(TextView view) {
TextView uniqueTextView = (TextView)view.getTag();
String text = uniqueTextView.getText().toString();
}

Categories

Resources