Saturday 23 February 2013

TimePickerDialog in Android

ScreenShots:



activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/chooseTimeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose Time"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/displayTimeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java:

package com.ram.tpdexample;

import java.util.Calendar;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
Calendar c = Calendar.getInstance();
TextView display;

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

Button changeTimeBtn = (Button) findViewById(R.id.chooseTimeButton);
display = (TextView) findViewById(R.id.displayTimeText);

changeTimeBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new TimePickerDialog(MainActivity.this, t, c
.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),
true).show();
}
});
}

TimePickerDialog.OnTimeSetListener t = new OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
display.setText("Choosen time is :" + hourOfDay + ":" + minute);
}
};

}




Friday 22 February 2013

DatePickerDialog in Android

ScreenShots:



activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/chooseDateButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Choose Date"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/displatext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java:
package com.ram.dpdexample;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends Activity {
    Calendar c = Calendar.getInstance();
    TextView display;
    int cday, cmonth, cyear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button changeDate = (Button) findViewById(R.id.chooseDateButton);
        display = (TextView) findViewById(R.id.displatext);

        changeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                new DatePickerDialog(MainActivity.this, d,
                        c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
                                .get(Calendar.DAY_OF_MONTH)).show();

            }
        });
    }

    DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {

            cday = dayOfMonth;
            cmonth = monthOfYear + 1;
            cyear = year;

            display.setText("Choosen date is :" + cday + "/" + cmonth + "/"
                    + cyear);
        }
    };
}

Thursday 14 February 2013

Camera in Android

ScreenShots:


activity_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/cameraButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Picture" />

    <ImageView
        android:id="@+id/snapshotImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

MainActivity.java:
package com.camerademo.ram;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
Button b;
ImageView img;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.snapshotImageView);
b = (Button) findViewById(R.id.cameraButton);
b.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(in, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
Bitmap image = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(image);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"error occured during opening camera", Toast.LENGTH_SHORT)
.show();
}
}
}

AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.camerademo.ram"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.CAMERA" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:configChanges="orientation"
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>




View and ViewGroup in Android


ViewGroup:
 A ViewGroup is a special view that can contain other views (called children.)
The view group is the base class for layouts and views containers


View:
This class represents the basic building block for user interface components.
A View occupies a rectangular area on the screen and is responsible for drawing and event handling.
View is the base class for widgets, hich are used to create interactive UI components (buttons, text fields, etc.)

Finding Current Location Address

ScreenShots:


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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java:

package com.ram.locationaddress;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
double latti, longi;
TextView display;

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

display = (TextView) findViewById(R.id.text);

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

LocationListener locationListener = new LocationListener() {

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latti = location.getLatitude();
longi = location.getLongitude();

getAddress();

 
}
};

locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

}

void getAddress() {
Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());

try {
// Addresses:A class representing an Address, i.e, a set of Strings
// describing a location
List<Address> addresses = gc.getFromLocation(latti, longi, 1);

String addr = "";

if (addresses.size() > 0) {
for (int i = 0; i <        addresses.get(0).getMaxAddressLineIndex(); i++)
addr += addresses.get(0).getAddressLine(i) + "\n";
}

display.setText(addr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

AndroidManifest.java:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
         <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.locationaddress.MainActivity"
            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>

Publishing Android Applications in Google Play

Click Here: http://www.youtube.com/watch?v=J5BMrf9mkqk

Friday 8 February 2013

FlashLight Example in Android

ScreenShots:


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"
    tools:context=".MainActivity" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="131dp"
        android:text="Switch" />

</RelativeLayout>

MainActivity.java:
package com.ram.flashlight;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends Activity {
Camera camera;
Parameters parameters;

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

final Switch switch1 = (Switch) findViewById(R.id.switch1);

switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (switch1.isChecked()) {

camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
Toast.makeText(getApplicationContext(),
"Turning on FlashLight..", Toast.LENGTH_LONG)
.show();

} else {

parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
Toast.makeText(getApplicationContext(),
"Turning off FlashLight..", Toast.LENGTH_LONG)
.show();

}
}
});
}

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />
  <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.flashlight.MainActivity"
            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>




SlideDrawer in Android

ScreenShots:



strings.xml:

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

    <string name="app_name">SlidingDrawer</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="menuname">Menu</string>
    <string name="item1">option1</string>
    <string name="item2">option2</string>
    <string name="item3">option3</string>
    <string name="item4">option4</string>
    <string name="item5">option5</string>

</resources>

activity_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" >
    <SlidingDrawer
        android:id="@+id/slidingDrawer1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:topOffset="50dip" >
    

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

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#FF444444"
            android:gravity="center"
            android:orientation="vertical" >
      

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

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

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

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

            <Button
                android:id="@+id/item5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/item5" >
            </Button>
    
        </LinearLayout>
  
    </SlidingDrawer>

</LinearLayout>

MainActivity.java:

package com.ram.slidingdrawer;

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

public class MainActivity extends Activity {

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

Button i1 = (Button)findViewById(R.id.item1);

i1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();
}
});
}


}