I have 100 imageview in my layout :
iv1 = (ImageView) findViewById(R.id.ImageView1);
iv2 = (ImageView) findViewById(R.id.ImageView2);
iv3 = (ImageView) findViewById(R.id.ImageView3);
...
...
iv98 = (ImageView) findViewById(R.id.ImageView98);
iv99 = (ImageView) findViewById(R.id.ImageView99);
iv100 = (ImageView) findViewById(R.id.ImageView100);
Now, in my program I want change all image sources time to time, so now how i do this i want some thing like this
for (int F=1; F<101; i++) {
int resID = getResources().getIdentifier("a"+F, "drawable", getPackageName());
ivF.setImageResource(resID);
}
so, any suggestion ?
thanks.
Why dont you create it dynamically , set ids to them and use these ids as per your need to get image references .
I found solution by my own, this how I should declare ImageView
ImageView iv1,iv2,iv3,iv4,iv5,iv6;
ImageView[] image = {iv1,iv2,iv3,iv4,iv5,iv6}
and then when i need to set an image :
for (int F=1; F<7; F++) {
int resID = getResources().getIdentifier("a"+F, "drawable", getPackageName());
image[F].setImageResource(resID);
}
Related
There are a lot of questions on this topic but none whose answers solve my problem.
I have an array of ImageView and, through a for-statement, I would like to get them INVISIBLE. The code is the following.
final ImageView[] image = new ImageView[12];
image[0] = (ImageView) findViewById(R.id.imageView1);
image[1] = (ImageView) findViewById(R.id.imageView2);
image[2] = (ImageView) findViewById(R.id.imageView3);
image[3] = (ImageView) findViewById(R.id.imageView4);
for (int p = 0; p < 4; p++) {
image[p].setVisibility(View.INVISIBLE);
}
It seems that the problem is putting p as argument of image[], I think so because if I put a number instead of p it works.
try to use this it uses Varargs
public void hideViews(View... views)
{ //it will work with parent class but you can change it to ImageView
for (View view : views) {
view.setVisibility(View.INVISIBLE);
}
}
and the call:
hideViews(image1,image2,image3,image4);
good luck;
i know what it means to do too much work on the main thread. But i am unable to determine where this occurs in code.
i get the results before the screen goes black
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d840, error=EGL_SUCCESS
I/Choreographer: Skipped 89 frames! The application may be doing too much work on its main thread.
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d400, error=EGL_SUCCESS
D/OpenGLRenderer: endAllStagingAnimators on 0x7f2b3b1e9800 (RippleDrawable) with handle 0x7f2b43ec6200
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2b3b03d100, error=EGL_SUCCESS
i use ImageViews in my layout but must set images through code because when i set them through xml layout alot more work is done, and way more frames are skipped along with other errors.
my code:
public class GameActivity extends AppCompatActivity {
private List<MatchImageCard> gameCards;
private List<Drawable> gameOverCards;
private List<ImageView> staticImages;
private Game mMatchGame;
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_game);
mMatchGame = new Game(12); //create game using class
assignStaticImages(); //get access to view normally set through xml layout
setStaticImages(); //set these views accessed
assignCards(); //access location of card view store in gameCards List
}
private void assignStaticImages(){
ImageView redStone = (ImageView) findViewById(R.id.red_img);
ImageView blueStone = (ImageView) findViewById(R.id.blue_img);
ImageView yellowStone = (ImageView) findViewById(R.id.yellow_img);
ImageView skull = (ImageView) findViewById(R.id.skull_img);
ImageView backgroundImage = (ImageView) findViewById(R.id.parchment_background);
ImageView backgroundScoreImage = (ImageView) findViewById(R.id.score_parchment);
ImageView spcl1 = (ImageView) findViewById(R.id.special_1);
ImageView spcl2 = (ImageView) findViewById(R.id.special_2);
ImageView oceanBackground = (ImageView) findViewById(R.id.ocean_image);
staticImages = new ArrayList<>();
staticImages.add(redStone);
staticImages.add(blueStone);
staticImages.add(yellowStone);
staticImages.add(skull);
staticImages.add(backgroundImage);
staticImages.add(backgroundScoreImage);
staticImages.add(spcl1);
staticImages.add(spcl2);
staticImages.add(oceanBackground);
}
private void setStaticImages(){
Drawable img1 = ResourcesCompat.getDrawable(getResources(), R.drawable.mouth, null);
Drawable img2 = ResourcesCompat.getDrawable(getResources(), R.drawable.nose, null);
Drawable img3 = ResourcesCompat.getDrawable(getResources(), R.drawable.eye, null);
Drawable img4 = ResourcesCompat.getDrawable(getResources(), R.drawable.display_skull, null);
Drawable img5 = ResourcesCompat.getDrawable(getResources(), R.drawable.parchment, null);
Drawable img6 = ResourcesCompat.getDrawable(getResources(), R.drawable.parchment, null);
Drawable img7 = ResourcesCompat.getDrawable(getResources(), R.drawable.penguin, null);
Drawable img8 = ResourcesCompat.getDrawable(getResources(), R.drawable.pirate, null);
Drawable img9 = ResourcesCompat.getDrawable(getResources(), R.drawable.island_ocean_1, null);
Drawable img10 = ResourcesCompat.getDrawable(getResources(), R.drawable.pirateship, null);
List<Drawable> images = new ArrayList<>();
images.add(img1);
images.add(img2);
images.add(img3);
images.add(img4);
images.add(img5);
images.add(img6);
images.add(img7);
images.add(img8);
images.add(img9);
for(int x = 0; x < staticImages.size(); x++){
ImageView img = staticImages.get(x);
img.setImageDrawable(images.get(x));
}
gameOverCards = new ArrayList<>();
gameOverCards.add(img10);
}
private void assignCards(){
List<ImageView> images = new ArrayList<>();
List<TextView> texts = new ArrayList<>();
ImageView img1 = (ImageView) findViewById(R.id.card1);
ImageView img2 = (ImageView) findViewById(R.id.card2);
ImageView img3 = (ImageView) findViewById(R.id.card3);
ImageView img4 = (ImageView) findViewById(R.id.card4);
ImageView img5 = (ImageView) findViewById(R.id.card5);
ImageView img6 = (ImageView) findViewById(R.id.card6);
ImageView img7 = (ImageView) findViewById(R.id.card7);
ImageView img8 = (ImageView) findViewById(R.id.card8);
ImageView img9 = (ImageView) findViewById(R.id.card9);
ImageView img10 = (ImageView) findViewById(R.id.card10);
ImageView img11 = (ImageView) findViewById(R.id.card11);
ImageView img12 = (ImageView) findViewById(R.id.card12);
images.add(img1);
images.add(img2);
images.add(img3);
images.add(img4);
images.add(img5);
images.add(img6);
images.add(img7);
images.add(img8);
images.add(img9);
images.add(img10);
images.add(img11);
images.add(img12);
TextView txt1 = (TextView) findViewById(R.id.card1_txt);
TextView txt2 = (TextView) findViewById(R.id.card2_txt);
TextView txt3 = (TextView) findViewById(R.id.card3_txt);
TextView txt4 = (TextView) findViewById(R.id.card4_txt);
TextView txt5 = (TextView) findViewById(R.id.card5_txt);
TextView txt6 = (TextView) findViewById(R.id.card6_txt);
TextView txt7 = (TextView) findViewById(R.id.card7_txt);
TextView txt8 = (TextView) findViewById(R.id.card8_txt);
TextView txt9 = (TextView) findViewById(R.id.card9_txt);
TextView txt10 = (TextView) findViewById(R.id.card10_txt);
TextView txt11 = (TextView) findViewById(R.id.card11_txt);
TextView txt12 = (TextView) findViewById(R.id.card12_txt);
texts.add(txt1);
texts.add(txt2);
texts.add(txt3);
texts.add(txt4);
texts.add(txt5);
texts.add(txt6);
texts.add(txt7);
texts.add(txt8);
texts.add(txt9);
texts.add(txt10);
texts.add(txt11);
texts.add(txt12);
gameCards = new ArrayList<>();
for(int x = 0; x < images.size(); x++){
ImageView img = images.get(x);
TextView txt = texts.get(x);
MatchImageCard card = new MatchImageCard(img, txt, x+1);
gameCards.add(card);
}
}
}
As you can see i have 9 ImageViews that i need to access at the beginning to setup layout. I don't know how this may be doing too much work since i access them once then forget about them. As well, i have 12 TextViews and 12 ImageViews that i need to constantly handle. to save time i created a class MatchImageCard, where each object contains 1 textView and 1 ImageView that exist in the xml Layout. Just hearing myself talk allows me to realize that i am doing a lot of work, but i don't know how to reduce the workload on the main thread. if i have to run new Threads, WHERE. on the 9 ImageViews in the beginning, is it where i assign them or Set them? I can make do with any advice offered.
You are doing too much work in the main thread. you should use asynctask and to save time you should use Picasso or Glide.
specially for the code inside the loops. Ex:
for(int x = 0; x < staticImages.size(); x++){
ImageView img = staticImages.get(x);
Picasso.with(context).load(images.get(x)).into(img);
}
As the error message suggest you are doing too much work in the Main thread (or UI thread). Move your code outside of the UI Thread using an AsyncTask. Other possible solution... you are using the emulator, I often had this problem with it, just check if you have the same issue with a real device.
This question already has answers here:
Accessing contents of R.string using a variable to represent the resource name
(4 answers)
Android: Using findViewById() with a string / in a loop
(8 answers)
Closed 7 years ago.
it's my code:
ImageView img1 = (ImageView) findViewById(R.id.img1);
ImageView img2 = (ImageView) findViewById(R.id.img2);
ImageView img3 = (ImageView) findViewById(R.id.img3);
ImageView img4 = (ImageView) findViewById(R.id.img4);
ImageView img5 = (ImageView) findViewById(R.id.img5);
ImageView img6 = (ImageView) findViewById(R.id.img6);
ImageView img7 = (ImageView) findViewById(R.id.img7);
ImageView img8 = (ImageView) findViewById(R.id.img8);
ImageView img9 = (ImageView) findViewById(R.id.img9);
I d like to create something like this:
for (int i=1; i<=9; i++) {
ImageView img+i = (ImageView) findViewById(R.id.img+i);}
How can I do that?
Do It like this. You can get drawable names from array stored in xml or code.
for (int i=1; i<=9; i++) {
int id = getResources().getIdentifier("drawableName", "drawable", context.getPackageName());
ImageView img = (ImageView) findViewById(id);
}
Here "drawableName" is the name of your image and leave "drawable" as is.
You cannot do this in the loop.
Basically R.id.img is a integer and
if you do R.id.img + i, it will result in some random integer.
I have 20 ImageViews in my Activity. I need one method to manage them easily.
My code is like this right now:
Imageview img1 = (Imageview) findviewbyid(R.id.imageview1);
img1.setbackgroundresource(R.drawable.image1);
Imageview img2 = (Imageview) findviewbyid(R.id.imageview2);
img2.setbackgroundresource(R.drawable.image2);
Imageview img3 = (Imageview) findviewbyid(R.id.imageview3);
img3.setbackgroundresource(R.drawable.image3);
.
.
.
but I need an easier way!
You can create an array of ids with all your resources:
int[] myIds = {R.id.imageview1, R.id.imageview2, R.id.imageview3, ...};
int[] myDrawables = {R.drawable.image1, R.drawable.image2, R.drawable.image3, ...};
Then iterate through it using a for loop, while, a map function (I know this is Java 6...).
for (int i = 0; i< myIds.length; i++) {
Imageview img = (Imageview) findviewbyid(myIds[i]);
img.setbackgroundresource(myDrawables[i]);
}
Although it would be better to use a GridView / ListView with an Adapter for that
It seems all your resources are not dynamically defined. Why don't you just set them in your xml:
<ImageView
android:id="#+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image1"/>
Hi I am trying to programatically add an image to an activity for an android app
I have this :
for (int i = 0; i < num_devices; i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
try {
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices);
link_devices.addView(imageView);
however it doesnt let me get the location of the images ( i try and get 0 for getTop, getLeft etc..)
am i doing it wrong and was is the correct way to do it
You are not setting image on that imageview, and you have set WRAP_CONTENT as layout params, which means the size of the imageView is same as size of image you are setting on it.
Since no image is attached the imageView size is 0.
Try setting an image, using any one of the codes ;- imageView.setImageBitmap(Bitmap bm), setImageDrawable(Drawable drawable) or setImageResource(int ResID).
I don't get what you're doing the with this code & its not required:
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);