I want to use onClick
method in android button but it is not working correct. On the other hand when I am using onClickListener
then it is showing me another error like
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
I couldn't find what is the problem with it.
Java code is given below :
public void onSearch(View view) throws IOException{
EditText sLocation = (EditText) view.findViewById(R.id.editText1);
String location = sLocation.getText().toString();
List<android.location.Address> addressList= null;
if (location != null || !location.equals(""))
{
Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
android.location.Address address = addressList.get(0);
LatLng latlng = new LatLng(address.getLatitude(),address.getLatitude());
gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
}
}
add_masjid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentStart="true"
android:text="@string/xyz"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<Button
android:id="@+id/generalId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="@drawable/rect1"
android:onClick="onSearch"
android:text="@string/abc"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:clickable="true" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/textView1"
android:layout_toStartOf="@+id/generalId"
android:ems="10"
android:inputType="text"
android:background="#FFFFFF"
android:labelFor="@+id/editText1"
android:layout_above="@+id/map"
android:layout_alignParentTop="true" />
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/generalId">
</com.google.android.gms.maps.MapView>
</RelativeLayout>