Covering android ImageView - android

While I was working on my project I found that the image couldn't cover the whole space of ImageView completely! this is the first time that i have got a problem like this.
Here is my code :
int ImgId = getResources().getIdentifier("test1", "drawable", getPackageName());
Photo=new LinearLayout(this);
lp = new LinearLayout.LayoutParams(Width/2,Width/2);
Photo.setLayoutParams(lp);
Photo.setOrientation(LinearLayout.VERTICAL);
Photo.setBackgroundColor(getResources().getColor(R.color.mainGreen));
Img = new ImageView(this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
Img.setLayoutParams(lp);
Img.setBackgroundColor(getResources().getColor(R.color.Black));
Img.setImageResource(ImgId);
Photo.addView(Img);
I have used this piece of code many times before. The image covered the ImageView's space but now its not working well!
What is the problem? How can I cover ImageView completely?

Img.setBackground(ImgId);
//Img.setImageResource(ImgId);
or
Img.setBackground(ImgId);
Img.setScaleType(ImageView.ScaleType.FIT_XY);
Img.setImageResource(ImgId);

Just add ScaleType to the ImageView to specify the behavior
Img.setScaleType(ImageView.ScaleType.CENTER_CROP);

Related

how to removing programmatically composed pictures

I create imageview programmatically. but after I finish game this imageview cannot close how can i close, remove this imageview.
if I use this code image.setImageResource(0); just last image closing but I want to close all image
final ImageView image = new ImageView(getContext());
image.setImageResource(id);
image.setLayoutParams(params);
layout.addView(image);
Great!!! This is my solution..
` final ImageView [] image = new ImageView[5];
`
image[userartis]=new ImageView(getActivity());
image[userartis].setImageResource(id); image[userartis].setLayoutParams(params);
layout.addView(image[userartis]);
for (int i=0;i<=userartis;i++){
image[i].setVisibility(View.GONE);
}

ImageView won't obey maxWidth and height

I have created an ImageView by code and I want it to have a max size of 24x24dp. But the setMaxHeight/Width aren't working. The image I use is a svg Drawable and it's size is 40x40dp.
I have also tried to use imageView.setMinimumHeight/Width and
imageView.setLayoutParams(new ViewGroup.LayoutParams((int) getTypedValueInDP(24), (int) getTypedValueInDP(24)));
without any effect!
Here I am creating the ImageView:
ImageView imageView = new ImageView(context);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
imageView.setPadding(left, 0, left, 0);
and I'm using it with a RelativeLayout also made in code like this:
RelativeLayout toastLayout = new RelativeLayout(context);
toastLayout.setBackground(getBLLShape());
toastLayout.addView(getImageView());
You need to specify the LayoutParams for the ImageView before adding to the ViewGroup.
ImageView imageView = new ImageView(this);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
RelativeLayout toastLayout = new RelativeLayout(this);
toastLayout.addView(imageView);
The padding seems to be messing up with the setMaxWidth(). Make sure that the padding you give at both sides must not be equal to or greater than setMaxWidth argument. In your code, you are giving padding to left and right side. So, change:
imageView.setMaxWidth(getTypedValueInDP(20));
to
imageView.setMaxWidth(2* left + getTypedValueInDP(20));
If your image and imageview have same aspect ratio. Did you try
imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE)

setImageUri on ImageView doesnt work

I am trying to make a picture with my app but it doesn't work.
I'm implementing a function to record a picture with my app, the result of this is a URI.
This URI I have in my activity, with this I try to show the user the recorded picture, to realize this I use from the imageview the method setImageUri ... but it doesn't work for me.
The thing I'm confused about, the imageview exist, in the screen I see the dimension.
In this case, the code that I have implemented
ImageView imgView = new ImageView(activityContext);
imgView.setImageURI(Uri.parse(captureImg.toString()));
mainFrame.addView(imgView, 100, 100);
another solutions:
Drawable d = Drawable.createFromPath(captureImg.toString());
ImageView imgView = new ImageView(activityContext);
imgView.setImageDrawable(d);
mainFrame.addView(imgView, 100, 100);
This both doesn't work for me, I have to try to use any solutions with Bitmap but this doesn't work me too.
Greets
Please make sure your "UI code" (imgView.setImageDrawable(d);) run on UIthread
Take this for reference: http://www.vogella.com/articles/AndroidPerformance/article.html
Try as:
ImageView imgView = new ImageView(activityContext);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
imgView.setLayoutParams(vp);
imgView.setMaxHeight(100);
imgView.setMaxWidth(100);
imgView.setImageURI(Uri.parse(captureImg.toString()));
mainFrame.addView(imgView);
you are not adding LayoutParams to dynamically created ImageView
---Try this---
ImageView imgView = new ImageView(activityContext);
imgView.setImageURI(Uri.parse(captureImg.toString()));
mainFrame.addView(imgView, new LinearLayout.LayoutParams(200,200));

Setting relative layout parameters

So I'm trying to add an imageview to my current xml design - and its working decently. Now my main problem is that I cant seem to find a way to set the attributes for the image like where it needs to be displayed etc.
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.blue_1);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i);
setContentView(mRelativeLayout);
I tried messing around with setlayoutparams but got absolutely no clue what to do with it.
You actually have to use the class LayoutParams to do that efficiently and easily :
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = 25;
params.topMargin = 25;
i.setImageResource(R.drawable.icon);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i, params);
This works for me and put my icon in the top left corner of the screen with the specified margin to the sides of the screen.
Does that help?

How to set gravity (or margins) of ImageView using code?

I want to add ImageView to FrameLayout with Gravity or margins. but FramLayout and ImageView has no method about that(Actually, I can't found that). Reason that selects Framelayout is to put ImageView on ImageView.
Help me plz. It is emergency for me to find solution.
thx.
Below is my code which help understanding my question.
FrameLayout imageFrame = new FrameLayout(mContext);
imageFrame.setLayoutParams(new GridView.LayoutParams(158, 158));
ImageView frame = new ImageView(mContext);
frame = new ImageView(mContext);
frame.setLayoutParams(new LayoutParams(158, 158));
frame.setScaleType(ImageView.ScaleType.CENTER_CROP);
frame.setImageResource(R.drawable.image_frame_n);
ImageView image = new ImageView(mContext);
image.setLayoutParams(new LayoutParams(148, 148));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setImageResource(mThumbIds[position]);
// image is needed to have a margin or gravity to be positioned at center of imageFrame
imageFrame.addView(image);
imageFrame.addView(frame);
Try:
image.setLayoutParams(new FrameLayout.LayoutParams(width, height, gravity));
More info:
http://developer.android.com/reference/android/widget/FrameLayout.LayoutParams.html

Categories

Resources