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;
Related
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.
My activity has 9 image buttons, I would like to change certain properties of multiple image buttons like disable all the image buttons in a certain situation & then enable them back. I think may be i can use loop something like this....
// int [] ids = { R.id.imgBtn1, R.id.imgBtn2......, R.id.imgBtn9 };
// for (int i=0; i<=ids.length; i++){
// ids[i].setEnabled(true);
// }
Thank you all
define one list like follow:
List<ImageView> images = new ArrayList<>();
ImageView iv1 = (ImageView)findViewById(R.id.image1);
ImageView iv2 = (ImageView)findViewById(R.id.image2);
ImageView iv3 = (ImageView)findViewById(R.id.image3);
ImageView iv4 = (ImageView)findViewById(R.id.image4);
ImageView iv5 = (ImageView)findViewById(R.id.image5);
ImageView iv6 = (ImageView)findViewById(R.id.image6);
ImageView iv7 = (ImageView)findViewById(R.id.image7);
//.....
and put all in one list,
images.add(iv1);
images.add(iv2);
// add other view
then work with this list and do what you want like:
for (ImageView iv : images)
{
// your code
}
// Add views from array of ids
ArrayList<View> views = new ArrayList<>();
int [] ids = { R.id.imgBtn1, R.id.imgBtn2, R.id.imgBtn9 };
for (int i=0; i<ids.length; ++i){
views.add(findViewById(ids[i]));
}
// Loop each view and enable it
for (View view : views) {
view.setEnabled(true);
}
I have a layout like so.
HorizontalScrollView
LinearLayout (Horizontal)
ImageView (A number of times)
Is there any event/way I can tell which ImageView is visible? I'd like an event if possible so I can store the index of the current Image that's visible.
Something like
class MyViewClass
private int index;
....
imageView.onEventViewListener(...
index = currentImageIndex;
);
EDIT: My current code loading the ImageViews
OnClickListener click = new OnClickListener() {
#Override
public void onClick(View v) {
Object i = v.getTag();
smoothScrollTo((width*((Integer) i+1)), 0);
}
};
for(int i = 0;i < mImages.size();i++) {
Gallery.Image image = mImages.get(i);
ImageView iv = new ImageView(getContext());
ViewGroup.LayoutParams par = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(par);
iv.setTag(i);
iv.setOnClickListener(click);
Picasso.with(getContext())
.load("http://www.example.com/files/galleries/" + image.getGalleryId() + "/" + image.getSrc() + "_full.jpg")
.into(iv);
layout.addView(iv);
}
You can use viewpager; it will provide all the events that's required by you
You can try this widget:HorizontalListView.
Its attribute mLeftViewAdapterIndex means first Visiable item's index.
Here is the source code in github.
https://github.com/MeetMe/Android-HorizontalListView/blob/master/AndroidHorizontalListView/src/com/meetme/android/horizontallistview/HorizontalListView.java
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);
}
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);