1

I'm using a custom view which has a background picture and is used to draw stuff on it.

This view is ignoring android:layout_width="wrap_content and android:layout_height="wrap_content, it is not wrapping the background picture and is stretching and pushing the controls under the view out of the screen.

The custom view is in main LinearLayout, but where ever I put it, it behaves the same way.

I tried with setLayoutParams, but physical size is somehow still the same under visible controls and the size is important because of the coordinates I save for other times. It is even present under the custom title, and coordinates are returned as negatives and thats also a problem.

I don't like the idea of statically setting the size because of different screen sizes.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

<org.danizmax.tradar.MapView
android:id="@+id/mapView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
/>

<!-- koordinate -->
<LinearLayout android:id="@+id/controls"
android:orientation="horizontal"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:paddingLeft="10sp"
android:paddingRight="10sp">

    <TextView android:id="@+id/coordsText" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
    android:textStyle="bold"
    android:textSize="15sp"
    android:gravity="center"
    android:layout_weight="1">
    </TextView>

</LinearLayout>

<!-- radij -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:paddingBottom="10sp"
android:paddingLeft="-5sp"
android:paddingRight="-5sp">
    <Button android:text="" 
        android:id="@+id/ButtonInc"
        android:background="@drawable/leftbuttonani"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1">
        </Button>
    <TextView android:id="@+id/radiusSize" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:textStyle="bold"
        android:textSize="16sp"
        android:gravity="center"
        android:layout_weight="1"
        android:paddingLeft="0sp"
        android:paddingRight="0sp">
        </TextView>
    <Button android:text="" 
        android:id="@+id/ButtonDec"
        android:background="@drawable/rightbuttonani"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1">
        </Button>
</LinearLayout>

<!-- alert togle -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:paddingLeft="-5sp"
android:paddingRight="-5sp">
    <Button android:text="" 
        android:id="@+id/ButtonDec"
        android:background="@drawable/buttonani"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1">
        </Button>
    <ToggleButton
        android:id="@+id/toggleAlert"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textColor="#D8D8D8"
        android:background="@drawable/buttonani"
        android:textOn="@string/alertOn"
        android:textOff="@string/alertOff"
        android:textStyle="bold"
        android:layout_weight="0.5"
        android:paddingTop="1sp">
        </ToggleButton>
    <Button android:text="" 
        android:id="@+id/ButtonDec"
        android:background="@drawable/buttonani"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1">
        </Button>
</LinearLayout>

<!-- izpis zadnjega dogodka -->
<TextView android:id="@+id/timeStatText" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="10sp"
android:gravity="center">
</TextView>

This is how it should look, the blue part is MapView:

alt text

any idea?

danizmax
  • 2,446
  • 2
  • 32
  • 41
  • here it is, but where ever I put it, even on must ridiculous place it will be the same. – danizmax Dec 15 '10 at 17:49
  • try to join linearlayouts to an overall linear layout, like .... ... – mad Dec 15 '10 at 17:56
  • are you trying to put this custom mapview under the other controls ? what do you mean its "stretching and pushing the controls under the view out of the screen"? So MapView is the background picture you are talking about ? – slim Dec 15 '10 at 18:04
  • no, on the top of the controls and under the title bar ofc. Yes Mapview should take only as much space as background picture is resized, note that background picture has correct size and is only taking the upper part of the screen, the bottom is black where controls should be. – danizmax Dec 15 '10 at 18:07
  • I added a prototype picture of the app, there are missing two more buttons left and right of the toggle button but that is not important. – danizmax Dec 15 '10 at 18:18
  • and currently that blue part is not extending the full width and height as you have it doing in this picture ? Just trying to understand what your desired result is ? – slim Dec 15 '10 at 18:23
  • Sure, no prob :P Well blue part is the background which I resize in onDraw() to a proper size and is not bigger than you see now. The controls you see below are missing and there is emptiness. The emptiness is still the MapView, I know that because I still can draw on it. – danizmax Dec 15 '10 at 18:28

1 Answers1

3

The problem was solved by overriding onMeasure():

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

       int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
       int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
       if(parentWidth<parentHeight){
           this.setMeasuredDimension(parentWidth, (int)(parentWidth*(float)factor));
       }else{
           this.setMeasuredDimension((int)(parentHeight/(float)factor), parentHeight);
       }
    }

More detailed answer was found HERE.

Community
  • 1
  • 1
danizmax
  • 2,446
  • 2
  • 32
  • 41
  • @danixmax i am facing exactly same issue as yours. Can you explain me about factor in your code? – vinothp Nov 06 '14 at 19:52
  • @Vino It's loong time ago I did this, but I believe it was just a zoom factor for the component. For example if the factor was 0.5 the view would be half the size of its parent. As explained in the answer, my problem was that I forgot to override the method and therefore my code was not executed. – danizmax Nov 07 '14 at 06:31
  • thanks daniz. I tried overriding onmeasure method. Width is working fine but not the height(taking parent height).I am presenting this view in between two views as match_parent for both width and height. Not sure you have any idea about this? – vinothp Nov 07 '14 at 10:27
  • You should probably post a real question as it is hard to find the problem without the layout XML and a picture. – danizmax Nov 10 '14 at 07:23