Layout dialog with xml layout file : Dialog « UI « Android






Layout dialog with xml layout file

  
package app.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
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.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import java.util.Date;
import java.text.SimpleDateFormat;
import android.location.Location;

 class Quake {
  private Date date;
  private String details;
  private Location location;
  private double magnitude;
  private String link;

  public Date getDate() { return date; }
  public String getDetails() { return details; }
  public Location getLocation() { return location; }
  public double getMagnitude() { return magnitude; }
  public String getLink() { return link; }
  
  public Quake(Date _d, String _det, Location _loc, double _mag, String _link) {
    date = _d;
    details = _det;
    location = _loc;
    magnitude = _mag;
    link = _link;
  }

  @Override
  public String toString() {
    SimpleDateFormat sdf = new SimpleDateFormat("HH.mm");
    return sdf.format(date) + ": " + magnitude + " " + details;
  }

}
public class Test extends Activity {
  
  static final private int QUAKE_DIALOG = 1;
  static final private int MENU_UPDATE = Menu.FIRST;
  
  ListView earthquakeListView;
  
  ArrayAdapter<Quake> aa;
  ArrayList<Quake> earthquakes = new ArrayList<Quake>();
  
  Quake selectedQuake;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    earthquakeListView = (ListView)this.findViewById(R.id.earthquakeListView);
    earthquakeListView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> _av, View _v, int _index, long _id) {
        selectedQuake = earthquakes.get(_index);
        showDialog(QUAKE_DIALOG);
      }
    });

    aa = new ArrayAdapter<Quake>(getApplicationContext(), android.R.layout.simple_list_item_1, earthquakes);
    earthquakeListView.setAdapter(aa);

    refreshEarthquakes();
  }
  private void refreshEarthquakes() {
    URL url;
    try {
      url = new URL("http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml");
           
      URLConnection connection = url.openConnection(); 
         
      HttpURLConnection httpConnection = (HttpURLConnection)connection; 
      int responseCode = httpConnection.getResponseCode(); 

      if (responseCode == HttpURLConnection.HTTP_OK) { 
        InputStream in = httpConnection.getInputStream(); 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(in);      
        Element docEle = dom.getDocumentElement();
        earthquakes.clear();
        NodeList nl = docEle.getElementsByTagName("entry");
        if (nl != null && nl.getLength() > 0) {
          for (int i = 0 ; i < nl.getLength(); i++) {
            Element entry = (Element)nl.item(i);
            Element title = (Element)entry.getElementsByTagName("title").item(0);
            Element g = (Element)entry.getElementsByTagName("georss:point").item(0);
            Element when = (Element)entry.getElementsByTagName("updated").item(0);
            Element link = (Element)entry.getElementsByTagName("link").item(0);

            String details = title.getFirstChild().getNodeValue();
            String hostname = "http://earthquake.usgs.gov";
            String linkString = hostname + link.getAttribute("href");

            String point = g.getFirstChild().getNodeValue();
            String dt = when.getFirstChild().getNodeValue();  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
            Date qdate = new GregorianCalendar(0,0,0).getTime();
            try {
              qdate = sdf.parse(dt);
            } catch (ParseException e) {
              e.printStackTrace();
            }

            String[] location = point.split(" ");
            Location l = new Location("dummyGPS");
            l.setLatitude(Double.parseDouble(location[0]));
            l.setLongitude(Double.parseDouble(location[1]));

            String magnitudeString = details.split(" ")[1];
            int end =  magnitudeString.length()-1;
            double magnitude = Double.parseDouble(magnitudeString.substring(0, end));
                
            details = details.split(",")[1].trim();
                
            Quake quake = new Quake(qdate, details, l, magnitude, linkString);
            addNewQuake(quake);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    finally {
    }
  }
  private void addNewQuake(Quake _quake) {
    earthquakes.add(_quake);
    aa.notifyDataSetChanged();
  }
    
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {      
    super.onCreateOptionsMenu(menu);
    menu.add(0, MENU_UPDATE, Menu.NONE, "update");
    return true;
  }
            
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
          
    switch (item.getItemId()) {
      case (MENU_UPDATE): {
        refreshEarthquakes();
        return true; 
      }
    } 
    return false;
  }
    
  @Override
  public Dialog onCreateDialog(int id) {
    switch(id) {
      case (QUAKE_DIALOG) :        
        LayoutInflater li = LayoutInflater.from(this);
        View quakeDetailsView = li.inflate(R.layout.row, null);
  
        AlertDialog.Builder quakeDialog = new AlertDialog.Builder(this);
        quakeDialog.setTitle("Quake Time");         
        quakeDialog.setView(quakeDetailsView);
        return quakeDialog.create();
    }
    return null;
  }
       
  @Override
  public void onPrepareDialog(int id, Dialog dialog) {
    switch(id) {
      case (QUAKE_DIALOG) :                  
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        String dateString = sdf.format(selectedQuake.getDate()); 
        String quakeText = "Mangitude " + selectedQuake.getMagnitude() + "\n" 
                           + selectedQuake.getDetails()  + "\n" + selectedQuake.getLink();          
          
        AlertDialog quakeDialog = (AlertDialog)dialog;
        quakeDialog.setTitle(dateString);
      
        TextView tv = (TextView)quakeDialog.findViewById(R.id.quakeDetailsTextView);
        if (tv != null)
          tv.setText(quakeText);

        break;
    }
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ListView
    android:id="@+id/earthquakeListView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
  />
</LinearLayout>

//row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10sp">
  <TextView
    android:id="@+id/quakeDetailsTextView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textSize="14sp"
    android:autoLink="all"      
  />
</LinearLayout>

   
    
  








Related examples in the same category

1.Dialog Helper
2.Use AlertDialog to inform exception
3.Add option item to Alert dialog and get user selection result
4.Add some padding to keep the dialog borders away
5.Dialog activity and TextView
6.Preference dialog
7.Show dialog
8.Dialog layout
9.extends DialogFragment
10.Color Picker Dialog
11.Dialog Yes No Message
12.Dialog Yes No Old School Message
13.Dialog Yes No Holo Light Message
14.Dialog Yes No Long Message
15.Dialog Yes No Ultra Long Message
16.Dialog with List of value
17.Dialog with Progress
18.Dialog with Single Choice
19.Dialog Multiple Choice
20.Dialog Multiple Choice Cursor
21.Dialog with Text Entry
22.Dialog with Xml layout
23.Dialog Activity
24.Demonstrates how to show an AlertDialog that is managed by a Fragment.
25.Fragment Dialog
26.Demonstrates the use of progress dialogs.
27.File open dialog
28.Show error AlertDialog
29.extends DialogPreference
30.Show Notification Alert Dialog
31.Text Dialog
32.Create Chang Log Dialog
33.Show Title And Message Dialog
34.Show dialog and parse URL
35.Error Dialog Wrapper
36.Ok Dialog Wrapper
37.Display an alert dialog
38.AlertDialog Question
39.import android.app.AlertDialog;
40.dialog Builder
41.A dialog that allows the user to select multiple entries.
42.Help Dialog Creator
43.Color Select Dialog
44.Create MessageBox