Android Overlapping Image Views and clickable - android

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.

Related

How to properly scale an imageview within parent layout?

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

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)

Draw Image: Center, Horizontal, Bottom in Java

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

Overlap image over two rows in a listview

I need create a listview with pinned overlay image over upper row and next row, like image example below.
Thanks a lot.
set the Layout parameters of image, ie size not exceed to the parent view
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

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