Wednesday, 22 February 2012

Get Address from latitude longitude in Android

In Android to Get address from location using Geocoder
Geocoder is a class for handling geocoding and reverse geocoding.
Geocoding is the process of transforming a street address to (latitude,logitude) coordinate.
Reverse geocoding is the process of transorming (latitude,logitude) coordinate to Street address.

Here we can get address based on coordinates using Revierse geocoding
and set Address in to Textview


 Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
       try {
  List<Address> noofaddresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
   if(addresses != null) {
   Address retursAddress= noofaddresses.get(0);
   StringBuilder strReturnedAddress = new StringBuilder("Address is:\n");
   for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
    strReturnedAddress.append(returnsAddress.getAddressLine(i)).append("\n");
   }
   myAddress.setText(strReturnedAddress.toString());
  }
  else{
   myAddress.setText("No Address returned!");
  }catch(Exception e){

}

Click here to Download Full Souce -> Find Address from latitude and longitude in Android



Saturday, 18 February 2012

Get Current Location in Android

In Android LocationManger class is used to get Latitude and Longitude.
Here we can see how to get Latitude and Longitude of Current location.
And How to send latitude and longitude to android emulator.

(UI Layout)activity_location_finder.xml


place .xml in layout folder

<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/txtlatandlong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Android Activity Class(LocationFinderActivity.class)

package com.androidsurya.locationfinder;

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

public class LocationFinderActivity extends Activity implements
        LocationListener {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude, longitude;
    protected boolean gps_enabled, network_enabled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_finder);
        txtLat = (TextView) findViewById(R.id.txtlatandlong);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.txtlatandlong);
        txtLat.setText("Latitude is:" + location.getLatitude()
                + ", Longitude is:" + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude", "disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude", "enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude", "status");
    }

}

Register the Activity in Android Manifest file using <activity> tag
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsurya.locationfinder"
    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.androidsurya.locationfinder.LocationFinderActivity"
            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>

Don't forget to place the uses permission(INTERNET,ACCESS_FINE_LOCATION)
Provide ACCESS_FINE_LOCATION permissions in manifest file for receiving location update
Provide INTERNET permissions in manifest file for using Internet.


How to send latitude and longitude to android emulator

Window -> Open Perspective->emulator device->emulator control->Location Control and  give Longitude and Latitude as input and ‘Send’ Button



Output Screenshot:

 






Saturday, 4 February 2012

How to set maximum volume programmatically In Android

While i am developing one application i was stuck how to the volume of an Android application to maximum, and I saw no of  post regarding this issue MediaPlayer.setValue function.

MediaPlayer.setValue( )
It's not the right function for setting the volume (see the help).

To set the volume to maximum i used below code it's working.

// Get the AudioManager
AudioManager audioManager =
(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
// Set the volume of played media to maximum.
audioManager.setStreamVolume (
AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);



Tags:volume increase in android,android volume,android mediaplayer volume increase,how to set maximum volume,android AudioManager volume setting,how to set max volume 

Thursday, 2 February 2012

Text to Speech Example in Android

In Android providing a best feature (from Android 1.6) called Text to Speech (TTS) which speaks the text. This example shows how to work with android text to speech.

UI Layout(androidtexttospeech.xml)

Place this xml file in resource/layout folder

<?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:background="#ffffff"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:padding="15dip"

android:text="Android Text To Speech"

android:textSize="25dip"

android:textStyle="italic" />

<EditText

android:id="@+id/inputText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_margin="10dip"

android:layout_marginTop="20dip"

android:hint="Please enter some text to speak" />

<Button

android:id="@+id/speakButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_margin="10dip"

android:text="Speak Out Now" />

</LinearLayout>

Android Activity class(TexttoSpeechActivity.class)

package com.androidsurya.texttospeechexample;

import com.androidhive.texttospeech.R;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.speech.tts.TextToSpeech;

import android.speech.tts.TextToSpeech.OnInitListener;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class TexttoSpeechActivity extends Activity implements OnInitListener {

private EditText inputTextEv;

private Button speakButton;

private int MY_DATA_CHECK_CODE = 99;

private TextToSpeech texttospeech;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.androidtexttospeech);

inputTextEv = (EditText) findViewById(R.id.inputText);

speakButton = (Button) findViewById(R.id.speakButton);

speakButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String text = inputTextEv.getText().toString();

if (text != null && text.length() > 0) {

Toast.makeText(TexttoSpeechActivity.this,

"TTS Saying: " + text, Toast.LENGTH_LONG).show();

texttospeech.speak(text, TextToSpeech.QUEUE_ADD, null);

}

}

});

// Check TTS Engine in your mobile or not.

Intent checkIntent = new Intent();

checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == MY_DATA_CHECK_CODE) {

if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {

// TTS in your mobile,so create the TTS object

texttospeech = new TextToSpeech(this, this);

} else {

// TTS in not in your mobile,so install TTS in your mobile

Intent installIntent = new Intent();

installIntent

.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

startActivity(installIntent);

}

}

}

@Override

public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {

Toast.makeText(TexttoSpeechActivity.this,

"Text-To-Speech engine is instaled in your mobile",

Toast.LENGTH_LONG).show();

} else if (status == TextToSpeech.ERROR) {

Toast.makeText(

TexttoSpeechActivity.this,

"Error occurred while installing Text-To-Speech engine in your mobile",

Toast.LENGTH_LONG).show();

}

}

}

Register the Activity in Android Manifest file

<activity

android:name="com.androidsurya.texttospeechexample.TexttoSpeechActivity"

android:label="@string/app_name" >

Output Screenshot:
 


 Some of other methods are in TextToSpeech Class

setPitch (float pitch)


Sets the speech pitch for the TextToSpeech engine. This has no effect on any pre-recorded speech.

setSpeechRate (float speechRate)

Sets the speech rate. This has no effect on any pre-recorded speech.

setLanguage(Locale loc)

Sets the text-to-speech language. The TTS engine will try to use the closest match to the specified language as represented by the Locale, but there is no guarantee that the exact same Locale will be used. Use isLanguageAvailable(Locale) to check the level of support before choosing the language to use for the next utterances.

isSpeeking()

Checks whether the TTS engine is busy speaking.

synthesizeToFile (String text, HashMap<String, String> params, String filename)

Synthesizes the given text to a file using the specified parameters.

getLanguage ()

Returns a Locale instance describing the language currently being used by the TextToSpeech engine.





For More information about Text to Speech : Android Developers site

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...