Add images to a HorizontalScrollView - android

I want to create a HorizontalScrollView which reads the images from drawable folder. The name of the images are "image1" "image2" ... "image20". I donĀ“t know how I can use the numbers to read them. Here is what I have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (R.drawable.image1);
sv.addView(iv);
}
}

You can do that in two ways.
First one is to create array with the id's of images you want to use and in your for cycle, just add the images to your layout:
int[] images = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image20};
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=0 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (images[i]);
sv.addView(iv);
}
Or the second way, you can create something similar to this:
for (int i=1 ; i<=20; i++){
String uri = "drawable/image"+i;
// int imageResource = R.drawable.image1;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
I didn't tested the codes, but I think they should work.

If you wish to use drawables without an array list, you can do this:
getResources().getIdentifier("Name of the Drawable", "drawable", "Your Package Name");
So your code will be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
int myImage = getResources().getIdentifier("image"+i, "drawable", "Your Package Name");
iv.setBackgroundResource(myImage);
sv.addView(iv);
}
}

A lot of examples like this show building a list of your images first. And then you can use your code and iterate through the list.
So something like
List<Drawable> imagesToAdd = Arrays.asList(R.drawable.image1,R.drawable.image2, .... R.drawable.image20);
Then you can even use the foreach loop to iterate through this.
for (Drawable image in imageToAdd) {
etc...
}

Related

How to get all images as array of bitmap from drawable folder using Drawable?

I am using the below code to get 9 (bird1 to bird9) images from drawable folder by manually giving each name.
How can i use an array or replace R.drawable.bird1 with variable ( as the below statement accept only actual value) to get all the 9 images in an array?
Drawable myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird1, null);
bitmap1 = ((BitmapDrawable) myDrawable).getBitmap();
myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird2, null);
bitmap2 = ((BitmapDrawable) myDrawable).getBitmap();
I got it working like this. I'm sure someone can do it much nicer... but this works.
Caveats:
1) Remember to replace "com.example" with your real package name.
2) Replace "numberOfBirdsBitmaps = 3" with however many you have.
3) The "derp" ImageViews are just for my testing purposes.
public class MainActivity extends AppCompatActivity {
int numberOfBirdsBitmaps = 3;
ArrayList<Bitmap> birdBitmapArray = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView derp1 = (ImageView) findViewById(R.id.imageView);
ImageView derp2 = (ImageView) findViewById(R.id.imageView2);
ImageView derp3 = (ImageView) findViewById(R.id.imageView3);
for (int i = 0; i<numberOfBirdsBitmaps; i++) {
String temporaryString = "bird" + ((Integer) (i+1)).toString();
int temporaryIdentifier = getResources().getIdentifier(temporaryString, "drawable","com.example");
Drawable temporaryDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), temporaryIdentifier, null);
Bitmap temporaryBitmap = ((BitmapDrawable) temporaryDrawable).getBitmap();
birdBitmapArray.add(temporaryBitmap);
}
derp1.setImageBitmap(birdBitmapArray.get(0));
derp2.setImageBitmap(birdBitmapArray.get(1));
derp3.setImageBitmap(birdBitmapArray.get(2));
}
}
Love,
Boober.
You can try and get the drawable id using a loop:
for(int=1; i<=9 ; i++)
{
getResources().getIdentifier("bird"+i,"drawable",getPackageName());
}

Declare ImageView programmatically Android

How can I declare some ImageViews programmatically in oncreate method ? I want to have something like this:
for (int i =1 ; i<=10;i++){
ImageView image+i = (ImageView)findViewById(R.id.image+i);
}
I don't know if it is possible something like this, or you have to make it imageview by imageview.
ImageView[] imageViews = new ImageView[10];
for (int i =1 ; i<= 10; i++){
int id = getResources().getIdentifier("image"+i, "id", getPackageName());
imageViews[i - 1] = (ImageView)findViewById(id);
}
getIdentifier returns the resource's id associate to the name, the first parameter, and you could use an array to store the view's reference.
you can use something like this method
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image+i = new ImageView(this);
image+i.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image+i.setMaxHeight(20);
image+i.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
go through this tutorial and this too.

Adding Dynamically ImageViews to horizontal scrollview in android

I'm trying to show images dynamically by string as ID and want to display it in Horizontal scroll-view as i have 13 cards which can't be fit on screen
Here is my code its working fine but i want it in Horizontal Scroll View
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 43, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(id);
im.setOnClickListener(this);
counter++;
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicksound);
}![enter image description here][2]
As per your requirement I will suggest you to RecyclerView which comes with library which you will have to add and use RecyclerView with horizontal orientation refer to link below.
https://developer.android.com/training/material/lists-cards.html
Or you can do it dynamically as below
here what you can do add Linear horizontal layout inside scrollview say id is imgcontainer then you can do is
create image view dynamically and add to linear layout.
for(int i=0;i<10;i++)
{
//create imageview here and setbg
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
((LinearLayout) findViewById(R.id.imgcontainer)).addView(
imageView, i);
}
int[] image_array = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image10};
LinearLayout ll = (LinearLayout) findViewById (R.id.ll_images);
for (int i=0 ; i<10; i++){
ImageView img = new ImageView (this);
img .setBackgroundResource (image_array [i]);
ll .addView(img );
}

Change property of multiple image button

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

Change many imageView sources using for loop

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

Categories

Resources