Hi Friends, if you like my blog please give your valuable comments it will help to improve my blog content and enthusiasm to write a lot in android World.

Wednesday, October 31, 2012

Displaying Chart On Android


In this tutorial, we are going to learn, how to display chart or bar graphs on android for your statistical data.
To begin with the tutorial, lets create new project.
FILE–>NEW–>Android Project
Now right click on the project in Package explorer and go to Properties.
Properties–>Java Build Path–>Libraries–>Add external JARs..

Once your API is linked, you are ready now for coding your program. Here is the link where from you can download the API http://www.kidroid.com/kichart/

Now lets code the program. This is the complete code of android chart sample.

package com.google.android.chartView;
import com.kidroid.kichart.model.Aitem;
import com.kidroid.kichart.view.LineView;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class chartView extends Activity {
/** Called when the activity is first created. */
LineView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String xaxis[]=new String[4];
xaxis[0]=”2006″;
xaxis[1]=”2007″;
xaxis[2]=”2008″;
xaxis[3]=”2009″;
float line1[]=new float[4];
line1[0]=120;
line1[1]=240;
line1[2]=500;
line1[3]=100;
float line2[]=new float[4];
line2[0]=100;
line2[1]=650;
line2[2]=700;
line2[3]=300;
float line3[]=new float[4];
line3[0]=50;
line3[1]=180;
line3[2]=360;
line3[3]=900;
Aitem items[]=new Aitem[3];
items[0]= new Aitem(Color.BLUE,”pauOut”,line1);
items[1]= new Aitem(Color.GREEN,”pauOut”,line2);
items[2]= new Aitem(Color.YELLOW,”pauOut”,line3);
lv=new LineView(this);
lv.setTitle(“Yearly Budget”);
lv.setAxisValueX(xaxis);
lv.setItems(items);
setContentView(lv);
}
}
here is the final view of the chart module. After this demo, you should get stuff like this.

Friday, October 26, 2012

Shake Animation Example


This example let you shake or animation your view on some event described.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it ShakeAnimationExample.
2.) You will see some default code into your main.xml, strings.xml and android manifest file.
3.) Write following into main.xml file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
     
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="Please enter your password:"
    />
     
    <EditText android:id="@+id/pw"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:singleLine="true"
        android:password="true"
    />
 
    <Button android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
    />
 
</LinearLayout>
4.) Create and Write following into anim/shake.xml:
1
2
3
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0" android:toXDelta="10" android:duration="1000"
    android:interpolator="@anim/cycle_7" />
5.) Create and Write following into anim/cycle_7.xml:
1
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />
Steps:
1.) Create a project named ShakeAnimationExample and set the information as stated in the image.
Build Target: Android 1.6
Application Name: ShakeAnimationExample
Package Name: com.example. ShakeAnimation
Activity Name: ShakeAnimationExample
Min SDK Version: 4
2.) Open ShakeAnimationExample.java file and write following code there:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.ShakeAnimation;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
 
public class ShakeAnimation extends Activity implements View.OnClickListener {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        View loginButton = findViewById(R.id.login);
        loginButton.setOnClickListener(this);
    }
 
    public void onClick(View v) {
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        findViewById(R.id.pw).startAnimation(shake);
        Toast.makeText(this, "Wrong Password", Toast.LENGTH_SHORT).show();
    }
 
}      
3.) Compile and build the project.
4.) Run on 1.6 simulator for the output.