Using loop dynamically to set image in array - android

int imgs[] =
{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e,
// R.drawable.abc,
};
I want to do that thing with the help of if loop.

I hope I understood you correctly and what you're looking for is dynamic creation of that array.
For example, if your drawables are named like R.drawable.image0, R.drawable.image1, etc. And there are N of them you can use the following code.
int imgs[] = new int[N];
 
for (int i = 0; i < N; i ++) {
imgs[i] = this.getResources().getIdentifier("image" + i, "drawable", this.getPackageName());
}
EDIT:
Well, there's some confusion in the comment section.
If you want to keep your drawable names as they are (a, b, c) you would need to define them too. For example
String[] names = {"a", "b", "c"};
int imgs[] = new int[names.length];
for (int i = 0; i < names.length; i ++) {
imgs[i] = this.getResources().getIdentifier(names[i], "drawable", this.getPackageName());
}
But as you see, you're not achieving much by using that loop in this case, because you still have to manually type all the names.

One Solution is create a model class like this :
class DrawableClass{
int id;
Drawable drawable;
}
create objects of this with incremental id in each object. and in loop parse with respect to id.

Related

How can I put series of numbers to a list in android?

What I'm trying to is making a list which contains series of numbers like [1,2,3,4,5,6...100].
In Java, it is simple using 'range' so I tried to find similar class in android.
Therefore, I found that some classes like Range and Intstream, but I don't know how to use them.
I'll be appreciated if you teach me how can I get my purpose, thanks.
You could write a simple function which would look like this:
public List<Integer> buildList(int maximum) {
List<Integer> list = new ArrayList();
for (int i = 1; i <= maximum; i++) {
list.add(i);
}
return list;
}
And a call which produces your desired result would look like this:
List<Integer> list = buildList(100);
If you want an array instead of a list, do this:
int[] array = list.toArray(new int[list.size()]);
int size = 100;
ArrayList<Integer> list = new ArrayList<>(size);
for (int i = 1; i <= size; i++) {
list.add(i);
Log.i("Value is = ", i+"");
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
.....
You don't need to define the size because ArrayList is dynamically
while in some other language we use list[index] to get the value.
But here we use list.get(index) to do that.

How to initialize an Integer array with R.drawables (Images) using for loop or directory in Android?

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

Edit text of several buttons using a for loop

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.

Converting String array to Integer Array

Since I couldn't figure out an easy way to convert my string array into an integer array, I looked up an example for a method and here is what I ended up with:
private int[] convert(String string) {
int number[] = new int[string.length()];
for (int i = 0; i < string.length(); i++) {
number[i] = Integer.parseInt(string[i]); // error here
}
return number;
}
parseInt requires a string which is what string[i] is but the error tells me "The type of the expression must be an array type but it resolved to String"
I can't figure out what is the problem with my code.
EDIT: I'm an idiot. Thanks all it was obvious.
You're trying to read a string as if it were an array. I assume you're trying to go through the string one character at a time. To do that, use .charAt()
private int[] convert(String string) {
int number[] = new int[string.length()];
for (int i = 0; i < string.length(); i++) {
number[i] = Integer.parseInt(string.charAt(i)); //Note charAt
}
return number;
}
If you expect the string to be an array of strings, however, you left out the array identifier in the function prototype. Use the following corrected version:
private int[] convert(String[] string) { //Note the [] after the String.
int number[] = new int[string.length()];
for (int i = 0; i < string.length(); i++) {
number[i] = Integer.parseInt(string[i]);
}
return number;
}
You have an error in your code. Use this code:
private int[] convert(String[] string) {
int number[] = new int[string.length];
for (int i = 0; i < string.length; i++) {
number[i] = Integer.parseInt(string[i]); // error here
}
return number;
}
Your method's parameter is a String and not a String array. You cannot access elements in a String with string[i]. If you want to actually get a single character from a String, use 'String.charAt(..)' or 'String.substring(..)'. Note that charAt(..) will return a char but those are easy enough to convert to Strings.
Use Arrays.asList( YourIntArray ) to create arraylist
Integer[] intArray = {5, 10, 15}; // cannot use int[] here
List<Integer> intList = Arrays.asList(intArray);
Alternatively, to decouple the two data structures:
List<Integer> intList = new ArrayList<Integer>(intArray.length);
for (int i=0; i<intArray.length; i++)
{
intList.add(intArray[i]);
}
Or even more:
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArray));
.#this is worked for me

Android: How can I make a Drawable array?

How can I make an array that handles some of my images so that I can use it like this?:
ImageView.setImageResource(image[1]);
I hope I explained well...
To do that, you don't want an array of Drawable's, just an array of resource identifiers, because 'setImageResource' takes those identifiers. How about this:
int[] myImageList = new int[]{R.drawable.thingOne, R.drawable.thingTwo};
// later...
myImageView.setImageResource(myImageList[i]);
Or for a resizable version:
ArrayList<Integer> myImageList = new ArrayList<>();
myImageList.add(R.drawable.thingOne);
// later...
myImageView.setImageResource(myImageList.get(i));
First of all you have to get all drawable IDs of your pictures with the following method:
int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)
For example, you could read all drawable IDs in a loop:
int drawableId = resources.getIdentifier("picture01", "drawable", "pkg.of.your.java.files");
picture02...
picture03...
..and then save them into an Array of Bitmaps:
private Bitmap[] images;
images[i] = BitmapFactory.decodeResource(resources, drawableId);
Now you are able to use your images as array.
with respect of #Bevor, you can use getResources() method instead of android.content.res.Resources like this:
ArrayList<Integer> imgArr = new ArrayList<Integer>();
for(int i=0;i<5;i++){
imgArr.add(getResources().getIdentifier("picture"+i, "drawable", "pkg.of.your.java.files"));
}
Usage:
imageView.setImageResource((int)imgArr.get(3));
Kotlin code:
initialize below class:
private var drawables: Array<Drawable>
initialize drawables in 1 line:
drawables = arrayOf(
model.getDrawable(R.drawable.welcome_1),
model.getDrawable(R.drawable.welcome_2),
model.getDrawable(R.drawable.welcome_3)
)
my model.getDrawable() is
fun getDrawable(id: Int): Drawable {
return ResourcesCompat.getDrawable(context.resources, id, context.theme)!!
}
set drawable to imageView
holder.imageView.setImageDrawable(drawables[position])
String[] arrDrawerItems = getResources().getStringArray(R.array.arrDrawerItems); // Your string title array
// You use below array to create your custom model like
TypedArray arrDrawerIcons = getResources().obtainTypedArray(R.array.arrDrawerIcons);
for(int i = 0; i < arrDrawerItems.length; i++) {
drawerItemDataList.add(new DrawerItemModel(arrDrawerItems[i], arrDrawerIcons.getResourceId(i, -1), i == 0 ? true : false));
}
drawerItemAdapter = new DrawerItemAdapter(this, drawerItemDataList);
mDrawerList.setAdapter(drawerItemAdapter);

Categories

Resources