Jan 25, 2013

Get extension and MIME type of file using MimeTypeMap

Example to get extension and MIME type of file using MimeTypeMap.

 String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
 String mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);


Jan 24, 2013

Define and apply style on text

To define style, create a xml in res/values/ directory, The name is arbitrary. For example: mystle.xml.

/res/values/mystyle.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="bigred_mono" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#FF0000</item>
        <item name="android:typeface">monospace</item>
    </style>
    <style name="smallblue" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">10sp</item>
        <item name="android:textColor">#0000FF</item>
    </style>
</resources>


Then, you can apply the style in your layout 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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/bigred_mono"
        android:text="It's bigred_mono" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/smallblue"
        android:text="It's smallblue" />

</LinearLayout>


Define and apply style on text

Jan 22, 2013

Example of using SimpleDateFormat

The example demonstrate how to convert a Date object to string using a specified format, and convert back.

SimpleDateFormat

package com.example.androidsimpledateformat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView dateNow = (TextView)findViewById(R.id.datenow);
        TextView dateFormatted = (TextView)findViewById(R.id.dateformatted);
        TextView dateBack = (TextView)findViewById(R.id.dateback);
        
        //Initializes this Date instance to the current time.
        Date now = new Date();
        
        //Convert current time to String using specified format
        String format = "yyyy-MM-dd'T'HH:mm:ssZ";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.US);
        String formattedNow = simpleDateFormat.format(now);
        
        //Convert the string back to Date object
        Date dateConvertBack = null;
        try {
   dateConvertBack = simpleDateFormat.parse(formattedNow);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
        
        dateNow.setText(now.toString());
        dateFormatted.setText(formattedNow);
        dateBack.setText(dateConvertBack.toString());
    }
    
}


<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/datenow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/dateformatted"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/dateback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


Jan 18, 2013

GPX decoding in Android, with time and ele.

Read last two article "Get Latitude and Longitude from gpx file" and "GPX decoding in Android, with custom class", it's further modified to decode "time" and "ele" also.

GPX decoding in Android, with time and ele.


package com.example.androidcodinggpx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  String path = Environment.getExternalStorageDirectory().toString() + "/temp/test.gpx";
  
  TextView textInfo = (TextView)findViewById(R.id.info);
  String info = "";
  
  File gpxFile = new File(path);
  info += gpxFile.getPath() +"\n\n";
  

  List<GpxNode> gpxList = decodeGPX(gpxFile);

  for(int i = 0; i < gpxList.size(); i++){
   info += gpxList.get(i).getGpsNodeInfo() + "\n";
  }
  
  textInfo.setText(info);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 
 class GpxNode{
  
  Location location;
  String ele;
  String time;
  
  GpxNode(){
   location = null;
   ele = "";
   time = "";
  }
  
  GpxNode(Location loc){
   location = loc;
   ele = "";
   time = "";
  }
  
  GpxNode(Location loc, String e, String t){
   location = loc;
   ele = e;
   time = t;
  }
  
  void setEle(String e){
   ele = e;
  }
  
  void setTime(String t){
   time = t;
  }
  
  Location getLocation(){
   return location;
  }
  
  String getLocationString(){
   return location.getLatitude() + ":" + location.getLongitude();
  }
  
  String getGpsNodeInfo(){
   return location.getLatitude() + ":" + location.getLongitude() + "\n"
     + time + "\n"
     + ele + "\n";
  }
 }
 
 private List<GpxNode> decodeGPX(File file){
  List<GpxNode> list = new ArrayList<GpxNode>();

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
   FileInputStream fileInputStream = new FileInputStream(file);
   Document document = documentBuilder.parse(fileInputStream);
   Element elementRoot = document.getDocumentElement();
   
   NodeList nodelist_trkpt = elementRoot.getElementsByTagName("trkpt");

   for(int i = 0; i < nodelist_trkpt.getLength(); i++){
    
    Node node = nodelist_trkpt.item(i);
    NamedNodeMap attributes = node.getAttributes();
    
    String newLatitude = attributes.getNamedItem("lat").getTextContent();
    Double newLatitude_double = Double.parseDouble(newLatitude);
    
    String newLongitude = attributes.getNamedItem("lon").getTextContent();
    Double newLongitude_double = Double.parseDouble(newLongitude);
    
    String newLocationName = newLatitude + ":" + newLongitude;
    Location newLocation = new Location(newLocationName);
    newLocation.setLatitude(newLatitude_double);
    newLocation.setLongitude(newLongitude_double);
    
    NodeList childNodes = node.getChildNodes();
    
    String newEle = searchNodeListForTaget(childNodes, "ele");
    String newTime = searchNodeListForTaget(childNodes, "time");
    
    //insert in list
    GpxNode newGpxNode = new GpxNode(newLocation, newEle, newTime);
    list.add(newGpxNode);

   }
   
   fileInputStream.close();
   
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return list;
 }
 
 String searchNodeListForTaget(NodeList src, String target){
  
  for(int i = 0; i < src.getLength(); i++){
   Node n = src.item(i);
   if (n.getNodeName().equals(target)){
    return n.getFirstChild().getNodeValue();
   }
  }
  return "";
 }

}


GPX decoding in Android, with custom class.

Last post describe "Get Latitude and Longitude from gpx file on Android". In order to prepare further expand the function, a custom class, GpxNode, is implemented. It will be enrich in further post.

GPX decoding in Android


It have the same output as in the last article "Get Latitude and Longitude from gpx file".

package com.example.androidcodinggpx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  String path = Environment.getExternalStorageDirectory().toString() + "/temp/test.gpx";
  
  TextView textInfo = (TextView)findViewById(R.id.info);
  String info = "";
  
  File gpxFile = new File(path);
  info += gpxFile.getPath() +"\n\n";
  

  List<GpxNode> gpxList = decodeGPX(gpxFile);

  for(int i = 0; i < gpxList.size(); i++){
   info += gpxList.get(i).getLocationString() + "\n";
  }
  
  textInfo.setText(info);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 
 class GpxNode{
  
  Location location;
  String ele;
  String time;
  
  GpxNode(){
   location = null;
   ele = "";
   time = "";
  }
  
  GpxNode(Location loc){
   location = loc;
   ele = "";
   time = "";
  }
  
  GpxNode(Location loc, String e, String t){
   location = loc;
   ele = e;
   time = t;
  }
  
  void setEle(String e){
   ele = e;
  }
  
  void setTime(String t){
   time = t;
  }
  
  Location getLocation(){
   return location;
  }
  
  String getLocationString(){
   return location.getLatitude() + ":" + location.getLongitude();
  }
  
 }
 
 private List<GpxNode> decodeGPX(File file){
  List<GpxNode> list = new ArrayList<GpxNode>();

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
   FileInputStream fileInputStream = new FileInputStream(file);
   Document document = documentBuilder.parse(fileInputStream);
   Element elementRoot = document.getDocumentElement();
   
   NodeList nodelist_trkpt = elementRoot.getElementsByTagName("trkpt");

   for(int i = 0; i < nodelist_trkpt.getLength(); i++){
    
    Node node = nodelist_trkpt.item(i);
    NamedNodeMap attributes = node.getAttributes();
    
    String newLatitude = attributes.getNamedItem("lat").getTextContent();
    Double newLatitude_double = Double.parseDouble(newLatitude);
    
    String newLongitude = attributes.getNamedItem("lon").getTextContent();
    Double newLongitude_double = Double.parseDouble(newLongitude);
    
    String newLocationName = newLatitude + ":" + newLongitude;
    Location newLocation = new Location(newLocationName);
    newLocation.setLatitude(newLatitude_double);
    newLocation.setLongitude(newLongitude_double);
    
    GpxNode newGpxNode = new GpxNode(newLocation);
    list.add(newGpxNode);

   }
   
   fileInputStream.close();
   
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return list;
 }

}


Next: GPX decoding in Android, with time and ele.

Infolinks In Text Ads