52
<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 ?

Kayhan Asghari
  • 2,817
  • 1
  • 28
  • 47
mynameisanthpny
  • 679
  • 1
  • 8
  • 16
  • What is the code of your Activity? maybe the issue is there? – Sephy Jul 27 '10 at 13:16
  • 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)); } } – mynameisanthpny Jul 27 '10 at 13:22
  • I moved your code into the question, so it's more understandable – Federico klez Culloca Jul 27 '10 at 13:24
  • thanks klez, no body catched the error yet, must be some simple mistake :( – mynameisanthpny Jul 27 '10 at 13:35
  • 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 ? – mynameisanthpny Jul 27 '10 at 15:49
  • 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 faulty – mynameisanthpny Jul 28 '10 at 05:55
  • have you try setWillNotDraw (false) in constructor – Ajay Pandya Apr 06 '15 at 07:27
  • 1
    In my case when I set background then onDraw works otherwise not. – Mohammad Afrashteh Oct 23 '18 at 08:39

9 Answers9

122

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

Community
  • 1
  • 1
Dragan Marjanović
  • 2,386
  • 1
  • 19
  • 17
  • 20
    This applies to viewgroup not view – Prakash Nadar Sep 11 '12 at 20:46
  • 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? – SlashG Nov 02 '16 at 08:56
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.

Dmide
  • 6,422
  • 3
  • 24
  • 31
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 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 LinearLayout – Hossein Kurd Jun 23 '21 at 07:23
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).

pb2q
  • 58,613
  • 19
  • 146
  • 147
Gust Jc
  • 181
  • 1
  • 2
  • 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. – Someone Somewhere Mar 21 '13 at 05:15
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.

Kayhan Asghari
  • 2,817
  • 1
  • 28
  • 47
virajdasondi
  • 31
  • 1
  • 4
1

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

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
1

Probably

setMinimumHeight(width);
setMinimumWidth(height);

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

CoolMind
  • 26,736
  • 15
  • 188
  • 224
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(...);
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
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(); 
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
-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.

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
Biplab Kundu
  • 187
  • 1
  • 13