Sunday, 2 December 2012

Get all information in a android phone

                         Get all information in a android phone





*#*#4636#*#*

Saturday, 1 December 2012

XML Parsing in Android


                       XML Parsing in Android



Xml stands for extensible markup language.There are  types of parsing methods in android

1 SaxPullparser
2 DOM parser

Here we are using SaxPullparser ,because it is more efficient that any other parser in android.

Following is a example for XML parsing using  SaxPullparser 



CustomXML.java

package fortyonepost.com.cx;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import teste.com.teste.R;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CustomXML extends Activity 
{
//this object will store a reference to the Activity's resources
private Resources resources;
//this integer will store the XMLResourceParser events
private int event;
//a boolean that will tell if it's the tag we are looking for
private boolean isCorrectTag = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //get the application's resources
        resources = this.getResources();
        
        try 
        {
         GetAttributes("tutorial");
         GetAttributes("level1");
        catch (XmlPullParserException e) 
{
         //Display a toast message.
Toast.makeText(this, "Could not retrieve the current parser event", Toast.LENGTH_SHORT);
        catch (IOException e) 
{
         //Display a toast message.
Toast.makeText(this, "Could not read XML file", Toast.LENGTH_SHORT);
}
    }
    
    public void GetAttributes(String tagName) throws XmlPullParserException, IOException
{
     //get a XmlResourceParser object to read contents of the 'custom.xml' file
XmlResourceParser parser = resources.getXml(R.xml.custom);
//get the initial parser event
event = parser.getEventType();
//while haven't reached the end of the XML file 
    while (event != XmlPullParser.END_DOCUMENT) 
    {
     //get the type of the event
     event = parser.getEventType();
    
     //if parser finds a new tag and its name equals 'tagName'
            if (event == XmlPullParser.START_TAG && parser.getName().contentEquals(tagName)) 
            {
             //print the attribute values at LogCat
             //print the ID
             Log.i("CustomXML", "id:" + parser.getIdAttribute());
            
             //print the difficulty
             float difficulty = parser.getAttributeFloatValue(null, "difficulty", 0.0f);
             Log.i("CustomXML", "difficulty:" + Float.toString(difficulty));
            
             //print the number of enemies
             int enemies = parser.getAttributeIntValue(null, "enemies", 0);
             Log.i("CustomXML", "enemies:" + Integer.toString(enemies));

             //Found the tag, set the boolean to true
             isCorrectTag = true;
            } 
            else if (event == XmlPullParser.TEXT && isCorrectTag)
            {
             //Print the tag's content
             Log.i("CustomXML", parser.getText());
             //set isCorrectTag to false;
             isCorrectTag = false;
             //break the loop, we already found what we were looking for
             break;
            }
            //go to the next tag
            parser.next();
    }
    //Release resources associated with the parser
    parser.close();
}
}


place a xml file in res-xm1-custom.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
<tutorial id="level" difficulty="1.5" enemies="3">The tutorial.</tutorial>
<level1 id="level" difficulty="2.6" enemies="10">The first level.</level1>
</resources>


check the logcat in your emulator for output  

 
http://androiddeveloperszone.blogspot.in/

Android Custom Spinner with Intent


          Android Custom Spinner with Intent  


 Following is a example program for custom spinner in android and user can select a particular item and leads to specific activity.


NewspinnerActivity.java

package adsdfd.ghgh;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
 
public class NewspinnerActivity extends Activity {
    String[] strings = {"CoderzHeaven","Google",
            "Microsoft", "Apple", "Yahoo","Samsung"};
 
    String[] subs = {"Heaven of all working codes ","Google sub",
            "Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};
 
 
    int arr_images[] = { R.drawable.icon,
                         R.drawable.icon, R.drawable.icon,
                         R.drawable.icon, R.drawable.icon, R.drawable.icon};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyAdapter(NewspinnerActivity.this, R.layout.row, strings));
    }
 
    public class MyAdapter extends ArrayAdapter<String>{
 
        public MyAdapter(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }
 
        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
            
            
           
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          System.out.println("abcdddddddddddd"+position);
          
          
          Intent i=new Intent(getApplicationContext(),abcd.class);
          startActivity(i);
          
          
          
            return getCustomView(position, convertView, parent);
            
        }
 
        public View getCustomView(int position, View convertView, ViewGroup parent) {
 
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.company);
            label.setText(strings[position]);
 
            TextView sub=(TextView)row.findViewById(R.id.sub);
            sub.setText(subs[position]);
 
            ImageView icon=(ImageView)row.findViewById(R.id.image);
            icon.setImageResource(arr_images[position]);
 
            return row;
            }
        }
   }

then following are the xml  file and in main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
      android:background="@drawable/icon"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/prompt"
    />
</LinearLayout>


then the row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>
    <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/icon"/>
    <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="3dip"
         android:layout_marginTop="2dip"
         android:textColor="@drawable/red"
         android:textStyle="bold"
         android:id="@+id/company"
         android:text="CoderzHeaven"
         android:layout_marginLeft="5dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="2dip"
         android:textColor="@drawable/darkgrey"
         android:layout_marginLeft="5dip"
         android:id="@+id/sub"
         android:layout_below="@+id/company"
         android:text="Heaven of all working codes"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</RelativeLayout>


abcd.class

package adsdfd.ghgh;

import android.app.Activity;
import android.os.Bundle;

public class abcd extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abcd);
}

}







Download Android Codes From SkyBlueAndroid

use hex color codes instead of color names in Java









  paint.setColor(Color.parseColor("#9d4807"));


click here

How to start a Activity from Service in Android

 
                     How to start a Activity from Service in Android

 within the service


                        Intent dialogIntent = new Intent(getBaseContext(), tone1.class);
  dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  getApplication().startActivity(dialogIntent);