I am currently building an app that has 6 images on the main view and it generates random images from my drawable folder (shuffling a deck of cards)
initializing the decks:
public static void initDecks() {
int m = 0;
for (int i = 0; i < suites.length; i += 1) {
for (int j = 0; j < regularCards.length; j += 1) {
regularDeck[m++] = "drawablw/" + suites[i] + regularCards[j]
+ ".jpg";
}
}
m = 0;
for (int i = 0; i < suites.length; i += 1) {
for (int j = 0; j < trickCards.length; j += 1) {
trickDeck[m++] = "drawable/" + suites[i] + trickCards[j]
+ ".jpg";
}
}
Collections.shuffle(Arrays.asList(regularDeck));
Collections.shuffle(Arrays.asList(trickDeck));
}
shuffle the deck:
public static String[] getCards(int size) {
String[] result = new String[size];
for (int i = 0; i < size - 2; i += 1) {
result[i] = regularDeck[i];
}
result[size - 1] = trickDeck[0];
Collections.shuffle(Arrays.asList(result));
return result;
}
in my main activity, I assign the cards to the view and when the user clicks on them, I want to know if the image is a trick card or a regular one.
is there a way to find out if the card that was clicked a trick one or not? something like if(image.getImageDrawable().equals(R.drawable.trick.jpg)?
Store the name/id of the drawable at the point where you set it to the ImageView. So you always have a member variable in your class/activity that holds the current image identifier.
Please look ovet this link
Get the ID of a drawable in ImageView
Basically you can set the id in ImageView tag when ever you set the image in ImageView and get from that where you need....
public static Integer getDrawableId(ImageView obj)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
Field f = ImageView.class.getDeclaredField("mResource");
f.setAccessible(true);
Object out = f.get(obj);
return (Integer)out;
}
I found that the Drawable resource id is stored in mResource field in the ImageView object.
Notice that Drawable in ImageView may not always come from a resource id. It may come from a Drawable object or image Uri. In these cases, the mResource will reset to 0. Therefore, do not rely on this much.
Related
I have this array of drawables:
Integer[] mThumbIds = { R.drawable.celeb_0, R.drawable.celeb_1,
R.drawable.celeb_2, R.drawable.celeb_3, R.drawable.celeb_4,
R.drawable.celeb_5, R.drawable.celeb_6, R.drawable.celeb_7,
R.drawable.celeb_8, R.drawable.celeb_9, R.drawable.celeb_10,
R.drawable.celeb_11, R.drawable.celeb_12, R.drawable.celeb_13,
R.drawable.celeb_14, R.drawable.celeb_15, R.drawable.celeb_16,
R.drawable.celeb_17, R.drawable.celeb_18, R.drawable.celeb_19,
R.drawable.celeb_20, R.drawable.celeb_21, R.drawable.celeb_22,
R.drawable.celeb_23, R.drawable.celeb_24, R.drawable.celeb_25,
R.drawable.celeb_26, R.drawable.celeb_27, R.drawable.celeb_28,
R.drawable.celeb_29, R.drawable.celeb_30, R.drawable.celeb_31,
R.drawable.celeb_32, R.drawable.celeb_33, R.drawable.celeb_34,
R.drawable.celeb_35, R.drawable.celeb_36, R.drawable.celeb_37,
R.drawable.celeb_38, R.drawable.celeb_39, R.drawable.celeb_40,
R.drawable.celeb_41, R.drawable.celeb_42, R.drawable.celeb_43,
R.drawable.celeb_44, R.drawable.celeb_45, R.drawable.celeb_46,
R.drawable.celeb_47, R.drawable.celeb_48, R.drawable.celeb_49};
I want to replace the above code with some way of reading all the images in a specific drawable folder and inserting them into mThumbIds
or replace it with something like this:
for (int i = 0; i < 50; i++) {
mThumbIds[i] =R.drawable.celeb_i; // i is a variable in R.drawable.celeb_i
}
I found the solution like so:
for (int i = 0; i < 50; i++) {
mThumbIds[i] = mContext.getResources().getIdentifier("celeb_"+String.valueOf(i), "drawable", mContext.getPackageName());
}
I have 16 buttons, whose names are "button1", "button2", and so on. Is there a way I can iterate through them using a for loop, by somehow appending the number value upon each iteration? Something like this:
for(int i = 1; i<17; i++ ){
Button b = (Button)findViewById(R.id.buttoni);
I know I can simply initialize each button in my onCreate() method, but I was just curious if I could do it in a way similar to my example code.
Thank you.
You can use getIdentifier :
for(int i = 1; i<17; i++ ){
int buttonId = getResources().getIdentifier("button"+i, "id", getPackageName());
Button b = (Button)findViewById(buttonId);
//Your stuff with the button
}
You can create an array of Button's and use getIdentifier method that allows you to get an identifier by its name.
final int number = 17;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
In case someone is interested how to achive the same result using Java only
The solution above uses Android specific methods (such as getResources, getIdentifier) and can not be used in usual Java, but we can use a reflection and write a method that works like a getIdentifier:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
And then:
final Button[] buttons = new Button[17];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
NOTE:
Instead of optimizing this kind of code you should rethink your layout. If you have 17 buttons on the screen, a ListView is probably the better solution. You can access the items via index and handle onClick events just like with the buttons.
I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}
I have various images in the res/drawable directory , i want to get a random image from it that has "module_" suffix, then load that into a drawable object.The image names are not not named consecutively i.e 1 ,2 ,3 etc , they have different names describing what it contains i.e. "apple", "banana" etc
Any ideas?
void populate() {
try {
ArrayList<Integer> number = new ArrayList<Integer>();
for (int i = 0; i <= 48; i++) // 1//
{
number.add(i + 1);
Log.i("nuber in loop ",number.add(i+1)+"");
}
Collections.shuffle(number);
Random r = new Random();
int Start = r.nextInt(number.size());
if (Start - 9 >= number.size() - 1)
Start -= 9;
Log.i("Start ",Start+"");
for (int i = 0; i <=8; i++)
{
String imgName = "img" + number.get(Start);
Log.i("imagename", imgName);
int id = getResources().getIdentifier(imgName, "drawable",
getPackageName());
Log.i("id", id + "");
ImgBtnArray[i].setImageResource(id);
Start++;
}
}
catch (Exception e) {
}
}
into the above function it will
48 is total no of images into drwable from thatany 9 randm img i'll get and it will check for not to allow duplicate no also
in to the above i've 1 to 49 imgs into sequence 1.png,2.png lyk that that is more easy way.. i think bt u can create string array may be this way u can do
I dont think its possible to a get a random image from the drawable directory, so i just hard coded an array with the images and randomly index into that.
I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
try this
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id-1] = 0;
}
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
That is because id has been considered as Length of the String and you r trying to access the same element... but the last element is vat[i][id-1] but u r trying to get vat[i][id]
So better use this..
for (int i = 0; i < sirid.length; i++) {
for(int j = 0; j<id ; j++){
vot[i][j] = 0;
}
}