Friday 14 September 2012



TEMPERATURE   COVERTER
Objective:






string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Temperature</string>
    <color name="myColor">#3399CC</color>
    <string name="myClickHandler">myClickHandler</string>
    <string name="show">Result is:</string>
    <string name="celsius">to celsius</string>
    <string name="fahrenheit">to fahrenheit</string>
    <string name="calc">calculate</string>
    <color name="red">#FF0000</color>

</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/myColor>

<EditText
              android:layout_height="wrap_content"
              android:id="@+id/editText1"
              android:layout_width="fill_parent"
              android:inputType="numberDecimal|numberSigned"></EditText>

         
<TextView
              android:id="@+id/Tview1"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:text="@string/show"
              android:textSize="40dp"
              android:textColor="@color/red"
              android:inputType="numberDecimal|numberSigned"></TextView>
               

 <RadioGroup
               android:layout_height="wrap_content"
               android:id="@+id/radioGroup1"
               android:layout_width="match_parent">
                  
                   <RadioButton
                             android:layout_width="wrap_content"
                             android:id="@+id/radio0"
                             android:layout_height="wrap_content"
                             android:text="@string/celsius"
                             android:textSize="25dp"
                             android:checked="true"></RadioButton>
                  
                   <RadioButton
                             android:layout_width="wrap_content"
                             android:id="@+id/radio1"
                             android:layout_height="wrap_content"
                             android:text="@string/fahrenheit"
                             android:textSize="25dp"></RadioButton>

          </RadioGroup>
         

          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/calc"></Button>

</LinearLayout>

TempConvertActivity.java

package com.android.temp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class TempConvertActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
          EditText etext;
          TextView Tview1;
          RadioButton cbutton,fbutton;
          Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        etext = (EditText) findViewById(R.id.editText1);
        Tview1=(TextView)findViewById(R.id.Tview1);
        cbutton=(RadioButton)findViewById(R.id.radio0);
        fbutton=(RadioButton)findViewById(R.id.radio1);
        btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

          @Override
          public void onClick(View v) {
                   // TODO Auto-generated method stub
                   if (etext.getText().length() == 0) {
                             Toast.makeText(this, "Please enter a valid number" , Toast.LENGTH_LONG).show();
                   }
                   else
                   {
                             float inputValue = Float.parseFloat(etext.getText().toString());

                             if(cbutton.isChecked())
                             {
                                      float outputValue=(inputValue - 32)*5/9;
                                      String text=String.valueOf(outputValue);
                                      Tview1.setText(text);
                             }
                             else
                             {
                                      float outputValue=(inputValue*9)/5+ 32;
                                      String text=String.valueOf(outputValue);
                                      Tview1.setText(text);
                             }
                   }
          }
}
          

Wednesday 12 September 2012


XML- BASE PROGRAM
OBJECTIVE
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="30dp"
android:textColor="@color/Blue"/>

</LinearLayout>

String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Wellcome to Android first-Example</string>
<string name="app_name">Helloxmlbase</string>
<color name="Blue">#0000FF</color>

</resources>

HelloxmlbaseActivity.java
package com.android.hello;

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

public class HelloxmlbaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

AndroidManifest.java

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

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloxmlbaseActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
JAVA-BASE PROGRAM

JavabaseActivity.java
package com.android.Hello1;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class JavabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView tv=new TextView(this);
tv.setText("Welcome to Android First-Example");
tv.setTextSize(30);
tv.setTextColor(Color.BLUE);
setContentView(tv);
}
}

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

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="JavabaseActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

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

</manifest>




HYBRID-BASE EXAMPLE
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Hello2</string>
<string name="hello">Welcome to Android First-Example</string>
<color name="Blue">#FFFFFF</color>
</resources>



Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/TextV1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="30dp"
android:textColor="@color/Blue"/>

</LinearLayout>

Hello2Activity.java

package com.android.Hello2;

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

public class Hello2Activity extends Activity {
/** Called when the activity is first created. */
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.TextV1);
tv.setText("This is modified Text");
tv.setTextSize(33);
}
}


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

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Hello2Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

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

</manifest>

Map



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adaptive.map" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MapAppActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps" />
</application>
</manifest>

map_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clickable="true"
android:apiKey="0sZ2uYOryArwt-zf6TeXndgeZH1VMhRxixDGXDg" />
</RelativeLayout>



MapAppActivity.java

package com.adaptive.map;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;





public class MapAppActivity extends MapActivity {

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;

@SuppressWarnings("deprecation")
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.map_activity); // bind the layout to the activity
// create a map view
// RelativeLayout linearLayout = (RelativeLayout)
// findViewById(R.layout.map_activity);

// Create MapView
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);

// Get MapView Controller
mapController = mapView.getController();
mapController.setZoom(14); // Zoom 1 is world view

// Get LocationManager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// Get Location Provider
String prov = locationManager.getBestProvider(new Criteria(), true);

// Connect Location Provider with Location Listener
locationManager.requestLocationUpdates(prov, 0, 0,
new GeoUpdateHandler());
}

@Override
protected boolean isRouteDisplayed() {
return false;
}




public class GeoUpdateHandler implements LocationListener {

@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); // New Map with passed location
// parameter is drawn
}

@Override
public void onProviderDisabled(String provider) {
}



@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

// Script to capture Longitude and Latitude
// javascript:void(prompt('',gApplication.getMap().getCenter()));

}

Friday 7 September 2012


ListDynamicActivity.java
package com.android.ListDynamic;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class ListDynamicActivity extends ListActivity {

          // LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
          ArrayList<String> listItems = new ArrayList<String>();

          // DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
          ArrayAdapter<String> adapter;

          // RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
          int clickCounter = 0;

          @Override
          public void onCreate(Bundle icicle) {

                   super.onCreate(icicle);
                   setContentView(R.layout.main);
                   adapter = new ArrayAdapter<String>(this,
                                      android.R.layout.simple_list_item_1, listItems);
                   setListAdapter(adapter);
          }

          // METHOD WHICH WILL HANDLE DYNAMIC INSERTION
          public void addItems(View v) {
                   listItems.add("Clicked : " + clickCounter++);
                   adapter.notifyDataSetChanged();
          }
}


Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/addBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addItems"
        android:text="Add New Item" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>
Output