How to add a count to resource id's - java

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

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.

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

ask with getView method in gridView

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

How to calculate string font width in pixels?

I want to calculate string font width in pixels in android. I am having string array and I am appending all these array elements to form one string. so I have to set fixed spacing in between two consecutive array element.i am placing this final string into textview. Finally I am adding this textview to tablerow. In short i want to view table structure with title and values. I have seen many post over here but not getting solution for this.
Thanks in advance...!!!
Looks like there is a measureText method available on Paint. I also found an example:
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setTextSize(64);
mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
// ...
float w = mPaint.measureText(text, 0, text.length());
Thanks Jake for the working solution, yet I found this to work for me too:
someText = "Lorem Ipsum";
TextView myTextView = (TextView) findViewById(R.id.myTextView);
float w = myTextView.getPaint().measureText(someText);
Getting the Paint directly from the TextView that I'm going to work with makes me feel that the anwser will be more accurate.
I don't know if this works, but it can.
int textSize=20;
Paint p=new Paint();
p.setTextSize(textSize);
String string="Hello im a string.";
public int getStringWidt()
{
return string.length*textSize;
}

Clone textview to append it to a ViewGroup

I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.

Categories

Resources