My application was supposed to be easy way to learn how to do android
programming but this has me stuck. The images I am grabbing are mostly
different sizes so only some of the images are overlapping the
TextView. I have tried settinga Maxheight but this only works when the
screen is in vertical orientation but not when changed to horizontal.
Here is the code:
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:id="@+id/screen"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
>
<LinearLayout
android:id="@+id/screen"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="40px"
android:text=""
android:paddingBottom="10px"
android:layout_weight="1.0"
/>
<EditText
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
/>
</LinearLayout>
<ImageView
android:id="@+id/picview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="true"
android:layout_gravity="bottom"
/>
</android.gesture.GestureOverlayView>
</LinearLayout>
JavaFile
package com.xkcd;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class xkcd extends Activity {
private URL u;
private URL imageURL;
private InputStream is;
private String s;
private JSONObject json;
private BufferedReader reader;
private ImageView picView;
private TextView title;
private TextView number;
private GestureOverlayView gestures;
private GestureLibrary gLib;
private int maxComic;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
json = new JSONObject();
final String TAG = "com.hascode.android.gesture";
if (!gLib.load()) {
Log.w(TAG, "could not load gesture library");
finish();
}
title = (TextView) findViewById(R.id.title);
number= (TextView) findViewById(R.id.number);
number.setKeyListener(new KeyListener(){
@Override
public void clearMetaKeyState(View arg0, Editable arg1,
int arg2) {
// TODO Auto-generated method stub
}
@Override
public int getInputType() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean onKeyDown(View view, Editable text, int
keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onKeyOther(View view, Editable text,
KeyEvent event)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onKeyUp(View view, Editable text, int
keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if(event.getKeyCode() == 66){
createURL(Integer.parseInt(number.getText().toString()));
}
return false;
}
});
picView = (ImageView) findViewById(R.id.picview);
picView.setAdjustViewBounds(true);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
picView.setMaxHeight(height-100);
picView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayHover();
}
});
gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(handleGestureListener);
launch();
}
public void createURL(int number){
String prefix = "http://xkcd.com/";
String suffix = "/info.0.json";
URL jsonURL;
String stringURL = prefix + number + suffix;
try {
jsonURL = new URL(stringURL);
createJSON(jsonURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void displayHover(){
AlertDialog alertDialog = new
AlertDialog.Builder(this).create();
alertDialog.setMessage(json.get("alt").toString());
alertDialog.setButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.show();
}
public void createJSON(URL url){
try {
is = url.openStream();
reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
s = reader.readLine();
is.close();
reader.close();
json = (JSONObject)new JSONParser().parse(s);
displayComic();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void displayComic(){
//Set the title of the app to the title of the comic and its
number
String comicTitle= (String)json.get("title") + " (Comic# " +
json.get("num") + ")";
title.setText(comicTitle);
number.setText(json.get("num").toString());
try {
imageURL= new URL ((String) json.get("img"));
picView.setImageBitmap(getImage(imageURL));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Bitmap getImage(URL url){
Bitmap bitmap = null;
InputStream in = null;
try {
in=url.openStream();
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
public void launch(){
//By default get the most recent XKCD comic
try {
u = new URL("http://xkcd.com/info.0.json");
} catch (MalformedURLException e) {
e.printStackTrace();
}
//Using the JSON string from the URL, create a JSON
object from 3rd
party code.
createJSON(u);
maxComic=Integer.parseInt(json.get("num").toString());
}
private OnGesturePerformedListener handleGestureListener = new
OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView
gestureView,Gesture gesture) {
ArrayList<Prediction> predictions =
gLib.recognize(gesture);
// one prediction needed
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
// checking prediction
if (prediction.score > 1.0) {
String action= prediction.name;
ComicThread newComic;
if (action.equals("Left")){
if(Integer.parseInt(json.get("num").toString())==1){
newComic= new
ComicThread(maxComic);
newComic.start();
}else
newComic= new
ComicThread((Integer.parseInt(json.get("num").toString())-1));
newComic.start();
}
if (action.equals("Right")){
if(Integer.parseInt(json.get("num").toString())==maxComic){
newComic= new
ComicThread(1);
newComic.start();
}else
newComic= new
ComicThread((Integer.parseInt(json.get("num").toString())+1));
newComic.start();
}
}
}
}
};
private class ComicThread extends Thread{
int number;
ProgressDialog dialog;
public ComicThread(int number){
this.number=number;
dialog= ProgressDialog.show(xkcd.this, "","Loading
Comic #" +number
+ ". Please wait...", true);
}
public void run(){
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
createURL(number);
dialog.dismiss();
}
};
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en