miércoles, 12 de diciembre de 2012

EditText: maxLength por Código

Por si alguna vez habéis tenido algún problema/duda con definir el tamaño máximo soportado por los EditText o TextView vía código aquí tenéis la solución. Simple pero funcional:

private void editTextMaxLength(EditText ed, int maxLength){
      InputFilter[] filterArray = new InputFilter[1];
      filterArray[0] = new InputFilter.LengthFilter(maxLength);
      ed.setFilters(filterArray);
}

It works!
Roger Sala,

martes, 4 de diciembre de 2012

Google Maps API v2. MapFragment

Por fin!

Los mapas y los Fragments ya se ha unificado. A continuación y sin andarse mucho por las ramas el ejemplo de como añadirlo y como manejar el GoogleMaps.

1.- Necesitamos la API Key que se obtiene de forma distinta a los MapActivity. En el siguiente enlace se detallan los pasos de como obtenerla. Obtener API KEY

2.- Descargar del SDK Manager -> Extras -> Google Play services.
3.- Importar en libs el archivo jar. AndroidSDK -> Extras -> Google -> Google Play Services -> libs -> google-play-services.jar

4.- Ya podemos empezar a generar el código.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.maps.fragments"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <permission
        android:name="com.example.maps.fragments.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.maps.fragments.permission.MAPS_RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.maps.fragments.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- API KEY  -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyA-v3ukHyXbPP0db9khnT6pwzNzMfM79Nw" />
    </application>

</manifest>



MainActivity.java

package com.example.maps.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements OnClickListener {

private GoogleMap mMap;
private static final LatLng MY_POINT = new LatLng(41.66, 1.54);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Init view elements
((Button) findViewById(R.id.none)).setOnClickListener(this);
((Button) findViewById(R.id.satellite)).setOnClickListener(this);
((Button) findViewById(R.id.hybrid)).setOnClickListener(this);
((Button) findViewById(R.id.normal)).setOnClickListener(this);
((Button) findViewById(R.id.terrain)).setOnClickListener(this);

// Verify Map availability
setUpMapIfNeeded();

}

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) {
// addMarkers
addMarkers();
addCameraOption();
}
}
}

private void addMarkers() {
//custom icon
Marker my_marker = mMap.addMarker(new MarkerOptions()
.position(MY_POINT).title("Melbourne")
.snippet("Some informaton: eg: Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));

//default icon
// Marker my_marker = mMap.addMarker(new MarkerOptions()
// .position(MY_POINT).title("Catalunya")
// .snippet("Some informaton: eg: Population: 4,137,400"));

}

private void addCameraOption(){
// Move the camera instantly to Sydney with a zoom of 15.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MY_POINT, 15));

// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomIn());

// Zoom out to zoom level 10, animating with a duration of 2 seconds.
// mMap.animateCamera(CameraUpdateFactory.zoomTo(10), null, 2000);

// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
   .target(MY_POINT)      // Sets the center of the map to Mountain View
   .zoom(17)                   // Sets the zoom
   .bearing(90)                // Sets the orientation of the camera to east
   .tilt(30)                   // Sets the tilt of the camera to 30 degrees
   .build();                   // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.hybrid:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.terrain:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.none:
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.normal:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.satellite:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;

default:
break;
}
}

}


activity_main.xml

<LinearLayout 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"
    android:orientation="vertical" >

    <!-- <fragment -->
    <!-- android:name="com.example.maps.fragments.TopFragment" -->
    <!-- android:layout_width="fill_parent" -->
    <!-- android:layout_height="100dip" -->
    <!-- android:tag="topFragment" /> -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:weightSum="5" >

        <Button
            android:id="@+id/normal"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Normal"
            android:textSize="12sp" />

        <Button
            android:id="@+id/hybrid"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hybrid"
            android:textSize="12sp" />

        <Button
            android:id="@+id/satellite"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Satellite"
            android:textSize="12sp" />

        <Button
            android:id="@+id/terrain"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Terrain"
            android:textSize="12sp" />

        <Button
            android:id="@+id/none"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="None"
            android:textSize="12sp" />
    </LinearLayout>

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

</LinearLayout>

Aquí tenéis una imagen del MapFragment con el marker personalizado, y con un zoom personalizado de la camera:



It works!
Roger Sala,