Monday, 25 December 2017

Create sample application that draw the circle, oval shape and square

Create sample application that draw the circle, oval shape and square.



Code:


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pk.drawshapes">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jayesh.drawshapes.MainActivity">

    <Button
        android:text="SQUARE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="12dp"
        android:id="@+id/btn_square" />


    <Button
        android:text="CIRCLE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/btn_circle"
        android:layout_alignParentTop="true" />

    <Button
        android:text="RECTANGLE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="15dp"
        android:id="@+id/btn_rect"
        android:layout_alignParentTop="true" />

    <Button
        android:text="OVAL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_square"
        android:layout_alignEnd="@+id/btn_square"
        android:layout_marginTop="23dp"
        android:id="@+id/btn_oval" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_input_add"
        android:layout_below="@+id/btn_oval"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="152dp"
        android:id="@+id/iv" />
</RelativeLayout>

MainActivity.java

package com.jayesh.drawshapes;

import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.Shape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private Button btn_square,btn_circle,btn_rect,btn_oval;
    ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_square = (Button)findViewById(R.id.btn_square);
        btn_circle = (Button)findViewById(R.id.btn_circle);
        btn_rect = (Button)findViewById(R.id.btn_rect);
        btn_oval = (Button)findViewById(R.id.btn_oval);
        iv = (ImageView)findViewById(R.id.iv);

        btn_square.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShapeDrawable square  = new ShapeDrawable(new RectShape());
                square.setIntrinsicHeight(500);
                square.setIntrinsicWidth(500);
                square.getPaint().setColor(Color.BLUE);
                iv.setImageDrawable(square);
            }
        });
        btn_rect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShapeDrawable rect = new ShapeDrawable(new RectShape());
                rect.setIntrinsicHeight(500);
                rect.setIntrinsicWidth(250);
                rect.getPaint().setColor(Color.BLACK);
                iv.setImageDrawable(rect);
            }
        });

        btn_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShapeDrawable circle = new ShapeDrawable(new OvalShape());
                circle.setIntrinsicHeight(500);
                circle.setIntrinsicWidth(500);
                circle.getPaint().setColor(Color.GREEN);
                iv.setImageDrawable(circle);
            }
        });

        btn_oval.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShapeDrawable oval = new ShapeDrawable(new OvalShape());
                oval.setIntrinsicHeight(500);
                oval.setIntrinsicWidth(300);
                oval.getPaint().setColor(Color.YELLOW);
                iv.setImageDrawable(oval);
            }
        });
    }

}


OUTPUT:


No comments:

Post a Comment