This is my first attempt at drawing an image outside of xml in all java. How can I:
Create a new imageView
Attach a drawable to the imageView
Display the imageView to the screen
Align the imageView to the bottom of the screen and centered vertically
All in java code?
Also, how can I "destroy" the image when I am done displaying it?
this "4. Align the imageView to the bottom of the screen and centered vertically" is not possible, just think about it !
so in following code i am showing imageView centered Horizontal
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
l.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
ImageView iv = new ImageView(this);
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
l.addView(iv);
setContentView(l);// or add to any view to whom you want to display it !
Also, how can I "destroy" the image when I am done displaying it
it depends on the code how you displays it
Related
In my App. I have a RelativeLayout containing an ImageView.
The RelativeLayout is set to MATCH_PARENT horizontally and WRAP_CONTENT vertically.
The ImageView is set to WRAP_CONTENT both horizontally and vertically. The image Drawable is a 24x24 Vector icon.
Because the icon is too small to be seen, I want to double its size. That's where the issue is:
// Parent RelativeLayout
RelativeLayout titleLayout = new RelativeLayout(MyApplication.getContext());
titleLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Icon
RelativeLayout.LayoutParams information_icon_layout = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
information_icon_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView information_icon = new ImageView(MyApplication.getContext());
information_icon.setLayoutParams(information_icon_layout);
information_icon.setPadding(
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_left),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_top),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_right),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_bottom)
);
information_icon.setImageDrawable(MyApplication.getContext().getResources().getDrawable(R.drawable.ic_info_outline_black_24dp));
information_icon.setScaleX(2);
information_icon.setScaleY(2);
information_icon.setAdjustViewBounds(true);
The icon scales as expected, but the parent RelativeLayout does not seem to take into account this change, and still calculates its vertical size using the original size of the Icon (24x24).
As a result, the icon is scaling outside of the parent, and only part of it is visible.
My question: How can I tell the parent RelativeLayout to take into account the scaled size of the icon?
Use scaletype property of an imageview it has main 3 property
center,
centercrop,
centerinside,
And if you want to scratch the image fully it also has property as fit center,fit end,fit start,fitxy.You can use this property in xml as well as java.
here is the link
scaletyper
Hi, I want to create this sort of a view where there should be a center image view and others overlapping it and i want it to be clickable. How can I achieve this?
You should be able to make it using just RelativeLayout and ImageView.
RelativeLayout lets you overlap views and position it anyway you want.
For getting the touches you can set OnClickListeners to each of the images.
Example:
RelativeLayout rl = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
//Set imageView bitmap
img1.setDrawable("Img");
//Click of the image
img1.setOnClickListener(new View.OnClickListener(){...});
//Size of the image
RelativeLayout.LayoutParams prms = new RelativeLayout.LayoutParams(width,height);
//Rules for position the view on screen
prms.addRule(...)
//Add image to layout
rl.addView(img1,prms);
setContentView(rl);
Z order is the order items are added to the layout.
Hope this helps.
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)
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.
I have been trying to use a ScrollView on a single ImageView with a JPG (~770 x 1024) over an AVD that's 600x800.
My main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Now, I add a single ImageView with
setContentView(R.layout.main);
ScrollView sv = (ScrollView)findViewById( R.id.scroller );
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "/sdcard/770x1024.jpg" ) ); // same happens with ScaleDrawable.
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv ); // and it does not go any better if I use Linear Layout between the ScrollView and the ImageView.
The result is
The image was displayed in a middle of a ScrollView, wrapped with background area on top and bottom as following:
| } blank
| }
Image|
. |
. :
. :
: } blank
: }
^-scrollbar
Instead of just
Image|
. |
. |
. |
. :
I tried to set the background of the ImageView red, and it verified that the blank margins were ImageView background.
iv.setBackgroundColor( color.Red );
Where I would expect the image to take no more than its size (scaled to the AVD size) and I expect the ScrollView to let me scroll over the remainder (if any).
For some reason, I see that the drawable size is 600x1024.
Moreover
I tried to add a LinearLayout with a dummy text view such as the linear layout is a parent to the ImageView and the TextView, and the ScrollView is a parent to the LinearLayout.
LinearLayout dummy = new LinearLayout( this );
dummy.addView(iv);
TextView someTextView = new TextView( this );
someTextView.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));
dummy.addView( someTextView );
sv.addView( dummy );
The result was very peculiar:
The entire layout was set into the height of a text-less text view (19).
______________________
| T I T L E B A R |
+--------------------+
| []<-image |
| height=19px |
. .
. .
+--------------------+
It is important for me to avoid stretching the image. I can not use FIT_XY for ScaleType.
What is the recommended way to implement a display of a page that can be potentially scrolled?
Do I have to do it manually with a plain layout and scrolling upon GestureDetector.OnScroll events?
Thanks
Shmuel
P.S: Another observation: With an image scaled-down to 600x780 (proportional scaling) it does work properly. Unfortunately, for me it is not feasible to use a set of scaled-down clones of the images.
Add this to your ImageView in the xml:
android:adjustViewBounds="true"
I've had to do something similar, that I believe should work in your scenario, but this is off my head with no testing, so YMMV:
ImageView iv = new ImageView(this);
Drawable image = new BitmapDrawable("/sdcard/770x1024.jpg");
ScrollView scrollView = (ScrollView)findViewById(R.id.scroller);
int xWidth = image.getIntrinsicWidth();
int yHeight = image.getIntrinsicHeight();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(xWidth, yHeight);
iv.setLayoutParams(params);
iv.setImageDrawable(image);
iv.setScaleType(ImageView.ScaleType.CENTER);
scrollView.addView(iv);
I don't know offhand whether or not ScrollView allows both horizontal and vertical scrolling at the same time, or whether you'd have to put a HorizontalScrollView within a ScrollView, or if that's even possible...but this SHOULD get you the correctly sized ImageView!
If you want the image to fill the whole space I would use ScaleType.CENTER_CROP. This will crop the left and right edge of your image a bit however.
Have a look to this post.
the official answer is this :
imgPreview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imgPreview.setAdjustViewBounds(true);