I have a map,using google maps api2 on android studio,when I tap anywhere, I want to open a listview where there would be radio buttons and other kind of stuff. I dont know how to do that,how to use intent,how to use listeners,can anyone help me? Here is the code I got so far:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener {
private MapView mapV;
private GoogleMap map;
private GoogleMapOptions options;
private CameraUpdate camUp;
private double lat,lon;
private LatLng koordinate;
private String provider;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
koordinate = new LatLng(location.getLatitude(),location.getLongitude());
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(koordinate, 10);
map.animateCamera(cameraUpdate);
}
public void openList(View view)
{
Intent intent = new Intent(intent.ACTION_VIEW ,);
ListView list = (ListView) findViewById(R.id.idList);
}
XML file:
<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" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idList"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="openList"/>
The Intent class is usually associated with the button. When a button is clicked, the Intent class can be used to simply wake/start another activity or it can pass some data(String/int/etc) to that activity, and it can also get some data back if you want.
To simply wake another activity, you do:
Intent i = new Intent (*activity_action_name*);
startActivity(i);
For listeners, it's better to use when you have a lots of onClick. Because it's a good way to reduce data redundancy. If you just have one onClick. It's not necessary to use OnClickListener.
Example: if you have two buttons. You can do following.
//in onCreate declare some buttons
Button bt1 = (Button) findViewById(R.id.*button1_id*);
bt1.setOnClickListener(new clsOnClickListener());
Button bt2 = (Button) findViewById(R.id.*button2_id*);
bt2.setOnClickListener(new clsOnClickListener());
//You can create a class that implements OnClickListener
private class clsOnClickListener implements View.OnClickListener{
public void onClick(View v){
if (v.getId()==R.id.button1_id{
//do what you want
}
else if (v.getId()==R.id.button2_id){
//do something else
}
}
}