Reduce redundant code with ImageViews and switch cases - java

I have 7 ImageView's having each a switch case of 7 different cases that have the same behavior of setting images for all 7 ImageView's. Is there any way to shorten the code for this, right now I have to call each ImageView's by its name in the switch case for all the ImageView's separately and it makes the code lengthy and amateur. Like the following switch code for all imageviews being called each with their imageview names.
ImageView1 = (ImageView) v.findViewById(R.id.ImageView1);
ImageView2 = (ImageView) v.findViewById(R.id.ImageView2);
ImageView3 = (ImageView) v.findViewById(R.id.ImageView3);
ImageView4 = (ImageView) v.findViewById(R.id.ImageView4);
ImageView5 = (ImageView) v.findViewById(R.id.ImageView5);
ImageView6 = (ImageView) v.findViewById(R.id.ImageView6);
ImageView7 = (ImageView) v.findViewById(R.id.ImageView7);
switch(x){
case1:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image1));
break;
case2:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image2));
break;
case3:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image3));
break;
case4:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image4));
break;
case5:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image5));
break;
case6:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image6));
break;
case7:
ImageView1.setImageDrawable(getResources().getDrawable(R.drawable.Image7));
break;
}

For this case, you could create an Array of ImageView and another one, with the id's of the drawables. Something like that:
int[] idOfDrawables= {R.drawable.image1, R.drawable.image2,...,R.drawable.image7};
ImageView[] bundleImageView= {ImageView1, ImageView2, ...,ImageView7};
After that you could create a method, where the logic will happen, like this:
public void setImageToImageView(ImageView[] bundleImageView, int[] idOfDrawables)
{
for (int i=0; i< bundleImageView.length; i++)
{
bundleImageView[i].setImageResource(idOfDrawables[i]);
}
}

As the others suggested, create two arrays:
int[] ids = {R.drawable.Image1, R.drawable.Image2,...,R.drawable.Image7};
ImageView[] imageViews = {ImageView1, ImageView2, ...,ImageView7};
then use this:
private void showImage(int x, ImageView iv) {
iv.setImageDrawable(getResources().getDrawable(ids[x - 1]));
}
if you want to put the image for ImageView3 call showImage(x, ImageView3)
or if you want to show the image for all:
for (int i = 0; i < imageViews.length; i++) {
showImage(x, imageViews[i])
}

Put all your images as well as your imageViews in two separate arrays in a way that corresponds to each other, then use a for loop to loop through it.
findViewById...
int[] imageIds = {R.drawable.image1, R.drawable.image2,...,R.drawable.image7};
ImageView[] imageViews = {image1, image2, ...,image7};
for (int i = 0; i < imageViews.length; i++) {
imageViews[i].setImageResource(imageIds[i]);
}

you can use butterknife open source project which helps you to decrease adding findviewbyId in your code.
Find the link for it
Butterknife open source project

Related

How to identify EditText by its tag

I am trying to get the value out of a series of EditText views in my app with a for() loop, but when I call the getText() method, the app crashes. There are 9 EditTexts and they each have a Tag from 1-9, so I am trying to use the getIdentifier() method and search for each EditText using its Tag, and I think that's the part that's not working... Any thoughts here? Thanks in advance!
public void xCalculateProfits (View view){
for (int x = 1; x <= 9; x++){
EditText xCurrentBox = (EditText) findViewById(getResources().getIdentifier(Integer.toString(x),"tag",getPackageName()));
Toast.makeText(this, xCurrentBox.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
I think you'r app crashed because xCurrentBox is null.
you can define an int array and in that you store id of each EditText and use for loop to access the all EditText.
of course you can find View with tag . but you must can access to root view.
for example i have a linear layout in my activity and i can use that to access child view by tag .
for example :
LinearLayout linearLayout = findViewById(R.id.linearLayout);
for(int i=1 ; i<=4 ; i++)
{
EditText et = linearLayout.findViewWithTag(i+"");
Toast.makeText(this , et.getText().toString() ,
Toast.LENGTH_SHORT).show();
}
To find view by its tag, use View#findViewWithTag("tag"). This is a method of a View class, so you have to call it on any layout below your EditTexts, for example root View:
View root = getWindow().getDecorView().getRootView();
for (int x = 1; x <= 9; x++){
EditText xCurrentBox = (EditText) root.findViewWithTag("" + i);
Toast.makeText(this, xCurrentBox.getText().toString(), Toast.LENGTH_SHORT).show();
}
However, if you created these EditTexts in layout .xml file (not generated dynamically), I would suggest creating an array containing their IDs, like so:
public int[] viewsIds = {
R.id.edit_text1,
R.id.edit_text2,
R.id.edit_text3,
R.id.edit_text4,
...
};
and then use it in your for loop
for (int x = 1; x <= 9; x++){
EditText xCurrentBox = (EditText) findViewById(viewsIds[i]);
Toast.makeText(this, xCurrentBox.getText().toString(), Toast.LENGTH_SHORT).show();
}
This way your code is safer as you can benefit from static typing with autogenerated R class

Is there any possibility to set scale of multiple imageviews in a loop?

I want to set the ScaleX and ScaleY of multiple imageviews to the same value.
Is there a possibility to do it in a loop?
for(int i=1;i<=9;i++){
String img = "imageview" + i;
(imageview + i).setScaleX(x);
}
// x is the variable
I have not tested, but this should work
You could set a tag for each ImageView, making sure to increment each, for example:
//first ImageView
android:tag="#string/one" //one = 1
//second ImageView
android:tag="#string/two" //two = 2
Then in your for loop:
for(int i=1;i<=9;i++){
ImageView img = findViewWithTag(String.valueOf(i));
img.setScaleX(x);
}
But I would not recommend this. Instead, just set the setScaleX(x) to each individually. It's not that much code.

Using a variable to define a resource

I want to display 5 random dice, of the 6 sided variety. I have code that generates 5 random integers between 1 and 6, and I have 5 ImageViews of dice. They have ids set to: d6_image_view_1, d6_image_view_2, etc. In the drawable folder, I have d1.jpg, d2.jpg, etc. So if I want to change the second picture to a 4, I would do this:
ImageView d6ImageView2 = (ImageView) findViewById(R.id.d6_image_view_2);
d6ImageView2.setImageResource(R.drawable.d4);
What if I have the numbers for the ImageView and drawable in variables? Do I have to do a long, complicated nested if setup to get the right output? Or can I somehow put those numbers right into the code? Obviously this doesn't work:
int image = 2;
int picture = 4;
ImageView d6ImageView(image) = (ImageView) findViewById(R.id.d6_image_view_(image));
d6ImageView(image).setImageResource(R.drawable.d(picture));
There are no dynamic variables in Java. Java variables have to be declared in the source code. So a solution could be to make two arrays of integers for both drawable resources and view id's
For example
int[] resourceIds = new int[] { R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6 }
int[] viewIds = new int[] { R.id.d6_image_view_1, R.id.d6_image_view_2, R.id.d6_image_view_3, R.id.d6_image_view_4, R.id.d6_image_view_5, R.id.d6_image_view_6 }
Then you could do
int image = 2;
int picture = 4;
ImageView imgView = (ImageView) findViewById(viewIds[image]);
imgView.setImageResource(resourceIds[picture]);

Trouble laying out ImageViews programmatically in RelativeLayout

I'm trying to add five or so ImageViews to a RelativeLayout, with each new one being to the right of the previous one. However, what I get is (apparently) all of them stacked up on each other, as if they were all ALIGN_PARENT_LEFT. Relevant code snippet:
ImageView[] foo = new ImageView[levels];
for (int i = 0; i < levels; i++) {
foo[i] = new ImageView(context);
layoutFoo.addView(foo[i]);
foo[i].setId(10 + i);
if (i == 0) {
RelativeLayout.LayoutParams fooParams = (RelativeLayout.LayoutParams) foo[i].getLayoutParams();
fooParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
foo[i].setLayoutParams(fooParams);
} else {
RelativeLayout.LayoutParams fooParams = (RelativeLayout.LayoutParams) foo[i].getLayoutParams();
fooParams.addRule(RelativeLayout.RIGHT_OF, foo[i-1].getId());
foo[i].setLayoutParams(fooParams);
}
}
Any hints what I'm doing wrong? It shouldn't matter that the ImageViews don't have any width (because I haven't assigned a bitmap to them) yet, right?
Edit: the problem turned out to be not setting a height and width for the ImageView. Solution is to set them either with setLayoutParams() or with the constructor as in JustWork's example.
You must add your ImageViews after setting properties them.
Move this code to end of the for loop:
layoutFoo.addView(foo[i]);
By the way you don't need to ImageView[]. You can accomplish it with one ImageView.
UPDATE:
Here is the code you can do same thing with:
ImageView foo;
RelativeLayout.LayoutParams fooParams;
for (int i = 0; i < levels; i++) {
foo = new ImageView(context);
foo.setId(10 + i);
fooParams = (RelativeLayout.LayoutParams) foo.getLayoutParams(); // I don't understand why you are trying to get params of which hasn't been specified before.
// If I were you, I would do like:
fooParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i == 0) {
fooParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
} else {
fooParams.addRule(RelativeLayout.RIGHT_OF, foo.getId()-1);
}
foo.setImageBitmap(yourBitmap); // Set bitmap.
foo.setLayoutParams(fooParams);
layoutFoo.addView(foo);
}
I think the easiest way to place those images will be using a gallery. Although it's deprecated, I think it's a better idea to do it that way.

setImageresource in android - how to use it for many pictures in a loop

it is a rather simple question. I have 5 imageViews and 5 images in drawable folder. ImageViews are named imageView1...imageView5 and images are named image1...image5
I want how can I write the following elegantly:
imageView1.setImageResource(R.drawable.image1)
imageView2.setImageResource(R.drawable.image2)
imageView3.setImageResource(R.drawable.image3)
imageView4.setImageResource(R.drawable.image4)
imageView5.setImageresource(R.drawable.image5)
As you understand, there are not just 5 imageViews, but there are many more and I want to write it in a loop. I am having problems because R.drawable.imageX is integer not string.
//Declaration
private int[] tabDrawables = new int[] {
R.drawable.image1,R.drawable.image2,
R.drawable.image3, R.drawable.image4,
R.drawable.image5};
private ArrayList<ImageView> listImages = new ArrayList<ImageView>();
//When you find your images on the onCreate method
//add all images here
listImages.add(imageView1);
listImages.add(imageView2);
...
for (int i = 0; i < tabDrawables.length; i++) {
listImages.get(i).setImageResource(tabDrawables[i]);
}

Categories