1

I have created a custom view in which a bitmap is drawn. The problem is that the custom view takes up the whole screen regardless of the layout width/height I set it or it's parent.

I have been going through the forums and I read about overriding the onmeasure method, but for me that returns zero so now the width and height are set to zero.

Here is what I have done so far:

EDIT

FULL xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sliderView="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <RelativeLayout
        android:id="@+id/cameraLayout"
        android:layout_width="wrap_content"
        android:background="@color/black"
        android:layout_height="wrap_content">

        <SurfaceView
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/surfaceview">
        </SurfaceView>

        <LinearLayout
            android:layout_above="@+id/beerName"
            android:id="@+id/beerColours"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/beer1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/beer1"
                android:layout_weight="1"/>
            <ImageView
                android:id="@+id/beer2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/beer2"
                android:layout_weight="1"/>
            <ImageView
                android:id="@+id/beer3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/beer3"
                android:layout_weight="1"/>
            <ImageView
                android:id="@+id/beer4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/beer4"
                android:layout_weight="1"/>
            <ImageView
                android:id="@+id/beer5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/beer5"
                android:layout_weight="1"/>
            <ImageView
                android:id="@+id/beer6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/beer6"
                android:layout_weight="1"/>
        </LinearLayout>

        <EditText
            android:background="@color/black"
            android:id="@+id/beerName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:layout_above="@+id/seperator"
            android:hint="@string/beer_name"
            android:textColorHint="@color/white"
            android:gravity="center_horizontal"
            />

        <ImageView
            android:id="@+id/seperator"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/dotted_separator"
            android:layout_above="@+id/sliderViewLayout"/>

        <RelativeLayout
            android:id="@+id/sliderViewLayout"
            android:layout_above="@+id/bottomsUp"
            android:background="@color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.test.bottomsup.custom_slider_library.sliderView
                android:layout_marginLeft="15dp"
                android:id = "@+id/sliderview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                sliderView:diagonalSlide="false"
                sliderView:translateAxisX="false"
                />
            <ImageView
                android:id="@+id/slider_frame"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/glass"
                android:layout_centerHorizontal="true"
                android:layout_alignParentLeft="true">
            </ImageView>
        </RelativeLayout>

        <com.test.bottomsup.custom.Components.ShadyButton
            android:id="@+id/bottomsUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_red"
            android:textColor="@color/white"
            android:text="@string/button_bottoms_up"
            android:layout_alignParentBottom="true" />

    </RelativeLayout>
</RelativeLayout>

In my fragment where I set the custom view is a follows:

  sliderView slider = (sliderView)view.findViewById(R.id.sliderview);
        slider.setImageResource(R.drawable.beer_with_arrows);
        slider.requestLayout();

Then in my custom view class I call onMeasure as follows:

   @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int wrapHeight = getWidth();
        int wrapWidth = getHeight();

        this.setMeasuredDimension(wrapHeight, wrapWidth);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

wrapHeight and wrapWidth are returned as zero. Could anyone offer some advice? I'm new to custom views etc so I'd appreciate the dig out ;)

****EDIT**** I should add that this is a slider, so in my custom view I am using the onscroll method...I imagine that might change things. So I have a frame image under which my view is scrolled, so I guess I need to set the height of my view to twice the frame height??

Can anyone help?

layout structure

layout structure

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • you have something wrong in your layout.xml, try first to replace com.test.bottomsup.custom_slider_library.sliderView with View, fix your layout file and finaly replace View with your custom view – pskink Mar 28 '14 at 13:33
  • 1
    can I ask how do you know there's something wrong with it? I have read that by default a custom view will take up the full screen also that when you are scrolling within the view the width and height will be returned as zero...thank you very much for your input...I will work on that suggestion – DJ-DOO Mar 28 '14 at 14:05
  • and what about a View? does it also take the full screen? where did you read that? – pskink Mar 28 '14 at 14:14
  • http://www.jayway.com/2012/12/12/creating-custom-android-views-part-4-measuring-and-how-to-force-a-view-to-be-square/ This is where I read that, scroll down to just under the second line chart image... – DJ-DOO Mar 28 '14 at 14:46
  • 1
    ok, so if you have a vertical linear layout and add a Button and then a custom view, does it mean that a Button will be hidden/covered by your view? i dont think so, it will simply fill the remaining space of the layout – pskink Mar 28 '14 at 14:54
  • ok, I'm not sure how helpful this is but at the moment, when I have the containing relative layout height set to wrap content it fills the screen (i know this as I have the background set to black) and the custom view and the image view are at the top of the device screen. I have button underneath (see edited question) and it remains in place but everything above the relative layout (and edit text, linear layout and surface view) are all either covered up or pushed up. – DJ-DOO Mar 28 '14 at 15:04
  • i dont see any Button nor ImageView, i see only one custom view inside relative layout – pskink Mar 28 '14 at 15:08
  • apologies...I have added my full xml...I also replaced the custom view with a regular view and I still get the same issue, the view fills the screen – DJ-DOO Mar 28 '14 at 15:11
  • so this is what i thought, the layout is wrong, but geez its too huge to me to understand id, sorry man... – pskink Mar 28 '14 at 15:14
  • ok that's no problem but can you tell me where the layout is wrong? – DJ-DOO Mar 28 '14 at 15:23
  • i dont know, i dont even know how it should look like... – pskink Mar 28 '14 at 15:26
  • then can I ask, and I'm not trying to be funny or anything but how do you know it's wrong? I'll create a jpeg to show you what it should look like – DJ-DOO Mar 28 '14 at 15:30
  • becaus if you use a View (not custom view, just base View) it shouldnt fill the whole layout, did you try hierarchyviewer, maybe it will help somehow – pskink Mar 28 '14 at 15:35
  • I'm not aware of that, I'll look at it, I've uploaded a basic image to show the structure which should help you understand the layout...thanks for your input – DJ-DOO Mar 28 '14 at 15:45
  • omg, it can be done with one linearlayout and one framelayout, advice: dont use that stupid graphical editor, make your layouts by hand – pskink Mar 28 '14 at 15:52
  • believe it or not...I did!! lol :D you see the surface view is a camera preview and I need if fullscreen and the rest of my attributes to overlay it...so only part of the camera preview is visible I always prefer to user Relative Layouts unless I'm lining up buttons or something as I feel it gives me more control as to what lies beneath what etc... – DJ-DOO Mar 28 '14 at 15:56
  • btw you can also use a tool called layoutopts, see http://developer.android.com/guide/topics/ui/declaring-layout.html – pskink Mar 28 '14 at 16:00

0 Answers0