In My application I have one big imageview 'A' and six small imageviews s1,s2,s3,s4,s5,s6,intially set the all imageviews with some drawbles.
when drag the small images(s1,s2 etc) and drop on the big imageview 'A' then it is replacing with corresponding image.
for example lets say Imageview A have BAT Image and imageview s1 have the BALL Image when drag the BALL image onto BAT Image then the BAT Image is replacing with BALL Image.
MY Requirement is that when i drag the BALL Image onto the BAT Image then the BAT image should be there and the BALL Image should be overlap(one above each other).so BAT and BALL images should be there on imageview A.
You can use FrameLayout or RelativeLayout. The order of the imageviews will be lastly inserted will be shown above.
eg:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bat"
/>
<ImageView
android:id="#+id/s1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball"
/>
</RelativeLayout>
Use below xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/RelativeLayout1"
android:orientation="vertical">
<!-- Add image A -->
<ImageView android:id="#+id/image1"
android:layout_width="800dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
android:src="#drawable/image1" />
<!-- Add smaller image-->
<ImageView android:id="#+id/image2"
android:layout_width="400dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_alignTop="#+id/image1"
android:src="#drawable/image2" />
</RelativeLayout>
The output will look like below
Use FrameLayout.
For example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#drawable/prescription_not_attached"
android:layout_gravity="center" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#drawable/home_measure"
android:layout_gravity="center" />
</FrameLayout>
Replace my Images with yours.
Hope it helps, Thank you
Try using a FrameLayout, using Linear will not allow overlapping...
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
ImageView bat = new ImageView(this);
iv.setImageResource(R.drawable.bat);
ImageView ball = new ImageView(this);
i.setImageResource(R.drawable.ball);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
i.setLayoutParams(layoutParams);
frame.addView(bat);
frame.addView(ball);
Related
I have the following imageview with a image smaller than the RelativeLayout that contains this ImageView. I want the image scalled to fit the width of the imageView but I cannot make this works. For example I
have this relativeLayout with 350dp width:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#drawable/selector_button_grey"
android:clickable="true"
android:padding="5dp">
<ImageView
android:id="#+id/image_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#color/black"
android:src="#drawable/Nachos"
/>
<TextView
android:id="#+id/image_selector_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/image_selector"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textSize="18sp" />
</RelativeLayout>
As you can see in the picture the image is in the center. The problem with fitXY is that the width works, but doesn't change the height. I want somehitng like, fitX. I have also tried centerInside but it looks the same.
I would also use centerCropor similar. If that does not work for you, check this image and see if you can find anything interesting:
If this is nothing for you, consider to check out image customazations in Android:
https://developer.android.com/guide/topics/graphics/2d-graphics.html
Because your Relative layout and Imageview both have wrap content height so you have to make them match parent and make scaletype fitXY..
as follow code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#drawable/selector_button_grey"
android:clickable="true"
android:padding="5dp">
<ImageView
android:id="#+id/image_selector"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="#color/black"
android:src="#drawable/Nachos"
/>
<TextView
android:id="#+id/image_selector_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/image_selector"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textSize="18sp" />
</RelativeLayout>
You should change the android:scaleType attribute to your ImageView.
If you want to display the pictures in proportion, you can use centerCrop.
If you don't want to display the pictures in proportion, you can use fitXY.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:background="#drawable/selector_button_grey"
android:clickable="true"
android:padding="5dp">
<ImageView
android:id="#+id/image_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#color/black"
android:scaleType="fitXY"
android:src="#drawable/Nachos" />
<TextView
android:id="#+id/image_selector_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/image_selector"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textSize="18sp"/>
</RelativeLayout>
Hope this helps!
In my case it is working fine.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="#drawable/Nachos">
</RelativeLayout>
There is some padding set on the RelativeLayout they may be also a cause to ImageView not touching the sides in your layout RelativeLayout:
Remove this:
android:padding="5dp"
And if you real want some padding set on Top and Bottom but NOT on the Right and Left so if you want padding Top and Bottom ADD these:
android:paddingTop="5dp"
android:paddingBottom="5dp"
And if you do not want padding at all do not ADD them!
I have used another method. First, I obtaing the dimesions without loading from memory :
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(f), null, bitmapOptions);
BitmapFactory.decodeFile(f.getName(), bitmapOptions);
float scale = (bitmapOptions.outHeight * 1f) / (bitmapOptions.outWidth);
cache.put(image, value);
} catch (Throwable e) {
e.printStackTrace();
}
Then used that value scale
imageView.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(IMAGE_SIZE, Math.round(IMAGE_SIZE * imageData.getScale())));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
imageView.setCropToPadding(true);
How can i achieve a layout with 2 images in android, one image overlaying another image at the top right?
an image overlapping another image at the top right
http://i.imgur.com/uT3L2wl.jpg
I tried using relative layout, but i could position the overlay image by giving layout margin, but if i do that i cant support all screen sizes with that design.
and also i couldnt position the overlay image with respect to the underimage.
i want to design my layout like the pic i have shared.
I could get this working from the below code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageButton
android:id="#+id/addnewcustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/c1"
android:layout_toRightOf="#id/c1" >
</RelativeLayout>
<ImageButton
android:id="#+id/hover1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/c1"
android:layout_alignLeft="#+id/c1"
android:layout_marginBottom="23dp"
android:layout_marginLeft="22dp"
android:background="#null"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Try look at FrameLayout. It will do the job.
http://www.learn-android-easily.com/2013/05/frame-layout-in-androis.html
How can I make this layout in android programmatically.
I want just an idea not whole coding thing.
Here is the answer but not the direct answer. This is what I personally do to create complex layout programatically.
Create the same layout in XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/some_big_image" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#DD000000" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView2"
android:layout_toRightOf="#+id/imageView2"
android:text="TextView"
android:textColor="#android:color/white" />
</RelativeLayout>
Now start from top parent to child.
RelativeLayout mParent = new RelativeLayout(this);
RelativeLayout.LayoutParams mParentParams = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
mParent.setLayoutParams(mParentParams);
ImageView mBigImageView = new ImageView(this);
mBigImageView.setLayoutParams(mParentParams);
mParent.addView(mBigImageView);
As you practice you can directly code the same without creating xml.
Basic Idea:
Create a RelativeLayout which contains one ImageView and two TextView which is aligned bottom
set the alpha of the RelativeLayout to adjust the transparency
You can have one relative layout which contain one image view for big image and below it one more linear layout containing image and text as "pearl Continental ..." and background of liner layout should be 50-60% transparent.
Create the background for LL using shapes. and set it as align bottom of parent (relative) layout.
Hope this helps you
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".40"
android:orientation="vertical" >
<ImageView
android:id="#+id/First"
android:layout_width="match_parent"
android:layout_height="160px"
android:layout_centerInParent="true"
android:padding="5dp" />
<ImageView
android:id="#+id/Second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/play_small" />
</RelativeLayout>
I want to set the height of first imageview with id --> First according to the second image view id -- > Second. I am setting the second image view src from xml and i am downloading the first image from some weblink.
You can use LayoutParams for this task.
Firstly you have to find your second ImageView height, and set it to the LayoutParams of your first ImageView.
LayoutParams params = firstImage.getLayoutParams();
params.height = secondImage.getWidth();
firstImage.setLayoutParams(params);
Hope this will help you.
I have a video thumbnail and when you click on it, the video starts playing in the YouTube player.
This works, but It's not clear that you have to click on the thumbnail to play, therefore, I want to draw a play button image over the thumbnail in the bottom right corner. How would I go about this?
I currently have the thumbnail in a Drawable object and the play button in my drawable resource directory.
I tried stuff with bitmaps and canvas but it's quite hard and I don't know if this is the way to go.
Use Relative or FrameLayout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/thumbnail"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
or
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/thumbnail"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_gravity="bottom|right"/>
</FrameLayout>
What about using a FrameLayout or a RelativeLayout to stack the views..
Here follows some psaudo code:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.yourpackage.VideoPlayer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></VideoPlayer>
<!-- Adjust the layout position of the button with gravity, margins etc... -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Play"
/>
</FrameLayout>
I used RelativeLayout and it worked for me
<RelativeLayout
android:id="#+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:clickable="true"
android:layout_gravity="top"
android:maxHeight="400dp"/>
<ImageView
android:id="#+id/play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_menu_play_large"
android:layout_centerInParent="true" />
</RelativeLayout>
This is Easiest Way i Found i think to merge morethan one images in one. this is very helpful if we want share merged file.
Resources r = getResources();
Drawable[] layers = new Drawable[3];
layers[0] = r.getDrawable(R.drawable.image3);
layers[1] = r.getDrawable(R.drawable.image1);
layers[2] = r.getDrawable(R.drawable.image2);
LayerDrawable layerDrawable= new LayerDrawable(layers);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// align image1 over image3
layerDrawable.setLayerGravity(1, Gravity.TOP);
layerDrawable.setLayerInsetTop(1, 250);
// align image2 over image3
layerDrawable.setLayerGravity(2, Gravity.BOTTOM);
layerDrawable.setLayerInsetBottom(2, 250);
}
// both image1 & 2 placed over image3 in layerDrawable.
imageView.setImageDrawable(layerDrawable);
Hope this will works