Android - how to add a dynamic image to activity - android

I currently have an activity that does not have an xml layout file. All items are added when needed. I am not in need to add a image to it and it doesnt seem to do anything...
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/Pics/Pic_1.jpg");
iv.setImageBitmap(bm);
ll.addView(iv);
The image is in the correct folder...11 at the end is the Linear view i am adding the image view too...am i missing something?

You can use debugger to see if image is loaded correctly.
And have you tried to set layout params on that ImageView. Because in your case it wouldn't take any space (I think). Try something like:
ll.addView(iv, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Or use exact sizes instead of LayoutParams.WRAP_CONTENT

Related

Create and remove image at certain coordinates on android

I have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)

Displaying Saved Bitmaps in Android (LinearLayout)

I have 2 bitmaps that I saved with view.getDrawingCache(); called firstBitmap and secondBitmap. They were saved from initial drawings made by the user. Now I want to display them. My issue is that only the first picture is displayed by itself (I want both to be shown at the same time). I know that the bitmaps are correct because if I remove layout.addView(pic1), I can see the second image by itself clearly. layout is a LinearLayout.
My suspicion is that the size is an issue but I believe that by default views that are added to the layout have fill parent so this should still result in 2 images not one. I have tried dynamically changing the width and size with LayoutParams but this makes the image disappear entirely. I am open to any suggestions.
ImageView pic1 = new ImageView(this);
pic1.setImageBitmap(firstBitmap);
layout.addView(pic1);
ImageView pic2 = new ImageView(this);
pic2.setImageBitmap(secondBitmap);
layout.addView(pic2);
Try to distribute the weight in your linear layout so that both bitmaps will be visible.
you could use the below code to do that.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
layout.addView(pic1, params);
layout.addView(pic2, params);

How to Make ImageView to Wrap its Contents?

I'm a starter Android learner and I think that this is a very easy question but I can't find it anywhere. I have added an ImageView like this:
//....
iv = new ImageView(context);
iv.setBackground(getResources().getDrawable(drawable));
iv.setAdjustViewBounds(true);
RelativeLayout.LayoutParams imajpara=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
iv.setMaxWidth(100);
iv.setMaxHeight(100);
iv.setLayoutParams(imajpara);
iv.setScaleType(ScaleType.CENTER_INSIDE);
//....
But the ImageView (a ball picture in this case) fits the entire screen, even though the image (.png) is 100x100 pixels. In the code you see my attempts to fix this but none of them worked. The imageview is in a RelativeLayout.
So how can I make the ImageView resize itself according to the image's dimensions?
Use setImageDrawable() instead of setBackground().
Instead of indicating "WRAP_CONTENT" in the constructor of your Layoutparams, you can set directly the right dimensions.

Resize ImageView in run time using a button - Android

I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="#drawable/picture01"
android:id="#+id/picture01holder"
android:layout_below="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
change the Imageview size by taking LayoutParams
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
pic01.setLayoutParams(layoutParams);
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);
Or you can set the components minimal size:
myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);
pic01.setLayoutParams(myParams)

ImageView and LinearLayout, the imageview it's getting blank spaces on top and bottom of the Image

I'm having some issues with LinearLayout and ImageView on Android. I must not use XML for defining the layouts, i'm trying to understand to do it with java code.
I have two elements on the linearLayout, a header (imageview) and a body (gridview with images). Someting is going wrong because the imageview it's getting some blanck spaces on top and on bottom of the original Image.
The original Imageview that i am using for the Header doesn't have these white spaces on top and on bottom of the image, i put the line header.setBackgroundColor(Color.WHITE); and then i noticed that something was going wrong with the imageview because its getting these blanck spaces.
I tested with multiple header images and i noticed that the higher height haves the image, the bigger are the blanck spaces so, they are generated dinamically for some kind of error/bug
How to remove them?
Here is a capture:
Here is the code:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
header = new ImageView(getApplicationContext());
header.setImageResource(R.drawable.header_acc_old);
header.setBackgroundColor(Color.WHITE);
myGridView = new GridView(this);
myImageAdapter=new ImageAdapter(this);
myGridView.setAdapter(myImageAdapter);
ll.addView(header);
ll.addView(myGridView);
setContentView(ll);
I Tryed with these solutions, but they didn't work:
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(header,layoutParams);
Try using setBackgroundResource instead of setImageResource while setting image to ImageView.

Categories

Resources