public void setZoom(List<LatLng> coords){ GoogleMap map = getMap(); LatLngBounds bounds = helper.getBounds(coords); int zoom_padding = 20; map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, zoom_padding)); } public LatLngBounds getBounds(List<LatLng> coords){ LatLngBounds.Builder builder = new LatLngBounds.Builder(); for(LatLng coord:coords){ builder.include(coord); } return builder.build(); }easy!
miércoles, 19 de junio de 2013
Fit map zoom to markers
Exists an easy way in Google Map API v2 to get the correct zoom for all the markers we have in the map:
miércoles, 12 de junio de 2013
XML Parser (XmlPullParser)
I feel more confortable parsing JSON, but sometimes I get some services in XML. After reading some articles about parsing and reading the google advices I finally decided to use the class XMLPullParser, not bad, here an example.
First we get the xml
First we get the xml
public InputStream getResponseInputStream(String url){ Log.i("URL REQUEST",url); URL request; try { request = new URL(url); HttpURLConnection conn = (HttpURLConnection)request.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); InputStream is = conn.getInputStream(); return is; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }Create the parser
public List<Member> getMemberData(String url){ InputStream is=null; try{ is = getResponseInputStream(url); XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(is, null); parser.nextTag(); List<Member> members = readMembers(parser); is.close(); return members; } catch (Exception e) { e.printStackTrace(); } return null; }Parsing:
private List<Member> readMembers(XmlPullParser parser) throws XmlPullParserException, IOException { List<Member> entries = new ArrayList<Member>(); parser.require(XmlPullParser.START_TAG, null, "root"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); // Starts by looking for the entry tag if (name.equals("item")) { entries.add(readMemberDetail(parser)); } else { skip(parser); } } return entries; } private Member readMemberDetail(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, null, "item"); Member member = new Member(); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String localName = parser.getName(); if(localName.equals("id")) member.id = readText(parser,"id"); else if(localName.equals("name")) member.name = readText(parser,"name"); else if(localName.equals("address")) member.address = readText(parser,"address"); else skip(parser); } return member; }And some utils like readText to get the content and skip if we get some tag we don't want to parse
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { if (parser.getEventType() != XmlPullParser.START_TAG) { throw new IllegalStateException(); } int depth = 1; while (depth != 0) { switch (parser.next()) { case XmlPullParser.END_TAG: depth--; break; case XmlPullParser.START_TAG: depth++; break; } } } public String readText(XmlPullParser parser, String tag){ try{ parser.require(XmlPullParser.START_TAG, null, tag); String text = readText(parser); parser.require(XmlPullParser.END_TAG, null, tag); return text; } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }More info
martes, 11 de junio de 2013
Unable to open Android Studio because files are locked
"Files in C:\Program Files(x86)\Android\android-studio\system\caches are locked.
Android Studio will not able to start up"
Yeahhh!! this is the error I get when I am trying to open my Android Studio with Windows 7, but, it is as simple to fix as right click over Android Studio icon -> Run as Administrator.
that's all
Android Studio will not able to start up"
Yeahhh!! this is the error I get when I am trying to open my Android Studio with Windows 7, but, it is as simple to fix as right click over Android Studio icon -> Run as Administrator.
that's all
lunes, 10 de junio de 2013
Enable "More" button on action bar
By default in "pure android" devices you can see at right, in action bar, three gray dots to open the overflow menu. This menu is open in other devices by the hardware button menu.
Those devices with hardware button hide the action bar button.
Sometimes, project managers and clients without any idea about design encourage to us to enable this option in action bar, I am not recommending this feature, if there is no button in action bar is for one reason, it is because the hardware button exists, but if after talking with your pm or your client he is still blind, ok, this is the hack.
Again from Stackoverflow
Those devices with hardware button hide the action bar button.
Sometimes, project managers and clients without any idea about design encourage to us to enable this option in action bar, I am not recommending this feature, if there is no button in action bar is for one reason, it is because the hardware button exists, but if after talking with your pm or your client he is still blind, ok, this is the hack.
Again from Stackoverflow
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); getOverflowMenu(); } protected void getOverflowMenu() { try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); if(menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { e.printStackTrace(); } } }
viernes, 7 de junio de 2013
Shake Listener for Android
Something is not implemented is a default shake listener for Android.
Here is my solution I steal from Stackoverflow
Here is my solution I steal from Stackoverflow
public class ShakerActivity extends Activity implements SensorEventListener{ private SensorManager mSensorManager; private float mAccel; // acceleration apart from gravity private float mAccelCurrent; // current acceleration including gravity private float mAccelLast; // last acceleration including gravity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* do this in onCreate */ mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mAccel = 0.00f; mAccelCurrent = SensorManager.GRAVITY_EARTH; mAccelLast = SensorManager.GRAVITY_EARTH; } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; mAccelLast = mAccelCurrent; mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z)); float delta = mAccelCurrent - mAccelLast; mAccel = mAccel * 0.9f + delta; // perform low-cut filter //if mAccel is greater than 2 mobile is being shaked if(mAccel>2){ //TODO: put the code you want to execute when mobile is shaked } } }
jueves, 6 de junio de 2013
How to post code
First of all, I know this is not related to android but first thing I have to learn is "How to post code" in my blog, making a quick search around google I've found this... http://geektalkin.blogspot.com.es/2009/11/embed-code-syntax-highlighting-in-blog.html let's see if it is working:
First post
Is supposed that I should put here all my doubts and issues about android development.
I hope some of you can take profit from this, and me too.
regards
I hope some of you can take profit from this, and me too.
regards
Suscribirse a:
Entradas (Atom)