Custom View onDraw() is being called but not appearing

87 Views Asked by At

SOLVED: I fixed the problem, it had to do with the constraints in my layout.

I am trying to create a custom view that for the time being simply displays a chart. This is my custom View class:

public class ChartView extends View {

    private final Paint paint;

    public ChartView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        setWillNotDraw(false);
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d("View ", "onDraw called");

        int x = getWidth();
        int y = getHeight();

        int divX = x / 5;
        int divY = y / 7;

        for (int i = 0; i <= 5; i++) {
            canvas.drawLine(divX * i, 0, divX * i, y, paint);
            Log.d("View", "line drawn");
        }
        for (int i = 0; i <= 7; i++) {
            canvas.drawLine(0, divY * i, x, divY * i, paint);
            Log.d("View", "line drawn");
        }
    }
}

At first I assumed onDraw() must not ever have been called, but I added the log messages and it was in fact being called and each line in the chart was being drawn. I also have overridden onMeausure() so I don't think thats the problem.

Just to test if anything would be drawn at all I added in a test TextView which appeared just fine. I Then found that my chartView instance in the Main activity was null because I didn't call the parent constructor properly. I fixed that though and still nothing was drawn. However, when I fixed the problem with the parent constructor, the TextView also disappeared.

This is my Main Activity:

public class MainActivity extends AppCompatActivity {

    private ChartView chartView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent mainIntent = getIntent();
        boolean testPassed = mainIntent.getBooleanExtra("test", false);
        if (testPassed) {
            Log.d("Main", "intents test passed");
        }

        chartView = (ChartView) findViewById(R.id.chartView);

    }

and this is my layout XML file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context=".MainActivity">

    <Views.ChartView
        android:id="@+id/chartView"
        android:layout_width="425dp"
        android:layout_height="634dp"
        android:layout_marginEnd="601dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="260dp"
        android:layout_height="292dp"
        android:layout_marginTop="256dp"
        android:text="TextView"
        android:textSize="100dp"
        app:layout_constraintBottom_toTopOf="@+id/chartView"
        app:layout_constraintEnd_toStartOf="@+id/chartView"
        app:layout_constraintHorizontal_bias="0.582"
        app:layout_constraintStart_toEndOf="@+id/chartView"
        app:layout_constraintTop_toBottomOf="@+id/chartView"
        app:layout_constraintVertical_bias="0.801" />
</androidx.constraintlayout.widget.ConstraintLayout>

Any help would be appreciated.

0

There are 0 best solutions below