How can I dynamically achieve a multiple image layering effect? - java

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.

Related

CardView and LinearLayout with TextView children not displaying at all

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

How to align imageViews which are created dynamically inside a class and not the xml file

My Problem: I want to create a for loop in the onCreate() method to save imageViews dynamically. I am being able to do everything properly it's just that the imageView is displayed at the top left most corner, i.e i am not being able to assign it it's alignment.
My Code:
RelativeLayout layout = findViewById(R.id.relativeLayout);
for(int i=0;i<3;i++)
{
ImageView image = new ImageView(this);
image.setMaxHeight(200);
image.setMaxWidth(200);
//CODE TO ADD THE ALIGNMENT OF THE IMAGE
image.setImageResource(R.drawable.red);
layout.addView(image);
}
Try this one:
for(int i=0;i<3;i++)
{
ImageView image = new ImageView(this);
image.setMaxHeight(200);
image.setMaxWidth(200);
//CODE TO ADD THE ALIGNMENT OF THE IMAGE
image.setImageResource(R.drawable.red);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.MarginLayoutParams.MATCH_PARENT, 200);
layoutParams.setMargins(40, 40, 40, 40);
image.setLayoutParams(layoutParams);
layout.addView(image);
}
The legal values can be found at https://developer.android.com/reference/android/view/View.html#setTextAlignment(int)
But this has no effect on an ImageView. I'm not sure what you're actually truing to do, so I can't help you. My best guess is you're trying to arrange views within the parent layout somehow, but to help with that we'd need to know the type of layout the parent is and what you want to do.

Android how to resize (scale) an xml vector icon programmatically

This is driving me crazy. I would like to be able to resize an xml vector drawable icon programmatically in order to use it in an ImageView.
This is what I've done so far which is not working
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_marker,null);
drawable.setBounds(0,0,512,512);
imageVenue.setImageDrawable(drawable);
The vector icon ic_marker is not resized. It just keeps the hardcoded width and height values every time.
Any ideas?
You can change the width and height of your imageview programmatically. Since vector drawables will preserve the original quality of the image, this will make the desired output happen.
ImageView iv = (ImageView) findViewById(R.id.imgview);
int width = 60;
int height = 60;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(params);
I'm currently facing the same problem.
I'm trying something like this, cause ViewParent has actually height set explicitly, so I use match_parent and set margins. It doesn't work all the time though, cause I simply use this view in a viewholder for RecyclerView... Also I've noticed that sometimes I see scaled up version with artifacts, sometimes full size, sometimes there are margins, and bigger margins... But it still might work for you, if you use it in a simpler scenario.
mImageViewFront.setImageDrawable(vectorDrawable);
final int paddingLR = mImageViewFront.getWidth() / 4;
final int paddingTB = mImageViewFront.getHeight() / 4;
LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins(paddingLR, paddingTB, paddingLR, paddingTB);
mImageViewFront.setLayoutParams(params);

Java Android - Making Array of images (same image)

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.

Move view dynamically in relativelayout

I am doing an pageindex, and the current page should be highlighted with a arrow image (imageview).
The index is a RelativeLayout with 25 textviews added to it:
for (int i = 0; i < 25; i++) {
TextView tv = new TextView(this);
tv.setText(Integer.toString(i+1));
tv.setGravity(Gravity.CENTER);
int id = 2000+i;
tv.setId(id);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
if(i==0)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
else
params.addRule(RelativeLayout.RIGHT_OF, prevViewId);
prevViewId = id;
rl.addView(tv,params);
}
And when the page changes I do something like this:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, 2000+i);
params.addRule(RelativeLayout.ALIGN_RIGHT, 2000+i);
params.addRule(RelativeLayout.ALIGN_LEFT, 2000+i);
arrowImageView.setLayoutParams(params);
rl.requestLayout();
rl.invalidate();
Everything looks correct, I can place the arrow at arbitrary page at "start up", but the arrow wont move when page is changed. I have debuged and verified that the code is run and everything looks correct, but still the arrow is stuck in the first position. If I force delete and add a new imageview instead of updating the LayoutParams the arrow will disappear totally.
I have the same problem.. I also want to move my views around at run-time inside a reference layout. So if anyone ca help that would be awesome.
What i believe is happening above is that the arrow IS having its location changed, however it doesn't get updated to the screen. Correct me if I'm wrong, please, this is just my guess as I am too having the same problem.
-edit-
After some messing around I've found what works for me is to remove first then add.
rl.removeView(tv);
rl.addView(tv,params);
-edit-
also, u can save the params for the moving view into a unique variable so that way all ude have to do is change the margins...
for example: instead of params, have it be its own name "arrowParams"
then to move it ude just need: arrowParams.leftMargin = 2000+i; and so on...

Categories