Skip to content

Commit 2ea116f

Browse files
JAVATECHIGJAVATECHIG
JAVATECHIG
authored and
JAVATECHIG
committed
Added Handler Example
1 parent 4d57969 commit 2ea116f

File tree

17 files changed

+257
-0
lines changed

17 files changed

+257
-0
lines changed

HandlerExample/AndroidManifest.xml

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

HandlerExample/project.properties

+14
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-18
7.48 KB
Loading
3.69 KB
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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=".MainActivity" >
7+
8+
<Button
9+
android:id="@+id/button1"
10+
android:layout_width="fill_parent"
11+
android:layout_height="wrap_content"
12+
android:layout_margin="15dp"
13+
android:text="Download Image" />
14+
15+
<ImageView
16+
android:id="@+id/imageView"
17+
android:layout_width="fill_parent"
18+
android:layout_height="fill_parent"
19+
android:scaleType="centerInside"
20+
android:src="@drawable/ic_launcher" />
21+
22+
</LinearLayout>

HandlerExample/res/menu/main.xml

+9
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/action_settings"
5+
android:orderInCategory="100"
6+
android:showAsAction="never"
7+
android:title="@string/action_settings"/>
8+
9+
</menu>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!--
4+
Customize dimensions originally defined in res/values/dimens.xml (such as
5+
screen margins) for sw600dp devices (e.g. 7" tablets) here.
6+
-->
7+
8+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<resources>
2+
3+
<!--
4+
Customize dimensions originally defined in res/values/dimens.xml (such as
5+
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
6+
-->
7+
<dimen name="activity_horizontal_margin">128dp</dimen>
8+
9+
</resources>
+11
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>
+12
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>

HandlerExample/res/values/dimens.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
3+
<!-- Default screen margins, per the Android Design guidelines. -->
4+
<dimen name="activity_horizontal_margin">16dp</dimen>
5+
<dimen name="activity_vertical_margin">16dp</dimen>
6+
7+
</resources>

HandlerExample/res/values/strings.xml

+8
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">HandlerExample</string>
5+
<string name="action_settings">Settings</string>
6+
<string name="hello_world">Hello world!</string>
7+
8+
</resources>

HandlerExample/res/values/styles.xml

+20
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.javatechig.handlerexample;
2+
3+
import java.io.InputStream;
4+
import org.apache.http.HttpEntity;
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.HttpStatus;
7+
import org.apache.http.client.methods.HttpGet;
8+
import org.apache.http.impl.client.DefaultHttpClient;
9+
10+
import com.javatechige.handlerexample.R;
11+
12+
import android.app.Activity;
13+
import android.app.ProgressDialog;
14+
import android.graphics.Bitmap;
15+
import android.graphics.BitmapFactory;
16+
import android.os.Bundle;
17+
import android.os.Handler;
18+
import android.os.Message;
19+
import android.util.Log;
20+
import android.view.View;
21+
import android.view.View.OnClickListener;
22+
import android.widget.Button;
23+
import android.widget.ImageView;
24+
25+
public class MainActivity extends Activity {
26+
27+
private ProgressDialog progressDialog;
28+
private ImageView imageView;
29+
private String url = "http://www.9ori.com/store/media/images/8ab579a656.jpg";
30+
private Bitmap bitmap = null;
31+
32+
public void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setContentView(R.layout.activity_main);
35+
36+
imageView = (ImageView) findViewById(R.id.imageView);
37+
38+
Button start = (Button) findViewById(R.id.button1);
39+
start.setOnClickListener(new OnClickListener() {
40+
41+
@Override
42+
public void onClick(View arg0) {
43+
progressDialog = ProgressDialog.show(MainActivity.this,
44+
"", "Loading..");
45+
new Thread() {
46+
public void run() {
47+
bitmap = downloadBitmap(url);
48+
messageHandler.sendEmptyMessage(0);
49+
}
50+
}.start();
51+
52+
}
53+
});
54+
}
55+
56+
private Handler messageHandler = new Handler() {
57+
public void handleMessage(Message msg) {
58+
super.handleMessage(msg);
59+
imageView.setImageBitmap(bitmap);
60+
progressDialog.dismiss();
61+
}
62+
};
63+
64+
private Bitmap downloadBitmap(String url) {
65+
// Initialize the default HTTP client object
66+
final DefaultHttpClient client = new DefaultHttpClient();
67+
68+
//forming a HttoGet request
69+
final HttpGet getRequest = new HttpGet(url);
70+
try {
71+
72+
HttpResponse response = client.execute(getRequest);
73+
74+
//check 200 OK for success
75+
final int statusCode = response.getStatusLine().getStatusCode();
76+
77+
if (statusCode != HttpStatus.SC_OK) {
78+
Log.w("ImageDownloader", "Error " + statusCode +
79+
" while retrieving bitmap from " + url);
80+
return null;
81+
}
82+
83+
final HttpEntity entity = response.getEntity();
84+
if (entity != null) {
85+
InputStream is = null;
86+
try {
87+
// getting contents from the stream
88+
is = entity.getContent();
89+
90+
// decoding stream data back into image Bitmap
91+
final Bitmap bitmap = BitmapFactory.decodeStream(is);
92+
93+
return bitmap;
94+
} finally {
95+
if (is != null) {
96+
is.close();
97+
}
98+
entity.consumeContent();
99+
}
100+
}
101+
} catch (Exception e) {
102+
getRequest.abort();
103+
Log.e(getString(R.string.app_name), "Error "+ e.toString());
104+
}
105+
106+
return null;
107+
}
108+
}

0 commit comments

Comments
 (0)