I am trying to give a border to an image view using a layout to wrap it and set the border as background to that particular layout. However the image view appears small and in the top left corner of the frame background.
Here is my code:
<RelativeLayout
android:id="#+id/Relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="40dp"
android:background="#drawable/pic_frame_big_01" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
How can I modify this code so that image fits into the relative layout and occupies it.
EDIT Changed the code of ImageView the image is centered now, but it appears as a very small image centered in the parent.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
Try adding: android:padding="10dp" to the RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="40dp"
android:background="#drawable/pic_frame_big_01"
android:padding="10dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/image1" />
</RelativeLayout>
android:src="#drawable/image1 is important too.. where you replace image1 with some image in your project.
Because you set it to align top left. Remove them and change width and height to match. Try this:
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
edit: Your relative layout is also aligned top left. Try to remove alignment attributes.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:background="#drawable/border_red" >
<ImageView
android:src="#drawable/holotaglogo"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Related
I need to adjust my layout with a button as exit on the right corner of the layout and an image button below the center of the layout. It should be accurately there to support the background for multiple screens.
I used relative layout and inside it the image buttons, but I am unable to set the image button 2 on its position. If i do it, the whole layout is vertically expanded.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffffff"
android:orientation="vertical"
tools:context="com.rabiya.menu.MainActivity"
android:id="#+id/rl01" >
<RelativeLayout
android:id="#+id/rl02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:background="#drawable/background3" >
<ImageButton
android:id="#+id/exit3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="#string/image"
android:layout_alignParentTop="true"
android:background="#drawable/exit"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv01"
android:background="#000"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rl03"
android:background="#00ffffff"
android:layout_below="#+id/tv01"
android:orientation="vertical"
>
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
android:paddingTop="20dip" />
<ImageButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
/>
<ImageButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
/>
<ImageButton
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/click"
android:contentDescription="#string/image"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I don't understand cleary wath yo want but seems a problems that I have
And i change my layout to the FrameLayout
http://www.tutorialspoint.com/android/android_frame_layout.htm
Mayby a gravity center for one and gravite up aling right for the buton do the works
Create a new custom view extended from LinearLayour or RelativeLayout and place an image inside of it. If I understand correctly, you want to manipulate image inside of the button.
Once you have your own view with image inside of you can override on click to make it behave like button.
I have a problem with ImageView as below:
I have an image with big size (bigger than its part in screen), when I set src for imageView as:
<ImageView
android:id="#+id/splash_second_iv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/phone_white"
android:layout_toLeftOf="#id/middle"
android:visibility="visible"
android:padding="0dp"
android:layout_marginBottom="0dp"/>
then I put it into a RelativeLayout, problem is the content of image is displayed as wrap in the ImageView but size of ImageView is very big (seems as it auto create padding in the image view in four sides of ImageView, since my calculation for layout fails (can not align property position of child)).
Anybody can tell me what's the problem in here and how do I resolve this problem?
Please have a look at the image to have a better understanding. Also I added attribute:
android:adjustViewBounds="true"
android:scaleType="fitEnd"
I can align the content as I want but size of ImageView still very big.
The xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:id="#+id/splash_slide_forth">
<TextView
android:id="#+id/splash_forth_tv_title"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="#string/splash_4th_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#2b333b"
android:layout_marginTop="40dp"
android:textSize="28sp"
/>
<TextView
android:gravity="center_horizontal"
android:layout_below="#id/splash_forth_tv_title"
android:id="#+id/splash_forth_tv_desc"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:layout_centerHorizontal="true"
android:text="#string/splash_4th_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2b333b"
android:layout_marginTop="10dp"
android:textSize="15sp"
/>
<ImageView
android:layout_below="#id/splash_forth_tv_desc"
android:id="#+id/splash_forth_iv_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/phone_vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scaleType="fitEnd"
android:adjustViewBounds="true"
android:visibility="gone"
/>
<ImageView
android:layout_below="#id/splash_forth_tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/splash_forth_iv_horizontal"
android:src="#drawable/horizontal_phone"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitEnd"
android:visibility="visible"
/>
Please set image as background,
<ImageView
android:id="#+id/splash_forth_iv_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/splash_forth_tv_desc"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitEnd"
android:background="#drawable/demo_image"
android:visibility="visible" />
I really wish it works for you,otherwise we will do something else.
I need to put two images one below the other in the center of the screen. They need to be in the center. the images will keep changing depending on the code so how do i get the images to work on all screens without moving or the size increasing or decreasing?
I tried using grid view but it seems too much code for two images. it works but is there anything more efficient?
For this type of design i always create a center point on screen,check below code -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_point" />
<View
android:id="#+id/center_point"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<ImageView
android:id="#+id/image2"
android:src="#drawable/ic_launcher"
android:layout_below="#+id/center_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
it will surely works.
All the best
If u have always only 2 images, you can set it in xml file like that:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
First image centers in parent and the second one centers horizontal and also aligns bottom of the screen.
Good luck.
Here this code may help you.You must create one LinearLayout and set him vertical orientation and layout_gravity = center horizontal.After that create two image views which are automatic position in center of the screen one below other.I hope this will help you.Happy coding :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/image_view_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
/>
<ImageView
android:id="#+id/image_view_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Make a vertical Linear-layout that contains both of your ImageViews and then set an equal layout_weight for both of them. Set the layout_centerInParent true for your Linear_layout
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/lock_layout_keypad"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_marginBottom="4dp">
<ImageView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
You can remove android:layout_weight.
I got three images of a frame, left , mid and right. The left and right are normal images which i give as wrap content. The center one is a patched image that can expand horizontally. I need to use relative layout to align them. But the center image must not have fix size(as it expands horizontally). I can do it using linear layout by following code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/cD"
android:src="#drawable/l_left_corner" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/l_vertical_streatch"
android:contentDescription="#string/cD" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/cD"
android:src="#drawable/l_right_corner" />
</LinearLayout>
Can anyone tell me how to do the same in relative layout(if possible, in frame layout too)?
Edit: My full layout (with nested weights)
http://pastie.org/8469454#1-2
You can use following code, just switch 40dp from my left and right ImageViews to wrap_content when you use some real image.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/image_left"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:background="#FF0000"
/>
<ImageView
android:id="#+id/image_center"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#id/image_left"
android:layout_toLeftOf="#id/image_right"
android:background="#00FF00"
/>
<ImageView
android:id="#+id/image_right"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:background="#0000FF"
/>
</RelativeLayout>
this is the result:
I try to make a Layout with two TextViews on top and an ImageView on the bottom of the Layout. But I have problems with the scalingtyp of the ImageView. The Image should always resize propotional but be always on the center buttom. Here is a picture with my problem:
I hope you unterstand my problem.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewGameOver1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top|center_horizontal"
android:gravity="center"
android:text="TextView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textViewGameOver2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewGameOver1"
android:layout_centerHorizontal="true"
android:layout_gravity="top|center_horizontal"
android:gravity="center"
android:text="TextView2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageViewGameOver"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textViewGameOver2"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center_horizontal"
android:scaleType="fitEnd"
android:src="#drawable/sheldon_win" />
</RelativeLayout>
Have anybody a idea to solve this problem?
Best regards!
You should be using a RelativeLayout instead of a LinearLayout. This will let you position the image where you want it and then you can set the size of the ImageView to be whatever size you want.
<ImageView
android:id="#+id/imageViewGameOver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/sheldon_win" />
This should put the image in the lower-right corner and the image should be whatever size the image file is. If you want to change that you can set the width and height to some size in dp's.