Monday 29 July 2013

XML processing in Android

The Java programming language provides several standard libraries for processing XML files.
 The SAX and the DOM XML parsers are also available on Android.

The SAX and DOM parsers API is on Android the same as in standard Java.
SAX and DOM have their limitations, therefore it is not recommended to use
them on Android. Therefore this tutorial does not give an example for the usage
of this library.

The Java standard provides also the Stax parser. This parser is not part of the
Android platform.

Android provides for XML parsing and writing the XmlPullParser class.
This parser is not available in standard Java but is similar to the Stax parser.
The parser is hosted at http://www.xmlpull.org/ .

On Android it is recommended to use the XmlPullParser.
It has a relatively simple API compared to SAX and DOM and is fast and requires
less memory then the DOM API.

-->There are two key methods: next() and nextToken(). While next() provides access
to high level parsing events, nextToken() allows access to lower level tokens.

-->The current event state of the parser can be determined by calling the
 getEventType() method. Initially, the parser is in the START_DOCUMENT state.

-->The method next() advances the parser to the next event.
 The int value returned from next determines the current parser state and is identical
to the value returned from following calls to getEventType ().

The following event types are seen by next():
---------------------------------------------
START_TAG
An XML start tag was read.

TEXT
Text content was read; the text content can be retrieved using the getText() method.
(when in validating mode next() will not report ignorable whitespace, use nextToken() instead)

END_TAG
An end tag was read

END_DOCUMENT
No more events are available

Example XmlPullParser:
 import java.io.IOException;
 import java.io.StringReader;

 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException.html;
 import org.xmlpull.v1.XmlPullParserFactory;

 public class SimpleXmlPullApp
 {

     public static void main (String args[])
         throws XmlPullParserException, IOException
     {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.END_DOCUMENT) {
              System.out.println("End document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }
     }
 } 

Saturday 27 July 2013

Number Picker Dialog

Number Picker Dialog by Anusha Reddy

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

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="78dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="40dp"
        android:text="Show Selected Number" />

</RelativeLayout>

MainActivity.java:

package com.anu.numberpicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

public class MainActivity extends Activity {
NumberPicker np;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

np = (NumberPicker) findViewById(R.id.numberPicker1);

np.setMaxValue(10);
np.setMinValue(1);

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

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int number = np.getValue();

Toast.makeText(getApplicationContext(),
"value is :" + number, 3000).show();

}
});

}

}





Wednesday 24 July 2013

Killing entire Application

Here i'm giving example to kill entire application.

Project name : KillApplication
Package name : com.ram.killapplicaiton
Layout files: activity1.xml, activity2.xml, activity3.xml, activity4.xml
Java files: Activity1.java, Activity2.java, Activity3.java, Activity4.java

ScreenShots:






activity1.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=".Activity1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Activity1"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button_callActivity2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="96dp"
        android:text="Call Activity2" />

</RelativeLayout>

activity2.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=".Activity2" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Activity2"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button_callActivity3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="Call Activity3" />


</RelativeLayout>

activity3.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=".Activity3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Activity3"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button_callActivity4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp"
        android:text="Call Activity4" />

</RelativeLayout>

activity4.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=".Activity4" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Activity4"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button_exitApplication_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="Exit Applicaiton" />

</RelativeLayout>

Activity1.java:
package com.ram.killapplication;

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 Activity1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);

Bundle bundle = this.getIntent().getExtras();
        if(bundle!= null)
        {
            boolean isActivityToBeFinish = this.getIntent().getExtras().getBoolean("finishstatus");
            if(isActivityToBeFinish)
            {
                finish();
                }
    }

Button b = (Button)findViewById(R.id.button_callActivity2);

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), Activity2.class));
}
});
}


}

Activity2.java:
package com.ram.killapplication;

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 Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button b = (Button)findViewById(R.id.button_callActivity3);

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), Activity3.class));
}
});
}

 


}

Activity3.java:
package com.ram.killapplication;

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 Activity3 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);

Button b = (Button)findViewById(R.id.button_callActivity4);

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), Activity4.class));
}
});
}

 


}

Activity4.java:
package com.ram.killapplication;

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 Activity4 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity4);

Button b = (Button)findViewById(R.id.button_exitApplication_button);

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Activity4.this,Activity1.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("finishstatus", true);
                startActivity(intent);
                finish();
}
});
}

 


}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.killapplication"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        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.ram.killapplication.Activity1"
            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.ram.killapplication.Activity2"
            android:label="@string/title_activity_activity2" >
        </activity>
        <activity
            android:name="com.ram.killapplication.Activity3"
            android:label="@string/title_activity_activity3" >
        </activity>
        <activity
            android:name="com.ram.killapplication.Activity4"
            android:label="@string/title_activity_activity4" >
        </activity>
    </application>


</manifest>