Pages

Tuesday 22 October 2013

Dynamic Polymorphism

Poly means Many and Morphism means forms, and if this happens at run time, is called Dynamic Polymorphism.

Let's observe the following code

class Person{
  public void joiningDate(){
    System.out.println(" Person's Joining Date");
  }    
}

class Student extends Person{
  public void joiningDate(){
        System.out.println(" Student's Joining Date");
  }    
}

class Employee extends Person{
  public void joiningDate(){
         System.out.println(" Employee's Joining Date");
  }    
}



public class MainClass{
public static void mian(String args[]){
    Person p = new person();
    p.joiningDate();

    Student s =  new Student();
    p = s;
    p.joiningDate();

    Employee e  = new Employee();
    p = e;
    p.joiningDate();
  }
}


output:-
     Person's Joining Date
     Student's Joining Date
     Employee's Joining Date


Explanation: - In the above code Person is super class and both Student and Employees classes are childs of Person class. The method joiningDate() is overridden in both children classes.

 Student s =  new Student();  // Created an object of type Student
    p = s;                             // Assigned the child's object to parent
    p.joiningDate();               // invoking the child's function.

At runtime p references to the Student , so overridden joiningDate() method in Student class is class invoked. Similarly overridden joiningDate() method in Employee class is class invoked for 
    Employee e  = new Employee();
    p = e;
    e.joiningDate();

In all the three cases we are calling  p.joiningDate(); same method, But which joiningDate() method is to be called is decided at runtime dynamically (depends on address of the subclass?). This is called Dynamic Binding or Dynamic Polymorphism.

Dynamic Polymorphism is achieved through method Overriding.
Static Polymorphism is achieved through method Overloading.



Saturday 19 October 2013

Custom Fonts


Android allows us to use the Custom Fonts to use in android development.

Download custom font(.otf ) from the internet
Eg:- http://www.fontsquirrel.com/fonts/kaushan-script (KaushanScript-Regular.otf)

Create  an Android Application Project
Create an assets folder in the project and add downloaded KaushanScript-Regular.otf .




MainActivity.java

package com.example.customfonts;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv = (TextView) findViewById(R.id.textView1);
        
        Typeface tp1 = Typeface.createFromAsset(getAssets(), "KaushanScript-Regular.otf");
        tv.setTypeface(tp1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Typeface class specifies the typeface and intrinsic style of a font. This is used in the paint along with optionally paint settings like textsize, textskewx, textscaleX to specify how text appears when drawn.

Typeface.createFromAsset(AssetManager mgr, String path)  creates a new typeface from the specified font data.
Parameter
    mgr --> The application's Asset manager
    path --> The file name of the font data in the assets directory

http://developer.android.com/reference/android/graphics/Typeface.html

setTypeface() sets the typeface to the corresponding textview.

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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        android:text="TextView"
        android:textSize="@dimen/textsize" />

</RelativeLayout>





Android Abbrevations


ADT    -- Android Development Tool

ADB    -- Android Debug Bridge

DDMS  -- Dalvik Debug Monitor Service

URI     --  Universal Re3source Identifier

API     --  Applicaqtion Program Interface

JNI     -- Java Native Interface

MIME   --  Multipurpose Internet Mail Extensions

AAPT   --  Android Asset Packaging Tool

ANR    --  ApplicATION Not Responding

APK    --  Android Packaging Key

BLOB  --  Binary Large Object



Friday 18 October 2013

Graphs using Achart Engine

Achart-Engine is software library for android devices to draw different types of charts. It supports Pie chart, Bar chart, Line chart, scatter chart, time chart.
http://www.achartengine.org/content/demo.html.

Download the achart engine jar from official site http://repository-achartengine.forge.cloudbees.com/snapshot/org/achartengine/achartengine/1.1.0/  or https://code.google.com/p/achartengine/downloads/detail?name=achartengine-1.1.0.jar&can=2&q=  or https://code.google.com/p/achartengine/downloads/list

Create Android Application project by using Eclipse IDE and add the downloaded jar to its build path.
Adding Achartengine jar to BuildPath
     1). Right click on the project
     2). Go to properties
     3). Click on the Java Build Path on the left panel
     4). Click on the Libraries on the right panel (Top Tabs)
     5). Add the jar by clicking the Add External Jar button
     6). Click on the Order and Export on the right panel (Top Tabs) and then                  select checkbox of achartengine jar.
     7). Click OK.



Example to draw Pie-chart using Achart-Engine.
Task:-  Launching screen of the application will have one button. on clicking the button a pie chart is displayed.

MainActivity.java

package com.example.chart1;

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 MainActivity extends Activity implements OnClickListener {
Button chartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chartButton= (Button) findViewById(R.id.chart_button);
chartButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.chart_button:
PieChart1 effort = new PieChart1 ();
Intent effortIntent = effort.getIntent(this);
startActivity(effortIntent);
break;
}
}

}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    >
    <Button
        android:id="@+id/chart_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Charts" />

</LinearLayout>


PieChart1.java

package com.example.chart1;
import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class PieChart1{
public Intent getIntent(Context context){
int []Projects = {30, 15, 15, 30,10};  
CategorySeries series = new CategorySeries("pie"); 

series.add("Project1",Projects[0]);            
series.add("Project2",Projects[1]);
series.add("Project3",Projects[2]);
series.add("Project4",Projects[3]);
series.add("Project5",Projects[4]);

// Set colors for Projects respectively           
int []colors = new int[]{Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN};
// set style for series
DefaultRenderer dRenderer = new DefaultRenderer();
for(int color : colors){
SimpleSeriesRenderer sRenderer = new SimpleSeriesRenderer();
sRenderer.setColor(color);
sRenderer.setDisplayBoundingPoints(true);

sRenderer.setDisplayChartValuesDistance(5);

sRenderer.setDisplayChartValues(true);
sRenderer.setChartValuesTextSize(15);
dRenderer.addSeriesRenderer(sRenderer);
}
dRenderer.isInScroll();
dRenderer.setZoomButtonsVisible(true);  
//set zoom button in Graph
dRenderer.setApplyBackgroundColor(true);
dRenderer.setBackgroundColor(Color.BLACK); 
//set background color
dRenderer.setChartTitle("Projects");
dRenderer.setChartTitleTextSize((float) 30);
dRenderer.setShowLabels(true);  
dRenderer.setLabelsTextSize(20);
dRenderer.setLegendTextSize(25);
dRenderer.setDisplayValues(true);
return ChartFactory.getPieChartIntent(context, series, dRenderer, "PieChart");
}

}


AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chart1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="org.achartengine.GraphicalActivity"/>    
    </application>


</manifest>








Friday 11 October 2013

Building and Run process


Build

During the build process, your Android projects are compiled and
packaged into an .apk file, the container for your application
binary. It contains all of the information necessary to run your
application on a device or emulator, such as compiled .dex files (.class files converted to Dalvik byte code), a binary version of the Androidmanifest.xml file, compiled resources and uncompiled resource files for your application.