Android - Cheat Sheet: Unterschied zwischen den Versionen
Aus Wikizone
(Die Seite wurde neu angelegt: „== Neue Activity ==“) |
|||
| (12 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
| + | Hier gibt es nur Code Schnipsel zum Kopieren ohne Erklärung | ||
== Neue Activity == | == Neue Activity == | ||
| + | MyNameActivity.java // Class | ||
| + | activity_my_name.xml // Layout File | ||
| + | AndroidManifest.xml | ||
| + | strings.xml | ||
| + | |||
| + | === Activity Class === | ||
| + | |||
| + | <pre> | ||
| + | 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); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | == Buttons verdrahten == | ||
| + | <pre> | ||
| + | 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); | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ==Map View == | ||
| + | === Google API Key bereithalten === | ||
| + | === Map Fragment zur Activity hinzufügen === | ||
| + | Im Layout File | ||
| + | <pre> | ||
| + | <fragment | ||
| + | android:id="@+id/map" | ||
| + | android:name="com.google.android.gms.maps.MapFragment" | ||
| + | android:layout_width="match_parent" | ||
| + | android:layout_height="match_parent" /> | ||
| + | </pre> | ||
| + | oder direkt in der Activity | ||
| + | <pre> | ||
| + | mMapFragment = MapFragment.newInstance(); | ||
| + | FragmentTransaction fragmentTransaction = | ||
| + | getFragmentManager().beginTransaction(); | ||
| + | fragmentTransaction.add(R.id.my_container, mMapFragment); | ||
| + | fragmentTransaction.commit(); | ||
| + | </pre> | ||
| + | |||
| + | ===Handle für die Karte=== | ||
| + | <pre> | ||
| + | private GoogleMap mMap; | ||
| + | ... | ||
| + | mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); | ||
| + | </pre> | ||
| + | === Verfügbarkeit testen === | ||
| + | In onResume (evtl. auch onCreate) Testen ob die Karte verfügbar ist. | ||
| + | <pre> | ||
| + | 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. | ||
| + | |||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | == Map Optionen == | ||
| + | === Map Type === | ||
| + | <pre> | ||
| + | GoogleMap map; | ||
| + | ... | ||
| + | // Sets the map type to be "hybrid" | ||
| + | map.setMapType(GoogleMap.MAP_TYPE_HYBRID); | ||
| + | </pre> | ||
| + | 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 ==== | ||
| + | <pre> | ||
| + | <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"/> | ||
| + | </pre> | ||
| + | |||
| + | ==== Optionen über Programmierung ==== | ||
| + | <pre> | ||
| + | GoogleMapOptions options = new GoogleMapOptions(); | ||
| + | |||
| + | options.mapType(GoogleMap.MAP_TYPE_SATELLITE) | ||
| + | .compassEnabled(false) | ||
| + | .rotateGesturesEnabled(false) | ||
| + | .tiltGesturesEnabled(false); | ||
| + | </pre> | ||
| + | Optionen im Konstruktor mit übergeben: | ||
| + | MapFragment.newInstance(GoogleMapOptions options) | ||
| + | oder bei MapViews: | ||
| + | MapView(Context, GoogleMapOptions) | ||
| + | == Marker == | ||
| + | https://developers.google.com/maps/documentation/android/marker | ||
Aktuelle Version vom 15. Oktober 2013, 07:55 Uhr
Hier gibt es nur Code Schnipsel zum Kopieren ohne Erklärung
Neue Activity[Bearbeiten]
MyNameActivity.java // Class activity_my_name.xml // Layout File AndroidManifest.xml strings.xml
Activity Class[Bearbeiten]
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[Bearbeiten]
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[Bearbeiten]
Google API Key bereithalten[Bearbeiten]
Map Fragment zur Activity hinzufügen[Bearbeiten]
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[Bearbeiten]
private GoogleMap mMap; ... mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Verfügbarkeit testen[Bearbeiten]
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[Bearbeiten]
Map Type[Bearbeiten]
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[Bearbeiten]
Optionen im Layout File[Bearbeiten]
<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[Bearbeiten]
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)
Marker[Bearbeiten]
https://developers.google.com/maps/documentation/android/marker