Jeanius
Jeanius

Reputation: 21

Custom views added to horizontal scrollview do not appear when scrollview is larger than screen

I really hope you can help! I have been banging my head against the wall for ages now.

The centre of my app is made up of a linear layout, divided into two. The top layout is a horizontal scroll view. This scroll view holds a relative layout which, in turn, holds a view "theLine" - just a line across the screen.

Dynamically, I add image buttons to the relative layout, in relation to theLine. These image buttons are connected by "DrawArrow", a custom view class. This just creates a line running through the buttons.

All works fine, until the scroll view becomes "activated" - ie, the imagebuttons added are too large in width for the screen. Then, "theLine" disappears, and the "DrawArrows" disappear. The image buttons do not disappear. I have noticed that onDraw in the "DrawArrows" is not called.

I have no idea why, and I could really do with some help.

Applicable XML

<LinearLayout 
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_toRightOf="@id/ScrollVertical"
android:weightSum="100"
android:baselineAligned="false">

    <HorizontalScrollView
        android:id="@+id/scrollHorizontal"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:fillViewport="true"
        android:layout_weight="85"
        android:layout_gravity="center">

            <RelativeLayout
                android:id="@+id/actionFrame"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left">

                    <View
                        android:id="@+id/theLine"
                        android:layout_width="match_parent"
                        android:layout_height="2dip"
                        android:layout_centerInParent="true"
                        android:background="@drawable/linegradient"
                        android:padding="0dp">
                    </View> 

            </RelativeLayout>

    </HorizontalScrollView>     

Java Draw Arrow

public DrawArrow(Context context, View startView, View endView) {
    super(context);   
    paint.setStrokeWidth(5);
    paint.setStyle(Paint.Style.STROKE); 
    start = startView;
    end = endView;
}

public void onDraw(Canvas canvas) {

    paint.setColor(Color.BLACK);     
    canvas.drawLine(start.getX() + (start.getWidth()/2), start.getY()+ (start.getHeight()/2), end.getX() + (end.getWidth()/2), end.getY() + (end.getHeight()/2), paint);
}   

Java (Where buttons/connections are added)

RelativeLayout container = (RelativeLayout) findViewById(R.id.actionFrame);
//Create button
container.addView(button);

DrawArrow connArr = new DrawArrow(this, findViewById(instruction.getPred().getId()), findViewById(currentPosition));  //Instruction is connected with button - they share an ID
container.addView(connArr);
}

Note: Button, depending on if it is the first or >first added, has different layout params.

All are WRAP_CONTENT, either ALIGN_TOP/ALIGN_BOTTOM to "theLine" and, if first, ALIGN_PARENT_LEFT, or if >first, RIGHT_OF the previous button.

Upvotes: 0

Views: 431

Answers (1)

Jeanius
Jeanius

Reputation: 21

In case anyone has this issue -

My problem was I had not overridden the onMeasure method within DrawArrow, so once the parent layout changed size, due to the scrolling, the DrawArrow was being added with 0 width.

All I had to do was pass the parent layout to DrawArrow in its constructor and, then, within onMeasure, setMeasuredDimension() to the parent width and size.

Upvotes: 2

Related Questions