Android - Cheat Sheet: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 55: | Zeile 55: | ||
==Map View == | ==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> | ||
Version vom 15. Oktober 2013, 07:27 Uhr
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.
}
}
}