So I've tried my best but I have not found anything close to what I would like. I would like to know if this is possible without making 50 instances in my xml file. What I think i've come close to though:
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
for(int i=0;i<50;i++){
bitmapArray.add(Bitmap.createBitmap(Bmp1,(int) image.getX(),(int) image.getY(), bullpick.getWidth(), bullpick.getHeight()));
bulletArray[i].setImageBitmap(bitmapArray.get(i));
}
I've also tried making an ImageView array of one imageView but that would only leave me with one imageview. Any help is much appreciated!
Edit: For anyone who doesn't know what I'm talking about, i'm trying to make an array of 50 imageviews of the same image, but anything that would end up with an array of 50 images would be what I'm after. Thanks!
Put your image in drawable folder.
Then create a simple loop:
ArrayList<ImageView> imageViews= new ArrayList<ImageView>();
for (int i = 0; i < 50; i++) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.yourimage);
imageViews.add(imageView);
}
This would create an arraylist of 50 imageviews.
Related
I've been trying to write some dynamically generated layout code for a simple app I came up with. I want to display a vertical row of cards, each on containing an undefined number of vertically aligned text boxes.
I wrote the code to generate these and populate the text, but it doesn't appear to be working and I can't figure it out for the life of me.
I'm new to Android Studio, and Java is still relatively fresh to me as well, so I could well be missing something quite obvious here.
I've tried using a few different types of View in A. Studio, and so far most work by themselves, but none can be contained within a card which would be ideal for me. Dynamically creating and editing properties of textViews works fine, but once I include the card view they no longer appear using the exact same code.
//Define Params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left,top,right,bottom);
//Add a card for each ingredient
for (Ingredient ing : ingredients)
{
CardView card = new CardView(this);
CardView.LayoutParams cardParams = new CardView.LayoutParams(CardView.LayoutParams.WRAP_CONTENT, 200);
card.setLayoutParams(cardParams);
card.setRadius(15);
card.setPadding(25,25,25,25);
card.setElevation(10);
card.setMaxCardElevation(30);
card.setBackgroundColor(Color.DKGRAY);
//Make a grid for each card, text on the left, image on the right
LinearLayout linearLayoutInCard = new LinearLayout(card.getContext());
LinearLayout.LayoutParams layoutParamsInCard = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayoutInCard.setLayoutParams(layoutParamsInCard);
card.addView(linearLayoutInCard);
for(int x = 0; x < 3; x++)
{
TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setPadding(left, top, right, bottom);
textView.setTextSize(15);
textView.setElevation(11);
textView.setTextColor(Color.WHITE);
linearLayoutInCard.addView(textView);
switch (x)
{
case 0:
textView.setText(ing.name);
break;
case 1:
textView.setText(ing.price);
break;
case 2:
//textView.setText(ing.calories);
break;
}
}
I'm expecting a vertical row of cards with text boxes vertically aligned withing them, each with their own content (this whole script will only make one card for now, but that's a data driven thing) yet when I run the application, I get nothing but a blank screen.
Before venturing much further...this could be an example of the XY Problem.
Maybe have a look at the RecyclerView option?
RecyclerViews are entirely designed to manage the UI look and responsiveness of changing/scrolling data sets.
They can be a bit "what the heck" at first...but once writing them a few times, your UI look and code base is a lot more efficient and clean.
RecyclerView example Youtube
Here's a picture of what I want to do:
where all the pictures are different and I could have between 2-10.
The code I have now is:
(flag is # of pictures)
(picUris[] is my array of Uri's)
RelativeLayout imgLayout = (RelativeLayout) findViewById(R.id.RelativeLayoutPictures);
for (int i = 0; i < flag; i++)
{
ImageView iv = new ImageView(this);
iv.setImageURI(picUris[i]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.setMargins(50*i, 50*i, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imgLayout.addView(iv, lp);
}
and the result:
The problems are that it's getting resized and doesn't get cut off by the screen like I want, and there's something weird going on at the bottom.
Why not using the Stackview? basically it will achieve the exact smae result without making you creating the view progrimatically....you don't have to re-invent the wheel...
where all the pictures are different and I could have between 2-10
I think you should use LayerDrawable and offsetting each layer by for example setLayerInset. Or creating all of the background in xml and set it to your viewgroup. I do not recommend you use FrameLayout and RelativeLayout because they both use much RAM in your situation.
I have a RecyclerView in which I am displaying an integer array of Drawable. However I have a folder of Bitmaps on the sd card I want to display instead. But I have not been able to work out how to convert the bitmaps into drawables and put them into an int array for display or even if this is the best way of working this out.
This is this is the code for where I set my adapter in onCreateView where I use getData() to give the images for the adapter:
adapter = new ProgressImagesListAdapter(getActivity(), getData());
mProgressImagesListRecycler.setAdapter(adapter);
This is getData():
public List<ProgressImagesListInformation> getData() {
List<ProgressImagesListInformation> mFrontViewImagesList = new ArrayList<>();
int[] mImages = new int[]{
R.drawable.boomer,
R.drawable.bosco,
R.drawable.brewster,
R.drawable.dory,
R.drawable.gabby,
R.drawable.hunter,
R.drawable.kitkat,
R.drawable.doug,
R.drawable.mae,
R.drawable.millie
};
for (int i = 0; i < mImages.length; i++)
{
ProgressImagesListInformation current = new ProgressImagesListInformation();
current.mImageId = mImages[i];
mFrontViewImagesList.add(current);
}
return mFrontViewImagesList;
}
The way I was thinking was to change the contents of getdata(), make a for loop that converts the contents of sd card folder bitmaps into array of drawables using somthing like the line below in the for loop but I have not been able to work a way to implement it. This obviously gives me the error found type bitmap expected type integer but I don't understand why or how to fix it.
ArrayList<Integer> ImagesArray = new ArrayList<>();
for (int i = 0; i < sNumberOfFilesInImageDirectory; i++)
{
ImagesArray.add(new BitmapDrawable(getResources(), MyBitmapImage));
}
Thanks for your help.
You'll have to know the location of those images on the SD card. Are they always on the same folder? If so, it'll be easier for you.
First of all, don't forget to declare android.permission.READ_EXTERNAL_STORAGEon your AndroidManifest.xml file.
Then, you'll need to use BitmapFactory along with the path to your images to get the Bitmap objects and put them into a List like an ArrayList or simply use an array.
After that you can use the images like any other. But they won't be int resources, they'll be actual Bitmap objects. To use the resources like R.drawable.someimage they'll have to be located under your res/ folder.
Example:
BitmapFactory.Options opt= new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, opt);
list.add(bitmap); // Or do something else with the Bitmaps.
And if you want to save those images elsewhere, check this answer.
The other steps would require you to understand how to use CardView and RecyclerView. You can read this post on Android Essence and this one on Big Nerd Ranch, and of course this one from the android training guide to get going.
**EDIT: ** And to use a Bitmap on an ImageView, use this:
Bitmap yourBitmap; // Getting from somewhere else.
ImageView myImage = (ImageView) findViewById(R.id.myimageview);
myImage.setImageBitmap(yourBitmap);
And if for some reason you have a Drawable object instead of a Bitmap you can call:
Drawable yourDrawable; // Getting from somewhere else.
ImageView myImage = (ImageView) findViewById(R.id.myimageview);
myImage.setImageDrawable(yourDrawable);
More info on ImageView on this link.
I am trying to do a Translate Animation on multiple ImageViews(Buttons) simultaneously. The TranslateAnimations however, vary for each ImageView, so I am putting them in an AnimationSet. The problem is, that somehow the animation is never even run... I don't know why. Here is my code:
ArrayList<TranslateAnimation> animlist = new ArrayList<TranslateAnimation>();
AnimationSet set = new AnimationSet(true);
//The following for-loop is actually running inside two other for loops... I'm skimming it down a little for you guys
for(int i = ROWS-1; i > row;i--){
if(!usedFields[column][i]){
ImageView iv = (ImageView)GameLayout.findViewWithTag(""+row+","+column);
TranslateAnimation transanim = new TranslateAnimation(0,0,-(i-row)*letterHeight,0);
transanim.setDuration(1000);
iv.setAnimation(transanim);
animlist.add(transanim);
break;
}
}
for (TranslateAnimation anim : animlist){
set.addAnimation(anim);
}
set.startNow();
Thank you, I appreciate it!
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]);
}