1

I would like to know how can I keep my relative layout fixed. I have a relative layout inside that I have an image view. I am also adding custom views to my relative layout. But when I move my custom views the relative layout height increases.

I want the custom view to get cut if it goes out of the bound of my relative layout.

Below is my relative layout

<RelativeLayout 
    android:id="@+id/relativelayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@android:color/holo_blue_light" >

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/content_description"
        android:src="@drawable/img" />    

</RelativeLayout>

Below is the code where I am moving my custom view

relativeLayoutParams.leftMargin += dx;
relativeLayoutParams.topMargin += dy;
v.setLayoutParams(relativeLayoutParams);                        
v.invalidate();
jagmohan
  • 2,052
  • 2
  • 26
  • 41
ik024
  • 3,566
  • 7
  • 38
  • 61

1 Answers1

0

If the RelativeLayout is the main view in the layout, move it into a ScrollView which fills the entire screen. Here is how you can do it

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 
        android:id="@+id/relativelayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light" >

        <ImageView 
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:contentDescription="@string/content_description"
            android:src="@drawable/img" />    

    </RelativeLayout>
</ScrollView>

If the RelativeLayout is not the main view, then you will have to specify some fixed height to it as using wrap_content for layout_height will make the RelativeLayout wrap its contents and it will increase its height accordingly.

jagmohan
  • 2,052
  • 2
  • 26
  • 41
  • your solution solves my problem to some extent. Initially I was trying out with the same image in image view. But in my application the image view which is inside my relative layout its source changes as I am taking image from gallery. The above relative layout is not my main view, I gave 300dp to its height it worked for one image, when I fetch another image from gallery it gives error as "y+height must be <= bitmap.height()" – ik024 Jan 16 '14 at 06:05
  • each image fetched from gallery will have different dimensions, I cant give hard coded values to my relative layout height – ik024 Jan 16 '14 at 06:06
  • Is there a way to dynamically set the relative layout height to that of image height which is fetched from gallery – ik024 Jan 16 '14 at 06:13
  • How about having a fixed height for ScrollView like android:layout_height="300dp"? This will make image scroll in the ScrollView. – jagmohan Jan 16 '14 at 09:10