android : Relative layout width wrap_content - android

I have following layout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<ImageView
android:id="#+id/personal_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/dom_logo_new" />
<ImageView
android:id="#+id/round_corner_top_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/corner_white_left_top" />
<ImageView
android:id="#+id/round_corner_top_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/corner_white_right_top" />
<ImageView
android:id="#+id/round_corner_bottom_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/corner_white_left_bottom" />
<ImageView
android:id="#+id/round_corner_bottom_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/corner_white_right_bottom" />
</RelativeLayout>
There is 1 main image (personal_image) and 4 small images.
I need display main image (personal_image) at center horizontal of the screen with 4 images at its each corner (top left, top right, bottom left, bottom right). but RelativeLayout has width and height of all screen.
How can I set the width and the height of RelativeLayout the same width and height of personal_image??

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<ImageView
android:id="#+id/personal_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/round_corner_top_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/round_corner_top_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/round_corner_bottom_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/round_corner_bottom_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/round_corner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
As you are using wrap content you donot have to worry about different screen sizes.it will manage by its own.

I suggest you use a 9-patch instead of corner images which is the Android way to solve such issues.

Related

unwanted horizontal spacing between my ImageViews

I'm trying to place four ImageViews in a 2x2 grid fashion, and I tried everything that I know from a tableLayout to a relativeLayout and finally a nested Layout as the following
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/top">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/top"
android:id="#+id/btm">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView3"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView4"
android:layout_below="#id/imageView2"
/></LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/changepic"
android:id="#+id/button"
android:layout_below="#id/btm"/>
but no matter what I do, I always get this "unwanted" horizontal spacing between ImageViews in same row, which is surprisingly absent between ImageViews verticaly, as the pic.
Picture Here
what is causing this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="left"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="right"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="#+id/btm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="left"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="right"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
I'd get rid of the linear layout altogether- you don't need it, and adding extra views has bad performance issues on android. Instead make the second image android:layout_toRightOf the left one. That should also kill the space.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:id="#+id/imageView"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_toRightOf="#id/imageView"
android:id="#+id/imageView2"
/>
...
In addition- are you sure your width is correct? You have it as a pair of 150dpx150dp images. Those images are not square. You may not be that wide, so the extra space is because the image only fills part of that 150dp. You probably want wrap_content for width.

ImageView getting very small under a linear layout

In one of my screens, I expect to acheive this kind of a layout
Highest importance being the images correctly scaled. These images i am giving are of adequate resolution and works fine on another fragment layout.
My layout.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/stadiumbg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/listview_item"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:orientation="vertical" >
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="70%"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="80%"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:orientation="vertical" >
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:padding="5dp"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/listview_item"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vote" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down Vote" />
</LinearLayout>
</LinearLayout>
Everything else is great. The drawable listview_item is something that has been given to provided rounded corners, and tested in a listview with the above said images. Perfectly fine.
But in this layout, both the images gets super downscaled. It is like this.
wasting all that space for no reason.
What I have tried:
adjustViewBounds set to false
Scaletype as fitXY
Any idea to get What I want?
Change the width and height of the ImageView. Instead of:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Use:
android:layout_width="200dp"
android:layout_height="200dp"
Change the numbers to fit what you want.

How to set RelativeLayout's width same as sibling View?

So i have layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/advantage_1" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/advantage_2" />
</RelativeLayout>
<ImageView
android:id="#+id/lower_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/line" />
</LinearLayout>
Because of aligning views inside the RelativeLayout it fills all the screen. How to set RelativeLayout's width same as lower ImageView(lower_image_view)?
Try like this. The main idea is you put your RelativeLayout and the ImageView inside another RelativeLayout. Then set android:layout_alignLeft="#+id/lower_image_view" and android:layout_alignRight="#+id/lower_image_view" of the child RelativeLayout. This will achieve the width of the ImageView. You may want to modify this to achieve your desired result, but you get the idea :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<RelativeLayout
android:id="#+id/parentRelativeLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignLeft="#+id/lower_image_view"
android:layout_alignRight="#+id/lower_image_view"
android:orientation="horizontal">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<ImageView
android:id="#+id/lower_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>

How to align three ImageViews horizontally in Android?

I want to show three ImageViews at the bottom of the screen with equal weight. But the size of the images is not fixed. I want to show 2 images and one should be hidden, what should I do?
How to adjust the space in between three ImageViews which should be side by side of each other, and the 3rd ImageView should not overlap the two ImageViews which are visible on the screen?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_imge_1"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:layout_toLeftOf ="#+id/imageView6"
android:layout_alignParentBottom="true"
android:padding="15dp" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#54F71D"
android:src="#drawable/ic_launcher"
android:layout_alignParentBottom="true"
android:padding="15dp"
android:layout_margin="10dp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1DF7AB"
android:src="#drawable/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/imageView6"
android:padding="15dp" />
</RelativeLayout>
Try this way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|bottom" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#F9F939"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#54F71D"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#1DF7AB"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Output
From what I was able to decrypt, you're probably trying to achieve this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:padding="15dp"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:padding="15dp"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F9F939"
android:src="#drawable/ic_launcher"
android:padding="15dp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
The result:
The key is to use an enclosing LinearLayout to hold your ImageViews and having that LinearLayout aligned to Bottom in your RelativeLayout.
Use android:layout_weight="1" for each ImageView. And replace your RelativeLayout to Linearlayout.

Horizontal Scroll view left right arrow display problem?

I have one horizontal scroll view in that there are images if user scroll and further images are available than I need to display right side right arrow to indicate user to scroll it again and see other images.
Thanks in advance.
I've just came across this problem and I realized, that you can use scrollview fade function for this purpose. Simply use relative layout in which you have 3 view: leftarrow, rightarrow, horizontalscroll. Set fadingEdgeLength to sufficient length, so when the scrollview fades out, arrow will appear.
Example:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:src="#drawable/arrow_l"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<ImageView android:src="#drawable/arrow_r"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<HorizontalScrollView android:id="#+id/horizontalScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdgeLength="20dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/harokLayout"
android:background="#EEEEEE"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<ImageView android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

Categories

Resources