Powered By Blogger

Monday, 12 March 2012

Networking Concepts


package com.networkingex;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NetworkingEx extends Activity {
    /** Called when the activity is first created. */
ImageView img;
   
private InputStream openHttpConnection(String urlstring)
throws IOException
{
InputStream in=null;
int response=-1;
URL url=new URL(urlstring);
URLConnection con=url.openConnection();
if(!(con instanceof URLConnection))
throw new IOException("NOT AN HTTP CONNECTION");
try{
HttpURLConnection httpcon=(HttpURLConnection)con;
httpcon.setAllowUserInteraction(false);
httpcon.setInstanceFollowRedirects(true);
httpcon.setRequestMethod("GET");
httpcon.connect();
response=httpcon.getResponseCode();
if(response==HttpURLConnection.HTTP_OK)
{
in=httpcon.getInputStream();
}

}catch(Exception e)
{
e.printStackTrace();
}

return in;

}
private Bitmap downloadImage(String url)
{
Bitmap bitmap=null;
InputStream in=null;
try
{
//in=openHttpConnection(url);
bitmap=BitmapFactory.decodeStream(openHttpConnection(url));
//in.close();
}catch(IOException e)
{
Toast.makeText(this, e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();

}
return bitmap;

}
private String downloadfile(String url)
{
int buffersize=200;
InputStream in=null;
try
{
in=openHttpConnection(url);
}catch(IOException e)
{
Toast.makeText(this,e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
InputStreamReader inr=new InputStreamReader(in);
int charread =0;
String str="";
char[] inputbuffer=new char[buffersize];
try
{
while((charread==inr.read(inputbuffer)))
{
String readline=String.copyValueOf(inputbuffer, 0,charread);
str+=readline;
inputbuffer=new char[buffersize];
}
in.close();
}catch(IOException e)
{
Toast.makeText(this,e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return url;

}
private String worddefinition(String word)
{
InputStream in=null;
String strDefinition="";
try
{
in=openHttpConnection("http://services.aonaware.com/DictService/DictService.asmx/Define?word="+word);
   Document doc=null;
   DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
   DocumentBuilder db;
   try
   {
    db=dbf.newDocumentBuilder();
    doc=db.parse(in);
   
   }catch(ParserConfigurationException e)
   {
    e.printStackTrace();
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
   doc.getDocumentElement().normalize();
   NodeList definitionElements=doc.getElementsByTagName("Definition");
 
 
  // NodeList definitionElements = null;
for(int i=0;i<definitionElements.getLength();i++)
   {

System.out.println("Lenght of Defintion:"+definitionElements.getLength());
Node item=definitionElements.item(1);
if(item.getNodeType()==Node.ELEMENT_NODE)
{
NodeList nodeList=item.getChildNodes();
strDefinition=nodeList.item(5).getTextContent();
/*//Element defElement=(Element)item;
NodeList wordDefinition=doc.getElementsByTagName("WordDefinition");
strDefinition="";
for(int j=0;j<wordDefinition.getLength();j++)
{
Element wordDefinitionElement =
(Element) wordDefinition.item(j);
NodeList textNodes =
((Node) wordDefinitionElement).getChildNodes();
strDefinition +=
((Node) textNodes.item(0)).getNodeValue() + ".";
}*/
/*NodeList nodeList=item.getChildNodes();
System.out.println("Lenght of Defintion:"+nodeList.getLength());

//System.out.println("Name::::::::::::::::2"+nodeList.item(2).getNodeName());
//System.out.println("Name:::::::::::::::3:"+nodeList.item(3).getNodeName());
//System.out.println("Name::::::::::::::::4"+nodeList.item(4).getNodeName());

Toast.makeText(getApplicationContext(),"Meaning is"+nodeList.item(5).getTextContent() , Toast.LENGTH_LONG).show();
//System.out.println("Name::::::::::::::::5"+nodeList.item(5).getTextContent());
*/
}
//return strDefinition;


   // NodeList itemnode= definitionElements.item(1);
   // System.out.println("Lenght of Defintion:"+itemnode.getLength());
    /*System.out.println("title:ln"+itemnode.getNodeName());
    System.out.println("title:nn"+itemnode.getNodeName());
    System.out.println("title:nv"+itemnode.getNodeValue());
    System.out.println("title:tc"+itemnode.getTextContent());*/
    /*if(itemnode.getNodeType()==Node.ELEMENT_NODE)
    {
    Log.d("mg","value is"+strDefinition);
    try
    {
    Element definitionelement=(Element)itemnode;
    }catch(Exception e)
    {
    Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
    }
   
    for(int j=0;j<wordDefinitionElements.getLength();j++)
    {
    Element wordelement=(Element)wordDefinitionElements.item(j);
    NodeList textnode=((Node)wordelement).getChildNodes();
    if(itemnode.getNodeName().equals("WordDefinition"))
    {
    strDefinition=itemnode.getNodeName();
    Log.d("msg is", "value is"+strDefinition);
    }
    //}
    Toast.makeText(getApplicationContext(),strDefinition, Toast.LENGTH_LONG).show();
    }*/



   
}
 



}catch(Exception e)
{
//Toast.makeText(getApplicationContext(),e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return strDefinition;
}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bitmap = null;
        try
        {
         bitmap=downloadImage("http://cars.88000.org/wallpapers/80/Porsche_911_Police_Car.jpg");
        }catch(Exception e)
        {
        e.printStackTrace();
        }
       
       
        img=(ImageView)findViewById(R.id.imageView1);
        img.setImageBitmap(bitmap);
        /*String str="";
        try
        {
         str=downloadfile("http://appleinsider.com.feedsportal.com/c/33975/f/616168/index.rss");
        }catch(Exception e)
        {
        Toast.makeText(getApplicationContext(),str, 10000).show();
        e.printStackTrace();
        }
        try
        {
        String s=worddefinition("dry");
        TextView tv=new TextView(this);
        tv.setText(s);
        }catch(Exception e)
        {
        Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
        e.printStackTrace();
        }*/
    }
}



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"
    />
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:background="@drawable/icon"></Button>
<ImageView android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_height="match_parent" android:layout_width="match_parent"></ImageView>
</LinearLayout>


Flickr with jason


package com.jasonex1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class JsonEx extends Activity {
   
   
String apikey1="733a667ca5dd461af38f81e7b1d0e7e8";
FlickrPhoto[] photos;
int [] ph={R.drawable.icon,R.drawable.icon};
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        HttpClient httpclient=new DefaultHttpClient();
        HttpGet httpget=new HttpGet("http://api.flickr.com/services/rest/?method=flickr.photos.search&tags=yourTag&format=json&api_key="+ apikey1 + "&per_page=5&nojsoncallback=1");
        HttpResponse response;
        try
        {
        response=httpclient.execute(httpget);
        HttpEntity entity=response.getEntity();
        if(entity!=null)
        {
        InputStream in=entity.getContent();
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        StringBuilder sb=new StringBuilder();
        String currentline="";
        try
        {
        while((currentline=br.readLine())!=null)
        {
        Log.d("line","current line data is"+currentline);
        sb.append(currentline+"\n");
        }
       
        }catch(IOException e)
        {
        e.printStackTrace();
        }
       
        String result=sb.toString();
        Log.d("result","result is"+result);
        JSONObject thedata=new JSONObject(result);
        JSONObject thephotosdata=thedata.getJSONObject("photos");
        JSONArray thephotodata=thephotosdata.getJSONArray("photo");
        photos=new FlickrPhoto[thephotodata.length()];
        for(int i=0;i<thephotodata.length();i++)
        {
        JSONObject photodata=thephotodata.getJSONObject(i);
        photos[i]=new FlickrPhoto(photodata.getString("id"),
        photodata.getString("owner"),photodata.getString("secret"),photodata.getString("server"),
        photodata.getString("title"),photodata.getString("farm"));
        }
        in.close();
       
        }
        }catch(Exception e)
        {
        e.printStackTrace();
        }
    ListView lv=(ListView)findViewById(R.id.listView1);
    lv.setAdapter(new FlickrEx(this,photos));
   
    }
    public  class FlickrEx extends BaseAdapter
    {
    Context con;
    private FlickrPhoto[] photos;
    LayoutInflater inflater;
    FlickrEx(Context c,FlickrPhoto[] items)
    {
    this.con=c;
    this.photos=items;
    inflater=(LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   
    }

@Override
public int getCount() {
// TODO Auto-generated method stub
return photos.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View photorow=inflater.inflate(R.layout.row,null);
ImageView img=(ImageView)photorow.findViewById(R.id.ImageView);
img.setImageBitmap(imageFromUrl(photos[position].makeURL()));
TextView tv=(TextView)photorow.findViewById(R.id.TextView);
tv.setText("image");

return photorow;
}
public Bitmap imageFromUrl(String url)
{
Bitmap bitmap;
URL imageurl=null;
try
{
imageurl=new URL(url);
}catch(Exception e)
{
e.printStackTrace();
}
try
{
HttpURLConnection httpconnection=(HttpURLConnection)imageurl.openConnection();
httpconnection.setDoInput(true);
httpconnection.connect();
InputStream in=httpconnection.getInputStream();
bitmap=BitmapFactory.decodeStream(in);

}catch(Exception e)
{
e.printStackTrace();
bitmap=Bitmap.createBitmap(10,10,Bitmap.Config.ARGB_8888);

}


return bitmap;

}
   
    }
    class FlickrPhoto
    {
    String id;
    String owner;
    String secret;
    String server;
    String title;
    String farm;
    public FlickrPhoto(String id1,String owner1,String secret1,String server1,String title1,String farm1)
    {
    this.id=id1;
    this.owner=owner1;
    this.secret=secret1;
    this.server=server1;
    this.farm=farm1;
    this.title=title1;
   
    }
    public String makeURL()
    {
   
return "http://farm" + farm + ".static.flickr.com/" + server + "/"+ id + "_" + secret + "_m.jpg";
   
    }
    }
}

Jason Example


package com.jasonex;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class jasonEx extends Activity {
    /** Called when the activity is first created. */
   
private final static String jstring= "{\"person\":{\"name\":\"A\",\"age\":30,\"children\":[{\"name\":\"B\",\"age\":5}," + "{\"name\":\"C\",\"age\":7},{\"name\":\"D\",\"age\":9}]}}";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tx1=(TextView)findViewById(R.id.textView1);
        TextView tx2=(TextView)findViewById(R.id.textView2);
        TextView tx3=(TextView)findViewById(R.id.textView3);
        try
        {
        JSONObject person=((new JSONObject(jstring)).getJSONObject("person"));
        String name=person.getString("name");
        Log.d("Name","name is"+name);
        tx1.setText("person name is"+name);
        tx2.setText(name+"is"+person.getInt("age")+"years old");
        tx3.setText(name+"has"+person.getJSONArray("children").length()+"chidren");
        }catch(JSONException e)
        {
        e.printStackTrace();
       
        }
       
    }
}

Creating your own keypad



    keypad.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hebrwKeyboardView" android:layout_width="fill_parent"
android:layout_alignParentBottom="true" android:layout_below="@+id/xsubLayout"
android:orientation="vertical" android:background="#252625"
android:visibility="visible" android:layout_height="180sp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="200sp"
android:orientation="vertical" android:layout_alignParentBottom="true"
android:clipChildren="true">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="200sp"
android:padding="0sp">
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="0sp">
<LinearLayout android:baselineAligned="true"
android:layout_width="fill_parent" android:layout_height="45sp"
android:fitsSystemWindows="true">
<Button android:id="@+id/xQ" android:layout_width="30sp"
android:layout_height="fill_parent" android:text="Q"
android:textColor="#000" android:tag="Q" android:padding="0sp"
android:textStyle="bold" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xW"
android:layout_width="30sp" android:layout_height="fill_parent"
android:padding="0sp" android:textColor="#000" android:tag="W"
android:text="W" android:textStyle="bold" android:layout_gravity="left" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xE"
android:layout_gravity="left" android:layout_width="30sp"
android:padding="0sp" android:layout_height="fill_parent"
android:text="E" android:tag="E" android:textStyle="bold"
android:textColor="#000" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xR"
android:layout_width="30sp" android:layout_gravity="center_horizontal"
android:layout_height="fill_parent" android:text="R" android:tag="R"
android:textColor="#000" android:textStyle="bold"
android:fitsSystemWindows="true" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xT"
android:layout_width="33sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" android:text="T"
android:tag="T" android:fitsSystemWindows="true"
android:textColor="#000" android:textStyle="bold"
android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xY"
android:layout_width="33sp" android:layout_height="fill_parent"
android:tag="y" android:layout_gravity="center" android:text="Y"
android:fitsSystemWindows="true" android:textColor="#000"
android:textStyle="bold" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xU"
android:layout_width="33sp" android:layout_gravity="center_horizontal"
android:layout_height="fill_parent" android:text="U" android:tag="U"
android:textColor="#000" android:textStyle="bold"
android:fitsSystemWindows="true" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xI"
android:layout_width="33sp" android:layout_height="fill_parent"
android:text="I" android:fitsSystemWindows="true" android:tag="I"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xO"
android:layout_width="33sp" android:layout_height="fill_parent"
android:text="O" android:fitsSystemWindows="true" android:tag="O"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xP"
android:layout_width="33sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:text="P"
android:fitsSystemWindows="true" android:tag="P"
android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
</LinearLayout>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="45sp">
<Button android:layout_alignWithParentIfMissing="true"
android:soundEffectsEnabled="true" android:id="@+id/xA"
android:layout_width="30sp" android:layout_centerHorizontal="true"
android:tag="A" android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold"
android:layout_height="fill_parent" android:text="A"
android:layout_alignParentLeft="true" android:fitsSystemWindows="true" />
<Button android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:layout_toRightOf="@id/xA"
android:id="@+id/xS" android:layout_width="30sp" android:tag="S"
android:layout_gravity="center_horizontal|center_vertical|center"
android:layout_height="fill_parent" android:text="S"
android:textColor="#000" android:textStyle="bold"
android:layout_alignWithParentIfMissing="true"
android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xD"
android:layout_toRightOf="@id/xS" android:layout_width="30sp"
android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold" android:tag="D"
android:layout_height="fill_parent" android:text="D"
android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xF"
android:layout_toRightOf="@id/xD" android:layout_width="30sp"
android:tag="F" android:layout_gravity="center_horizontal|center_vertical|center"
android:layout_height="fill_parent" android:text="F"
android:textColor="#000" android:textStyle="bold"
android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xG"
android:layout_toRightOf="@id/xF" android:layout_width="33sp"
android:textColor="#000" android:textStyle="bold"
android:layout_height="fill_parent" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="G" android:text="G" android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xH"
android:layout_toRightOf="@id/xG" android:layout_width="33sp"
android:tag="H" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="center_horizontal|center_vertical|center"
android:text="H" android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xJ"
android:layout_toRightOf="@id/xH" android:layout_width="33sp"
android:textColor="#000" android:textStyle="bold"
android:layout_height="fill_parent" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="J" android:text="J" android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xK"
android:textColor="#000" android:textStyle="bold"
android:layout_toRightOf="@id/xJ" android:layout_width="33sp"
android:layout_height="fill_parent" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="K" android:text="K" android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xL"
android:textColor="#000" android:textStyle="bold"
android:layout_toRightOf="@id/xK" android:layout_width="33sp"
android:layout_height="fill_parent" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="L" android:text="L" android:fitsSystemWindows="true" />
<Button android:layout_alignWithParentIfMissing="true"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="true" android:id="@+id/xS1"
android:layout_toRightOf="@id/xL" android:layout_width="33sp"
android:textColor="#000" android:textStyle="bold"
android:layout_height="fill_parent" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="ç" android:text="ç" android:fitsSystemWindows="true" />
</RelativeLayout>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="fill_vertical"
android:fitsSystemWindows="true" android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="45sp" android:gravity="bottom"
android:orientation="horizontal">
<Button android:soundEffectsEnabled="true" android:id="@+id/xS2"
android:layout_width="30sp" android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold"
android:layout_height="fill_parent" android:text="à" android:tag="à"
android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xZ"
android:layout_width="30sp" android:layout_gravity="center_horizontal|center_vertical|center"
android:layout_height="fill_parent" android:text="Z" android:tag="Z"
android:textColor="#000" android:textStyle="bold"
android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xX"
android:layout_width="30sp" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="X" android:layout_height="fill_parent" android:text="X"
android:textColor="#000" android:textStyle="bold"
android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xC"
android:layout_width="30sp" android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="C" android:layout_height="fill_parent" android:text="C"
android:textColor="#000" android:textStyle="bold"
android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xV"
android:layout_width="30sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold" android:tag="V"
android:text="V" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xB"
android:layout_width="33sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="B" android:text="B" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xN"
android:layout_width="33sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold" android:tag="N"
android:text="N" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xM"
android:layout_width="33sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold" android:tag="M"
android:text="M" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xS3"
android:layout_width="33sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:tag="é"
android:layout_gravity="center_horizontal|center_vertical|center"
android:text="é" android:fitsSystemWindows="true" />

<Button android:soundEffectsEnabled="true" android:id="@+id/xBack"
android:layout_width="33sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:tag="back"
android:layout_gravity="center_horizontal|center_vertical|center"
android:fitsSystemWindows="true" android:background="@drawable/back_high" />


</LinearLayout>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fitsSystemWindows="true"
android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="45sp" android:gravity="bottom"
android:orientation="horizontal">
<Button android:soundEffectsEnabled="true" android:id="@+id/xChange"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:paddingLeft="35sp" android:tag="upper" android:background="@drawable/change" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xS4"
android:layout_width="33sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:tag="è" android:text="è" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xSpace"
android:textColor="#000" android:textStyle="bold"
android:layout_width="75sp" android:layout_height="fill_parent"
android:tag=" " android:text="|_____|" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xS5"
android:layout_width="30sp" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold"
android:layout_gravity="center_horizontal|center_vertical|center"
android:tag="û" android:text="û" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xS6"
android:layout_width="30sp" android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|center_vertical|center"
android:textColor="#000" android:textStyle="bold" android:tag="î"
android:text="î" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xNum"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:textColor="#000" android:textStyle="bold" android:tag="num"
android:text="12#" android:fitsSystemWindows="true" />
<Button android:soundEffectsEnabled="true" android:id="@+id/xDone"
android:layout_width="40sp" android:layout_height="fill_parent"
android:layout_gravity="right" android:background="@drawable/hide_high"
android:textColor="#000" android:textStyle="bold" android:tag="done"
android:fitsSystemWindows="true" />
</LinearLayout>
</TableRow>
</TableLayout>
</TableLayout>
</RelativeLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/xMLayout"
android:background="#000000" android:layout_height="fill_parent"
android:focusable="true"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/xsubLayout"
android:keepScreenOn="true" android:orientation="vertical"
android:layout_height="fill_parent">
<EditText android:id="@+id/xEt" android:layout_width="fill_parent"
android:focusableInTouchMode="true" android:layout_height="wrap_content" />
<EditText android:id="@+id/et1" android:layout_width="fill_parent"
android:layout_below="@+id/xEt" android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:id="@+id/xK1"
android:layout_height="wrap_content" android:orientation="vertical"
android:visibility="gone">
<include android:id="@+id/xKeyBoard" layout="@layout/keyboard"></include>
</RelativeLayout>
</RelativeLayout>


package com.mykey;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class MyKey extends Activity implements OnTouchListener,OnClickListener,OnFocusChangeListener {
    /** Called when the activity is first created. */
   
EditText et1,et2;
private Button mBSpace, mBdone, mBack, mBChange, mNum;
private RelativeLayout mLayout, mKLayout;
private boolean isEdit = false, isEdit1 = false;
private String mUpper = "upper", mLower = "lower";
private int w, mWindowWidth;
private String sL[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z", "ç", "à ", "é", "è", "û", "î" };
private String cL[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "ç", "à ", "é", "è", "û", "î" };
private String nS[] = { "!", ")", "'", "#", "3", "$", "%", "&", "8", "*",
"?", "/", "+", "-", "9", "0", "1", "4", "@", "5", "7", "(", "2",
"\"", "6", "_", "=", "]", "[", "<", ">", "|" };
private Button mB[] = new Button[32];
   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try
        {
        setContentView(R.layout.main);
        setKeys();
setFrow();
setSrow();
setTrow();
setForow();
et1 = (EditText) findViewById(R.id.xEt);
et1.setOnTouchListener(this);
et1.setOnFocusChangeListener(this);
et2 = (EditText) findViewById(R.id.et1);

et2.setOnTouchListener(this);
et2.setOnFocusChangeListener(this);
et1.setOnClickListener(this);
et2.setOnClickListener(this);
mLayout = (RelativeLayout) findViewById(R.id.xK1);
mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);
       
       
       
        }catch (Exception e)
        {
        e.printStackTrace();
        }
     
       
    }

private void setFrow() {
w = (mWindowWidth / 13);
w = w - 15;
mB[16].setWidth(w);
mB[22].setWidth(w + 3);
mB[4].setWidth(w);
mB[17].setWidth(w);
mB[19].setWidth(w);
mB[24].setWidth(w);
mB[20].setWidth(w);
mB[8].setWidth(w);
mB[14].setWidth(w);
mB[15].setWidth(w);
mB[16].setHeight(50);
mB[22].setHeight(50);
mB[4].setHeight(50);
mB[17].setHeight(50);
mB[19].setHeight(50);
mB[24].setHeight(50);
mB[20].setHeight(50);
mB[8].setHeight(50);
mB[14].setHeight(50);
mB[15].setHeight(50);

}

private void setTrow() {
w = (mWindowWidth / 12);
mB[25].setWidth(w);
mB[23].setWidth(w);
mB[2].setWidth(w);
mB[21].setWidth(w);
mB[1].setWidth(w);
mB[13].setWidth(w);
mB[12].setWidth(w);
mB[27].setWidth(w);
mB[28].setWidth(w);
mBack.setWidth(w);

mB[25].setHeight(50);
mB[23].setHeight(50);
mB[2].setHeight(50);
mB[21].setHeight(50);
mB[1].setHeight(50);
mB[13].setHeight(50);
mB[12].setHeight(50);
mB[27].setHeight(50);
mB[28].setHeight(50);
mBack.setHeight(50);

}

private void setSrow() {
w = (mWindowWidth / 10);
mB[0].setWidth(w);
mB[18].setWidth(w);
mB[3].setWidth(w);
mB[5].setWidth(w);
mB[6].setWidth(w);
mB[7].setWidth(w);
mB[26].setWidth(w);
mB[9].setWidth(w);
mB[10].setWidth(w);
mB[11].setWidth(w);
mB[26].setWidth(w);

mB[0].setHeight(50);
mB[18].setHeight(50);
mB[3].setHeight(50);
mB[5].setHeight(50);
mB[6].setHeight(50);
mB[7].setHeight(50);
mB[9].setHeight(50);
mB[10].setHeight(50);
mB[11].setHeight(50);
mB[26].setHeight(50);
}

private void setForow() {
w = (mWindowWidth / 10);
mBSpace.setWidth(w * 4);
mBSpace.setHeight(50);
mB[29].setWidth(w);
mB[29].setHeight(50);

mB[30].setWidth(w);
mB[30].setHeight(50);

mB[31].setHeight(50);
mB[31].setWidth(w);
mBdone.setWidth(w + (w / 1));
mBdone.setHeight(50);

}

private void setKeys() {
// TODO Auto-generated method stub
mWindowWidth=getWindowManager().getDefaultDisplay().getWidth();
mB[0]=(Button)findViewById(R.id.xA);
mB[1]=(Button)findViewById(R.id.xB);
mB[2]=(Button)findViewById(R.id.xC);
mB[3]=(Button)findViewById(R.id.xD);
mB[4]=(Button)findViewById(R.id.xE);
mB[5]=(Button)findViewById(R.id.xF);
mB[6]=(Button)findViewById(R.id.xG);
mB[7]=(Button)findViewById(R.id.xH);
mB[8]=(Button)findViewById(R.id.xI);
mB[9]=(Button)findViewById(R.id.xJ);
mB[10]=(Button)findViewById(R.id.xK);
mB[11]=(Button)findViewById(R.id.xL);
mB[12]=(Button)findViewById(R.id.xM);
mB[13]=(Button)findViewById(R.id.xN);
mB[14]=(Button)findViewById(R.id.xO);
mB[15]=(Button)findViewById(R.id.xP);
mB[16]=(Button)findViewById(R.id.xQ);
mB[17]=(Button)findViewById(R.id.xR);
mB[18]=(Button)findViewById(R.id.xS);
mB[19]=(Button)findViewById(R.id.xT);
mB[20]=(Button)findViewById(R.id.xU);
mB[21]=(Button)findViewById(R.id.xV);
mB[22]=(Button)findViewById(R.id.xW);
mB[23]=(Button)findViewById(R.id.xX);
mB[24]=(Button)findViewById(R.id.xY);
mB[25]=(Button)findViewById(R.id.xZ);
mB[26]=(Button)findViewById(R.id.xS1);
mB[27]=(Button)findViewById(R.id.xS2);
mB[28]=(Button)findViewById(R.id.xS3);
mB[29]=(Button)findViewById(R.id.xS4);
mB[30]=(Button)findViewById(R.id.xS5);
mB[31]=(Button)findViewById(R.id.xS6);
mBSpace=(Button)findViewById(R.id.xSpace);
mBdone=(Button)findViewById(R.id.xDone);
mBChange=(Button)findViewById(R.id.xChange);
mBack=(Button)findViewById(R.id.xBack);
mNum=(Button)findViewById(R.id.xNum);
for(int i=0;i<mB.length;i++)
mB[i].setOnClickListener(this);
mBSpace.setOnClickListener(this);
mBdone.setOnClickListener(this);
mBack.setOnClickListener(this);
mBChange.setOnClickListener(this);
mNum.setOnClickListener(this);



}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(v==et1)
{
hideDefaultKeyboard();
enableKeyboard();

}
if(v==et2)
{
hideDefaultKeyboard();
enableKeyboard();
}
return true;
}

private void enableKeyboard() {
// TODO Auto-generated method stub
mLayout.setVisibility(RelativeLayout.VISIBLE);
mKLayout.setVisibility(RelativeLayout.VISIBLE);

}

private void hideDefaultKeyboard() {
// TODO Auto-generated method stub
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==mBChange)
{
if(mBChange.getTag().equals(mUpper))
{
changeSmallLetters();
changeSmallTags();

}
else if(mBChange.getTag().equals(mLower))
{
changeCapitalLetters();
changeCaptialTags();
}
}else if(v!=mBdone && v!=mBack && v!=mBChange && v!=mNum)
{
addText(v);
}else if(v==mBdone)
{
disableKeyboard();
}else if(v==mBack)
{
isBack(v);
}else if(v==mNum)
{
String tag=(String)mNum.getTag();
if(tag.equals("num"))
{
changeSyNuLetters();
changeSyNuTags();
mBChange.setVisibility(Button.INVISIBLE);
}
if(tag.equals("ABC"))
{
changeCapitalLetters();
changeCaptialTags();
}
}

}

private void disableKeyboard() {
// TODO Auto-generated method stub
mLayout.setVisibility(RelativeLayout.INVISIBLE);
mKLayout.setVisibility(RelativeLayout.INVISIBLE);


}

private void changeSyNuTags() {
// TODO Auto-generated method stub
for(int i=0;i<nS.length;i++)
mB[i].setTag(sL[i]);
mNum.setTag("ABC");

}

private void changeSyNuLetters() {
// TODO Auto-generated method stub
for(int i=0;i<nS.length;i++)
mB[i].setText(nS[i]);
mNum.setTag("ABC");




}

private void changeCaptialTags() {
// TODO Auto-generated method stub
for(int i=0;i<cL.length;i++)
mB[i].setTag(cL[i]);
//mBChange.setTag("upper");
mNum.setText("num");

}

private void changeCapitalLetters() {
// TODO Auto-generated method stub
mBChange.setVisibility(Button.VISIBLE);
for(int i=0;i<cL.length;i++)
mB[i].setText(cL[i]);
mBChange.setTag("upper");
mNum.setTag("12#");
}

private void changeSmallTags() {
// TODO Auto-generated method stub
for(int i=0;i<sL.length;i++)
mB[i].setTag(sL[i]);
mBChange.setTag("lower");
mNum.setTag("num");

}

private void changeSmallLetters() {
// TODO Auto-generated method stub
mBChange.setVisibility(Button.VISIBLE);
for(int i=0;i<sL.length;i++)
mB[i].setText(sL[i]);
mNum.setTag("12#");

}

private void isBack(View v) {
// TODO Auto-generated method stub
if(isEdit==true)
{
CharSequence cc=et1.getText();
if(cc!=null && cc.length()>0)
{
et1.setText("");
et1.append(cc.subSequence(0, cc.length()-1));
}
}
if(isEdit1==false)
{
CharSequence cc=et2.getText();
if(cc!=null && cc.length()>0)
{
et2.setText("");
et2.append(cc.subSequence(0, cc.length()-1));
}
}

}

private void addText(View v) {
// TODO Auto-generated method stub
if(isEdit==true)
{
String b;
b=(String)v.getTag();
if(b!=null)
{
et1.append(b);
}
}
if(isEdit1==true)
{
String c;
c=(String)v.getTag();
if(c!=null)
{
et2.append(c);
}
}

}

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == et1 && hasFocus == true) {
isEdit = true;
isEdit1 = false;

} else if (v == et2 && hasFocus == true) {
isEdit = false;
isEdit1 = true;

}

}
}

Wednesday, 7 March 2012

Android And its Version


What Is Android?
Android is a mobile operating system that is based on a modified version of Linux. It was originally
developed by a startup of the same name, Android, Inc. In 2005, as part of its strategy to enter the
mobile space, Google purchased Android and took over its development work (as well as its development
team).
Google wanted Android to be open and free; hence, most of the Android code was released under
the open-source Apache License, which means that anyone who wants to use Android can do so by
downloading the full Android source code. Moreover, vendors (typically hardware manufacturers)
can add their own proprietary extensions to Android and customize Android to differentiate their
products from others. This simple development model makes Android very attractive and has thus
piqued the interest of many vendors. This has been especially true for companies affected by the phenomenon
of Apple’s iPhone, a hugely successful product that revolutionized the smartphone industry.
Such companies include Motorola and Sony Ericsson, which for many years have been developing
their own mobile operating systems. When the iPhone was launched, many of these manufacturers
had to scramble to find new ways of revitalizing their products. These manufacturers see Android as
a solution — they will continue to design their own hardware and use Android as the operating system
that powers it.
The main advantage of adopting Android is that it offers a unified approach to application development.
Developers need only develop for Android, and their applications should be able to run on numerous
different devices, as long as the devices are powered using Android. In the world of smartphones, applications
are the most important part of the success chain. Device manufacturers therefore see Android
as their best hope to challenge the onslaught of the iPhone, which already commands a large base of
applications.
Android Versions
Android has gone through quite a number of updates since its first release. Table 1-1 shows the various
versions of Android and their codenames.
Tab le 1-1: A Brief History of Android Versions
Android Version Releas e Da te Codename
1.1 9 February 2009
1.5 30 April 2009 Cupcake
1.6 15 September 2009 Donut
2.0/2.1 26 October 2009 Eclair
2.2 20 May 2010 Froyo
2.3 6 December 2010 Gingerbread
3.0 Unconfirmed at the time of writing Honeycomb

Features of Android



As Android is open source and freely available to manufacturers for customization, there are no fixed
hardware and software configurations. However, Android itself supports the following features:
➤➤ Storage — Uses SQLite, a lightweight relational database, for data storage. Chapter 6 discusses
data storage in more detail.
➤➤ Connectivity — Supports GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth (includes
A2DP and AVRCP), WiFi, LTE, and WiMAX. Chapter 8 discusses networking in more detail.
➤➤ Messaging — Supports both SMS and MMS. Chapter 8 discusses messaging in more detail.
➤➤ Web browser — Based on the open-source WebKit, together with Chrome’s V8 JavaScript engine
➤➤ Media support — Includes support for the following media: H.263, H.264 (in 3GP or MP4
container), MPEG-4 SP, AMR, AMR-WB (in 3GP container), AAC, HE-AAC (in MP4 or
3GP container), MP3, MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF, and BMP
➤➤ Hardware support — Accelerometer Sensor, Camera, Digital Compass, Proximity Sensor,
and GPS
➤➤ Multi-touch — Supports multi-touch screens
➤➤ Multi-tasking — Supports multi-tasking applications
➤➤ Flash support — Android 2.3 supports Flash 10.1.
➤➤ Tethering — Supports sharing of Internet connections as a wired/wireless hotspot