Monday, December 18, 2023

Q.8) write a android program to used View.

 activity.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <com.example.customview.customview

        android:id="@+id/customview"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


</RelativeLayout>


customview.java

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.View;


public class customview extends View {


    private Paint paint;


    public customview(Context context) {

        super(context);

        init();

    }


    public customview(Context context, AttributeSet attrs) {

        super(context, attrs);

        init();

    }


    private void init() {

        paint = new Paint();

        paint.setColor(Color.BLUE);

        paint.setStyle(Paint.Style.FILL);

    }


    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);


        // Draw a colored rectangle

        canvas.drawRect(100, 100, getWidth() - 100, getHeight() - 100, paint);

    }

}


No comments:

Post a Comment