Custom onDraw() method not called
Asked Answered
P

9

53
<LinearLayout android:id="@+id/svLL" android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/sv"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <!--
            <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/scrollbar_2_text" />
        -->
        <com.mypackage.MyDrawableView
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
    </ScrollView>
</LinearLayout>

public class MyDrawableView extends View {

    Context thisContext;

    public MyDrawableView(Context context, AttributeSet attr) {
        super(context);
        thisContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(12);
        canvas.drawText("Blah blah", 0, 100, paint);
    }
}

public class MyActivity extends Activity {
    // Your member variable declaration here
    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Your code here super.onCreate(savedInstanceState);
        setContentView(R.layout.xmllayout);
        LinearLayout svll = (LinearLayout) findViewById(R.id.svLL);
        svll.setLayoutParams(new LinearLayout.LayoutParams(300, 300));
    }

} 

I am putting a breakpoint, but breakpoint is never hit inside onDraw() method, what's wrong ?

Pessary answered 27/7, 2010 at 13:8 Comment(8)
What is the code of your Activity? maybe the issue is there?Cima
public class MyActivity extends Activity { // Your member variable declaration here // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { // Your code here super.onCreate(savedInstanceState); setContentView(R.layout.xmllayout); LinearLayout svll = (LinearLayout) findViewById(R.id.svLL); svll.setLayoutParams(new LinearLayout.LayoutParams(300, 300)); } }Pessary
I moved your code into the question, so it's more understandableDaisie
thanks klez, no body catched the error yet, must be some simple mistake :(Pessary
interesting, i overrode onLayout and onMeasure. breakpoint was hit in these functions. but for onLayout the bottom is coming 0, right is 300 left and top are 0. is this the reason onDraw is not called ? is not there any customview example code using xml ?Pessary
when i made it fill_parent also same result. i tried doing it 300px and 300px in layout xml, also bottom is always coming 0 and onDraw is never called. i tend to assume that this way it is not gonna work and is faultyPessary
have you try setWillNotDraw (false) in constructorOnomatology
In my case when I set background then onDraw works otherwise not.Jump
A
124

Add setWillNotDraw(false) in MyDrawableView constructor. Original answer is here.

Alvy answered 17/5, 2012 at 23:33 Comment(2)
This applies to viewgroup not viewLemmuela
Worked for me. But there is a problem. I'm animating the View that I have clipped. When onDraw is overridden, the view flickers (looks like it was drawn again). When unclipped, it animates smoothly. Can you help me solve this?Darindaring
S
38

Your View has a height of 0. You set your View to have height=wrap_content, but your don't override onMeasure() to tell the UI toolkit how big your View is.

Shylashylock answered 27/7, 2010 at 17:25 Comment(1)
In my case it Not workes, LinearLayoutCompat >> HorizontalScrollView >> LinearLayout >> CustomView . when CustomView inherited Textview it there is no problem, but it faces a problem when it extends ConstrainLayout or LinearLayoutLop
J
18

You need to @Override the OnMeasure(int, int) method to specify the size of your view by calling setMeasuredDimension(int,int).

If you don't, the default method will set your view to have size width=0 height=0. And therefore don't even try to call onDraw at all.

The OnMeasure method calls setMeasuredDimension(int,int) which tells how big is your view.

(So you need to get the size of the android screen on the OnMeasure method and call setMeasuredDimension accordingly).

Josephus answered 15/10, 2012 at 4:0 Comment(1)
Indeed, when the dimensions are 0 then onDraw() isn't called. I was using getSuggestedMinimumWidth() which returned 0. I then used that with setMeasuredDimension() to make a square... needless to say, onDraw() wasn't called.Charlesettacharleston
L
3

Just make layout_width and layout_height of the custom view some fix at initial time instead of wrap_content or fill_parent. That worked for me.

Logging answered 5/5, 2012 at 9:14 Comment(0)
B
1

You need to use invalidate() in constructor method to get the onDraw Called.

Babu answered 29/1, 2013 at 8:14 Comment(0)
D
1

Probably

setMinimumHeight(width);
setMinimumWidth(height);

in constructor are not set or onMeasure() is not overriden.

Dorn answered 22/11, 2017 at 15:28 Comment(0)
P
1

Here is one more reason that your view might not be appearing. If you are creating your custom ViewGroup with subviews, remember to add the subviews to your ViewGroup.

In your ViewGroup constructor call

addView(mySubView);

Also remember to measure and layout your subviews in onLayout()

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mySubview.measure(...);
    mySubview.layout(...);
}
Prestonprestress answered 20/9, 2018 at 7:50 Comment(0)
L
0

After you set the custom view width, you should set scroll width.

FMChart = (HKFMDrawChart)findViewById(R.id.fm_chart);
FMChart.setWidth(xxxx);
float scrollViewWidth = FMChart.getScrellViewWidth(); 
FMChart.setLayoutParams(new LinearLayout.LayoutParams(
      (int) (scrollViewWidth + HKApplication.getScreenWidth() / 2),
       LinearLayout.LayoutParams.WRAP_CONTENT));
FMChart.postInvalidate(); // onDraw(); 
Lenalenard answered 3/4, 2015 at 10:3 Comment(0)
A
-2

Though I am new in android, still I believe the problem is because of

setContentView(R.layout.xmllayout); 

Instead of "R.layout.xmllayout", pass MyDrawableView object (using findViewById) and check. I have not tried, but hope, it will work.

Anselma answered 8/2, 2012 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.