Skip to content

Commit d759517

Browse files
Done Weather App
1 parent b8d7bcb commit d759517

File tree

18 files changed

+413
-0
lines changed

18 files changed

+413
-0
lines changed

AndroidManifest.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.weatherapp"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="8"
9+
android:targetSdkVersion="17" />
10+
<uses-permission android:name="android.permission.INTERNET"/>
11+
12+
<application
13+
android:allowBackup="true"
14+
android:icon="@drawable/ic_launcher"
15+
android:label="@string/app_name"
16+
android:theme="@style/AppTheme" >
17+
<activity
18+
android:name="com.example.weatherapp.WeatherActivity"
19+
android:label="@string/app_name" >
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>

ic_launcher-web.png

54.4 KB
Loading

libs/android-support-v4.jar

377 KB
Binary file not shown.

proguard-project.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# To enable ProGuard in your project, edit project.properties
2+
# to define the proguard.config property as described in that file.
3+
#
4+
# Add project specific ProGuard rules here.
5+
# By default, the flags in this file are appended to flags specified
6+
# in ${sdk.dir}/tools/proguard/proguard-android.txt
7+
# You can edit the include path and order by changing the ProGuard
8+
# include property in project.properties.
9+
#
10+
# For more details, see
11+
# http://developer.android.com/guide/developing/tools/proguard.html
12+
13+
# Add any project specific keep options here:
14+
15+
# If your project uses WebView with JS, uncomment the following
16+
# and specify the fully qualified class name to the JavaScript interface
17+
# class:
18+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19+
# public *;
20+
#}

project.properties

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system edit
7+
# "ant.properties", and override values to adapt the script to your
8+
# project structure.
9+
#
10+
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11+
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12+
13+
# Project target.
14+
target=android-17

res/drawable-hdpi/ic_launcher.png

8.02 KB
Loading

res/drawable-ldpi/ic_launcher.png

2.5 KB
Loading

res/drawable-mdpi/ic_launcher.png

3.97 KB
Loading

res/drawable-xhdpi/ic_launcher.png

13.2 KB
Loading

res/layout/activity_weather.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
tools:context=".WeatherActivity" >
7+
8+
<LinearLayout
9+
android:layout_width="fill_parent"
10+
android:layout_height="wrap_content"
11+
android:orientation="horizontal" >
12+
13+
<Button
14+
android:id="@+id/button_get_weather"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:gravity="center"
18+
android:text="@string/weather" />
19+
<Spinner
20+
android:id="@+id/spinner_city_name"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_marginLeft="10dip" />
24+
</LinearLayout>
25+
26+
<WebView
27+
android:id="@+id/webView_weather"
28+
android:layout_width="fill_parent"
29+
android:layout_height="fill_parent" />
30+
31+
</LinearLayout>

res/menu/activity_weather.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
2+
3+
<item
4+
android:id="@+id/menu_settings"
5+
android:orderInCategory="100"
6+
android:showAsAction="never"
7+
android:title="@string/menu_settings"/>
8+
9+
</menu>

res/values-v11/styles.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 11+. This theme completely replaces
5+
AppBaseTheme from res/values/styles.xml on API 11+ devices.
6+
-->
7+
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
8+
<!-- API 11 theme customizations can go here. -->
9+
</style>
10+
11+
</resources>

res/values-v14/styles.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 14+. This theme completely replaces
5+
AppBaseTheme from BOTH res/values/styles.xml and
6+
res/values-v11/styles.xml on API 14+ devices.
7+
-->
8+
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
9+
<!-- API 14 theme customizations can go here. -->
10+
</style>
11+
12+
</resources>

res/values/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">WeatherApp</string>
5+
<string name="weather">Click to view</string>
6+
<string name="menu_settings">Settings</string>
7+
8+
</resources>

res/values/styles.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme, dependent on API level. This theme is replaced
5+
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
6+
-->
7+
<style name="AppBaseTheme" parent="android:Theme.Light">
8+
<!--
9+
Theme customizations available in newer API levels can go in
10+
res/values-vXX/styles.xml, while customizations related to
11+
backward-compatibility can go here.
12+
-->
13+
</style>
14+
15+
<!-- Application theme. -->
16+
<style name="AppTheme" parent="AppBaseTheme">
17+
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
18+
</style>
19+
20+
</resources>
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.example.weatherapp;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.Reader;
8+
import java.io.StringWriter;
9+
import java.io.Writer;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
15+
import org.apache.http.HttpResponse;
16+
import org.apache.http.client.ClientProtocolException;
17+
import org.apache.http.client.HttpClient;
18+
import org.apache.http.client.methods.HttpGet;
19+
import org.apache.http.impl.client.DefaultHttpClient;
20+
21+
import android.app.Activity;
22+
import android.app.ProgressDialog;
23+
import android.os.Bundle;
24+
import android.view.Menu;
25+
import android.view.View;
26+
import android.webkit.WebView;
27+
import android.widget.ArrayAdapter;
28+
import android.widget.Button;
29+
import android.widget.Spinner;
30+
31+
import com.example.weatherapp.data.City;
32+
import com.example.weatherapp.tools.XmlParser;
33+
34+
public class WeatherActivity extends Activity {
35+
36+
private String weatherText = "";
37+
38+
private HashMap<String, String> cityPair;
39+
private ArrayAdapter<String> arrayAdapterSpinnerCityName;
40+
41+
private HttpResponse response = null;
42+
43+
private Button btnWeather;
44+
private WebView webViewWeather;
45+
private Spinner spinnerCityName;
46+
private ProgressDialog httpGetProgressDialog;
47+
48+
@Override
49+
protected void onCreate(Bundle savedInstanceState) {
50+
super.onCreate(savedInstanceState);
51+
setContentView(R.layout.activity_weather);
52+
53+
btnWeather = (Button) findViewById(R.id.button_get_weather);
54+
webViewWeather = (WebView) findViewById(R.id.webView_weather);
55+
56+
spinnerCityName = (Spinner) findViewById(R.id.spinner_city_name);
57+
58+
cityPair = new HashMap<String, String>();
59+
60+
createCityWoeidPair();
61+
62+
arrayAdapterSpinnerCityName = new ArrayAdapter<String>(this,
63+
android.R.layout.simple_list_item_1, new ArrayList<String>(
64+
cityPair.keySet()));
65+
66+
spinnerCityName.setAdapter(arrayAdapterSpinnerCityName);
67+
68+
btnWeather.setOnClickListener(new View.OnClickListener() {
69+
70+
@Override
71+
public void onClick(View v) {
72+
73+
httpGetProgressDialog = new ProgressDialog(WeatherActivity.this)
74+
.show(WeatherActivity.this, "Please wait...",
75+
"Fetching weather data for "
76+
+ spinnerCityName.getSelectedItem()
77+
.toString());
78+
79+
new Thread() {
80+
@Override
81+
public void run() {
82+
super.run();
83+
84+
try {
85+
weatherText = parseXmlString(convertInputStreamToString(convertResponseToInputStream(getWeatherRss(spinnerCityName
86+
.getSelectedItem().toString()))));
87+
} catch (IOException e) {
88+
// TODO Auto-generated catch block
89+
e.printStackTrace();
90+
}
91+
92+
runOnUiThread(changeWeatherText);
93+
}
94+
}.start();
95+
}
96+
});
97+
}
98+
99+
private HttpResponse getWeatherRss(String cityname) {
100+
try {
101+
HttpClient client = new DefaultHttpClient();
102+
HttpGet request = new HttpGet();
103+
request.setURI(new URI(
104+
"http://weather.yahooapis.com/forecastrss?w="
105+
+ cityPair.get(cityname) + "&u=c"));
106+
response = client.execute(request);
107+
} catch (URISyntaxException e) {
108+
e.printStackTrace();
109+
} catch (ClientProtocolException e) {
110+
// TODO Auto-generated catch block
111+
e.printStackTrace();
112+
} catch (IOException e) {
113+
// TODO Auto-generated catch block
114+
e.printStackTrace();
115+
}
116+
return response;
117+
}
118+
119+
private String parseXmlString(String xmlString) {
120+
return XmlParser.parse(xmlString);
121+
}
122+
123+
private String convertInputStreamToString(InputStream inputStream)
124+
throws IOException {
125+
if (inputStream != null) {
126+
Writer writer = new StringWriter();
127+
128+
char[] buffer = new char[1024];
129+
try {
130+
Reader reader = new BufferedReader(new InputStreamReader(
131+
inputStream, "UTF-8"), 1024);
132+
int n;
133+
while ((n = reader.read(buffer)) != -1) {
134+
writer.write(buffer, 0, n);
135+
}
136+
} finally {
137+
inputStream.close();
138+
}
139+
return writer.toString();
140+
} else {
141+
return "";
142+
}
143+
}
144+
145+
private InputStream convertResponseToInputStream(HttpResponse response)
146+
throws IOException {
147+
return response.getEntity().getContent();
148+
}
149+
150+
private Runnable changeWeatherText = new Runnable() {
151+
152+
@Override
153+
public void run() {
154+
155+
if(httpGetProgressDialog.isShowing() && httpGetProgressDialog != null)
156+
httpGetProgressDialog.hide();
157+
158+
webViewWeather.loadData(weatherText, "text/html", "utf-8");
159+
}
160+
};
161+
162+
@Override
163+
public boolean onCreateOptionsMenu(Menu menu) {
164+
// Inflate the menu; this adds items to the action bar if it is present.
165+
getMenuInflater().inflate(R.menu.activity_weather, menu);
166+
return true;
167+
}
168+
169+
private void createCityWoeidPair() {
170+
City dhaka = new City("Dhaka", "22502221");
171+
City kathmondu = new City("Kathmondu", "2269179");
172+
173+
cityPair.put(dhaka.getName(), dhaka.getWOEID());
174+
cityPair.put(kathmondu.getName(), kathmondu.getWOEID());
175+
}
176+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.weatherapp.data;
2+
3+
public class City {
4+
5+
private String name;
6+
private String WOEID;
7+
8+
public City(String name, String woeid)
9+
{
10+
this.name = name;
11+
WOEID = woeid;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getWOEID() {
23+
return WOEID;
24+
}
25+
26+
public void setWOEID(String wOEID) {
27+
WOEID = wOEID;
28+
}
29+
}

0 commit comments

Comments
 (0)