my app has two functions, validate the input the user enter. one EditText must be numbers only, one must be alphabet only.no space or special characters. i have done the numbers, but i search there are no alphabet-only validation. so can someone teach me how to loop through EditText and ensure every character is a alphabet? EDIT: i change my code in my afterTextChanged.when i press a alphabet or number in my userTextInout, my app crashed second is to calculate the steps user has taken or just distance by using gps. however my output has nothing. it display nothing. I'm not sure whether it has something to do with the emulator. in my past application, when i click on the emulator control to send gps data, it will not send most of the time, after i click repeatedly on the send button then it can update my location.
i heard there is a new Location API for more accuracy but what is the difference between that and mine? at the Google developer page there are not much detail.
even if i calculated the distance, how do i display it? the setText does not display long. it is my first time using distanceBetween method so i am not familar with it.
XML code
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/textInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="34dp"
android:ems="5"
android:maxLength="100"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/numberInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textInput"
android:layout_below="@+id/textInput"
android:layout_marginTop="64dp"
android:ems="5"
android:inputType="number"
android:maxLength="3" />
<TextView
android:id="@+id/Distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/numberInput"
android:layout_marginLeft="37dp"
android:layout_marginTop="107dp"
android:text="TextView" />
<TextView
android:id="@+id/currentLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Distance"
android:layout_below="@+id/Distance"
android:layout_marginTop="29dp"
android:text="TextView" />
<TextView
android:id="@+id/currentLon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/currentLat"
android:layout_below="@+id/currentLat"
android:layout_marginTop="29dp"
android:text="TextView" />
</RelativeLayout>
Main java code
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
//import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements TextWatcher{
protected LocationManager locationManager;
EditText userNumberInput;
EditText userTextInput;
TextView distanceText;
TextView latitude;
TextView longitude;
double lat1,lon1,lat2,lon2;
long dist;
float[] result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userNumberInput=(EditText)findViewById(R.id.numberInput);
userNumberInput.addTextChangedListener(this);
userTextInput=(EditText)findViewById(R.id.textInput);
distanceText=(TextView)findViewById(R.id.Distance);
latitude=(TextView)findViewById(R.id.currentLat);
longitude=(TextView)findViewById(R.id.currentLon);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location loc1=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Criteria criteria = new Criteria();
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 0, 1, myLocationListener);
latitude.setText(ToString(loc1.getLatitude()));
longitude.setText(ToString(loc1.getLongitude()));
//locationManager.requestLocationUpdates(netprovider, 0, 1, myLocationListener);
}
private CharSequence ToString(double latitude2) {
// TODO Auto-generated method stub
return null;
}
LocationListener myLocationListener = new LocationListener()
{
public void onLocationChanged(Location loc2)
{
//Location loc2=new Location("");
lat2=loc2.getLatitude();
lon2=loc2.getLongitude();
latitude.setText(ToString(loc2.getLatitude()));
longitude.setText(ToString(loc2.getLongitude()));
if(lat1 != 0 && lon1 != 0) {
Location.distanceBetween(lat1,lon1,lat2,lon2,result);
dist+=(long)result[0];
}
lat1=lat2;
lon1=lon2;
lat2=loc2.getLatitude();
lon2=loc2.getLongitude();
distanceText.setText(lat1+","+lon1+","+lat2+","+lon2, dist);
}
private CharSequence ToString(double latitude) {
// TODO Auto-generated method stub
return null;
}
public void onProviderDisabled(String provider)
{
// Update application if provider disabled.
}
public void onProviderEnabled(String provider)
{
// Update application if provider enabled.
}
public void onStatusChanged(String provider, int status,
Bundle extras)
{
// Update application if provider hardware status changed.
}
};
@Override
public void afterTextChanged(Editable edit) {
String textFromEditView = edit.toString();
try
{
boolean isOnlyAlphabet = textFromEditView.matches("/^[A-z]+$/");
if(isOnlyAlphabet == false)
{
edit.replace(0, edit.length(), "only alphabets");
}
}
catch(NumberFormatException e){}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}}
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 41
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 69
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.765: D/libEGL(7383): loaded /system/lib/egl/libEGL_mali.so
01-28 21:33:00.765: D/libEGL(7383): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-28 21:33:00.775: D/libEGL(7383): loaded /system/lib/egl/libGLESv2_mali.so
01-28 21:33:00.780: D/(7383): Device driver API match
01-28 21:33:00.780: D/(7383): Device driver API version: 10
01-28 21:33:00.780: D/(7383): User space API version: 10
01-28 21:33:00.780: D/(7383): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
01-28 21:33:00.815: D/OpenGLRenderer(7383): Enabling debug mode 0
01-28 21:33:00.815: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.815: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.820: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.820: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.825: E/SensorManager(7383): thread start
01-28 21:33:00.825: D/SensorManager(7383): registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 41
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 69
01-28 21:33:01.040: D/SensorManager(7383): unregisterListener:: Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:01.040: D/Sensors(7383): Remain listener = Sending .. normal delay 200ms
01-28 21:33:01.040: I/Sensors(7383): sendDelay --- 200000000
01-28 21:33:01.045: D/SensorManager(7383): JNI - sendDelay
01-28 21:33:01.045: I/SensorManager(7383): Set normal delay = true
01-28 21:33:01.545: W/IInputConnectionWrapper(7383): showStatusIcon on inactive InputConnection
01-28 21:33:10.460: E/SensorManager(7383): thread start
01-28 21:33:10.465: D/SensorManager(7383): registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:12.555: D/SensorManager(7383): unregisterListener:: Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:12.555: D/Sensors(7383): Remain listener = Sending .. normal delay 200ms
01-28 21:33:12.555: I/Sensors(7383): sendDelay --- 200000000
01-28 21:33:12.555: D/SensorManager(7383): JNI - sendDelay
01-28 21:33:12.555: I/SensorManager(7383): Set normal delay = true
01-28 21:33:12.555: D/SensorManager(7383): registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42417a08
01-28 21:33:12.565: W/IInputConnectionWrapper(7383): getSelectedText on inactive InputConnection
01-28 21:33:12.565: W/IInputConnectionWrapper(7383): setComposingText on inactive InputConnection
01-28 21:33:12.565: W/IInputConnectionWrapper(7383): getExtractedText on inactive InputConnection
01-28 21:33:12.675: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:12.675: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:12.680: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:12.680: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.225: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.225: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.225: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.230: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.425: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.425: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.430: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.430: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.865: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.880: D/dalvikvm(7383): GC_CONCURRENT freed 233K, 7% free 12329K/13191K, paused 13ms+4ms, total 41ms
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.395: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.395: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.395: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.400: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.995: D/AndroidRuntime(7383): Shutting down VM
01-28 21:33:20.995: W/dalvikvm(7383): threadid=1: thread exiting with uncaught exception (group=0x4178f2a0)
01-28 21:33:20.995: E/AndroidRuntime(7383): FATAL EXCEPTION: main
01-28 21:33:20.995: E/AndroidRuntime(7383): java.lang.IllegalArgumentException: results is null or has length < 1
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.location.Location.distanceBetween(Location.java:394)
01-28 21:33:20.995: E/AndroidRuntime(7383): at com.example.validationapp.MainActivity$1.onLocationChanged(MainActivity.java:74)
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:263)
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:196)
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:212)
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.os.Looper.loop(Looper.java:137)
01-28 21:33:20.995: E/AndroidRuntime(7383): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-28 21:33:20.995: E/AndroidRuntime(7383): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 21:33:20.995: E/AndroidRuntime(7383): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 21:33:20.995: E/AndroidRuntime(7383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-28 21:33:20.995: E/AndroidRuntime(7383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-28 21:33:20.995: E/AndroidRuntime(7383): at dalvik.system.NativeStart.main(Native Method)
01-28 21:33:30.770: I/Process(7383): Sending signal. PID: 7383 SIG: 9