Android - Cheat Sheet
Aus Wikizone
Hier gibt es nur Code Schnipsel zum Kopieren ohne Erklärung
Neue Activity
MyNameActivity.java // Class activity_my_name.xml // Layout File AndroidManifest.xml strings.xml
Activity Class
package de.webmynet.android.myappname;
import android.app.Activity;
import android.os.Bundle;
public class MyNameActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_name);
}
}
Buttons verdrahten
import android.widget.Button;
public class MyNameActivity extends Activity implements OnClickListener {
Button b1,b2;
...
b1 = (Button)findViewById(R.id.b1);
b2 = (Button)findViewById(R.id.b2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
...
@Override
public void onClick(View v) {
int id = v.getId();
Intent i = null;
switch (id){
case R.id.b1:
i = new Intent(this,OtherNameActivity.class);
break;
case R.id.b2:
i = new Intent("de.webmynet.appname.OTHERNAME");//Variant with Manifest Activity Name
break;
default:
break;
}
startActivity(i);
}
Map View
Google API Key bereithalten
Map Fragment zur Activity hinzufügen
Im Layout File
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" />
oder direkt in der Activity
mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.my_container, mMapFragment);
fragmentTransaction.commit();
Handle für die Karte
private GoogleMap mMap; ... mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Verfügbarkeit testen
In onResume (evtl. auch onCreate) Testen ob die Karte verfügbar ist.
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
Map Optionen
Map Type
GoogleMap map; ... // Sets the map type to be "hybrid" map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
MAP_TYPE_NORMAL: Basic map with roads. MAP_TYPE_SATELLITE: Satellite view with roads. MAP_TYPE_TERRAIN: Terrain view with roads.
Optionen
Optionen im Layout File
<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" map:cameraBearing="112.5" map:cameraTargetLat="-33.796923" map:cameraTargetLng="150.922433" map:cameraTilt="30" map:cameraZoom="13" map:mapType="normal" map:uiCompass="false" map:uiRotateGestures="true" map:uiScrollGestures="false" map:uiTiltGestures="true" map:uiZoomControls="false" map:uiZoomGestures="true"/>
Optionen über Programmierung
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_SATELLITE)
.compassEnabled(false)
.rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
Optionen im Konstruktor mit übergeben:
MapFragment.newInstance(GoogleMapOptions options)
oder bei MapViews:
MapView(Context, GoogleMapOptions)