Blog purpose for android basic example for android app developer. any query please my contact

Saturday, 25 February 2023

android studio view example app

 android studio view example app


There may be many times where instead of using android custom view u would require to create your own view. Creating new Views gives you the power to fundamentally shape the way your applications look and feel. By creating your own controls, you can create UIs that are uniquely suited to your needs. To create new controls from a blank canvas, you extend either the View or SurfaceView class. The View class provides a Canvas object with a series of draw methods and Paint classes. Use them to create a visual interface with bitmaps and raster graphics. You can then override user events, including screen touches or key presses to provide interactivity.This class teaches u to create your own view by extending from the View class in android.



1.MainActivity.java:

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

 

public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button start = (Button)findViewById(R.id.button1);

        start.setOnClickListener(new View.OnClickListener() {

 

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                startActivity(new Intent("com.example.paintapp.paint"));

            }

        });

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

 

    @Override

    protected void onPause() {

        // TODO Auto-generated method stub

        super.onPause();

        finish();

    }

 

}


2.paint.java:


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class paint extends Activity implements OnClickListener {

 

    paint_draw var;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.paint);

        Button red = (Button) findViewById(R.id.red);

        Button blue = (Button) findViewById(R.id.blue);

        Button black = (Button) findViewById(R.id.black);

        red.setOnClickListener(this);

        blue.setOnClickListener(this);

        black.setOnClickListener(this);

    }

 

    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.black:

            startActivity(new Intent("com.example.paintapp.black_rect"));

            break;

        case R.id.blue:

            startActivity(new Intent("com.example.paintapp.blue_rect"));

            break;

        case R.id.red:

            startActivity(new Intent("com.example.paintapp.red_rect"));

            break;

        }

    }

 

}



3.black_rect.java:


import android.app.Activity;

import android.os.Bundle;

 

public class black_rect extends Activity{

 

    paint_draw var;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        var = new paint_draw(this);

        var.method("black");

        setContentView(var);

    }

 

}


4.paint_draw.java:



import android.annotation.SuppressLint;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.view.View;

 

public class paint_draw extends View{

 

    String color,shape;

    Paint blue =new Paint();

 

    public paint_draw(Context context) {

        super(context);

        // TODO Auto-generated constructor stub

    }

 

    public void method(String color){

      if(color=="blue"){

          blue.setColor(Color.BLUE);

      }

      if(color=="red"){

         blue.setColor(Color.RED);

      }

      if(color=="black"){

          blue.setColor(Color.BLACK);

      }    

    }

    @SuppressLint("DrawAllocation")

    @Override

    protected void onDraw(Canvas canvas) {

        // TODO Auto-generated method stub

        super.onDraw(canvas);

        Rect myrect = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()/2);

        blue.setStyle(Paint.Style.FILL);

        canvas.drawRect(myrect, blue);

    }

 

}


5.blue_rect.java:


import android.app.Activity;

import android.os.Bundle;

 

public class blue_rect extends Activity{

 

    paint_draw var;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        var = new paint_draw(this);

        var.method("red");

        setContentView(var);

    }

 

}


6.red_rect.java:

import android.app.Activity;

import android.os.Bundle;

 

public class red_rect extends Activity {

 

    paint_draw var;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        var = new paint_draw(this);

        var.method("red");

        setContentView(var);

    }

 

}



7.activity_main.xml:

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

    xmlns:tools="http://schemas.android.com/tools"

    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=".MainActivity" >

 

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="80dp"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="190dp"

        android:text="Start Paint Activity"

        android:textDirection="ltr"

        android:textAlignment="center" 

        android:textSize="20dp"/>

 

</RelativeLayout>


8.paint_main.xml:


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

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

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="vertical" >

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="66dp"

        android:text="Choose an Option"

        android:textSize="40dp"

        android:textAppearance="?android:attr/textAppearanceLarge" />

 

    <Button

        android:id="@+id/red"

        android:layout_marginTop="80dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Red" />

 

    <Button

        android:id="@+id/blue"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Blue" />

 

    <Button

        android:id="@+id/black"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Black" />

 

</LinearLayout>


9.AndroidManifest.xml:


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

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

    package="com.example.paintapp"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="17" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.example.paintapp.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity

            android:name="com.example.paintapp.paint"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="com.example.paintapp.paint" />

 

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

 

        <activity

            android:name="com.example.paintapp.red_rect"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="com.example.paintapp.red_rect" />

 

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <activity

            android:name="com.example.paintapp.blue_rect"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="com.example.paintapp.blue_rect" />

 

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <activity

            android:name="com.example.paintapp.black_rect"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="com.example.paintapp.black_rect" />

 

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>


No comments:

Post a Comment