1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| package com.android2ee.tuto.gui;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* @author Mathias Seguy (Android2EE)
* @goals This class aims to:
* <ul>
* <li></li>
* </ul>
*/
public class DynamicGui extends Activity {
/******************************************************************************************/
/** Attributes **************************************************************************/
/******************************************************************************************/
/**
* tag used for the log (lower case because it's the naturla prameter of the log.*(tag,msg)
* methods
*/
private final static String tag = "DynamicGui";
/**
* The linear layout that contains the GUI builds dynamically
*/
private LinearLayout layoutOfDynamicContent;
/**
* The hello message
*/
private TextView txvHello;
/**
* The reload Button
*/
private Button btnReload;
/******************************************************************************************/
/** Life cycle Management **************************************************************************/
/******************************************************************************************/
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// instanciate the components of the main.xml layout declaration
layoutOfDynamicContent = (LinearLayout) findViewById(R.id.layoutOfDynamicContent);
txvHello = (TextView) findViewById(R.id.txvHello);
btnReload = (Button) findViewById(R.id.btnReload);
// Add a click listener on the reload button
btnReload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
buildDynamicView(GenerateData.getInstance().generateData());
}
});
// launch the first gui construction
buildDynamicView(GenerateData.getInstance().generateData());
}
/******************************************************************************************/
/** Dynamic GUIO building *****************************************************************/
/******************************************************************************************/
/**
* Build the dynamic content
*
* @param posts
* the list of posts to display
*/
private void buildDynamicView(List<JSONObject> posts) {
// Log.w(tag, "buildDynamicView");
// first delated the content:
// layoutOfDynamicContent.removeAllViews();
layoutOfDynamicContent.removeAllViewsInLayout();
// Then add a ScrollView within that layout:
ScrollView scvMain = new ScrollView(this);
scvMain.setHorizontalFadingEdgeEnabled(true);
scvMain.setFadingEdgeLength(20);
layoutOfDynamicContent.addView(scvMain, getParams(FILL, FILL));
// Then declare the LinearLayout where you'll build the content
LinearLayout lilContent = new LinearLayout(this);
lilContent.setOrientation(LinearLayout.VERTICAL);
// then browse the list of JSONObject and for each build the associated GUI
for (JSONObject post : posts) {
try {
lilContent.addView(buildPostGUI(post), getParams(FILL, WRAP));
} catch (JSONException e) {
Log.e(tag, e.toString());
// Do nothing or add an exception Panel in the gui
// anyway for a demonstration, i do nothing
}
}
// add the linearLayout to the scrollView
scvMain.addView(lilContent, getParams(FILL, FILL));
}
/**
* Build the Gui associated to a post. This GUI is within a LinearLayout
*
* @param post
* the JSONObject we have to build the GUI for
* @return the linearlayout that shows the JSONObject
* @throws JSONException
*/
private LinearLayout buildPostGUI(JSONObject post) throws JSONException {
// Log.w(tag, "buildPostGUI");
// define the layout
LinearLayout lilPost = new LinearLayout(this);
lilPost.setOrientation(LinearLayout.VERTICAL);
lilPost.setPadding(0, 15, 0, 0);
// then add the web picture
ImageView webPicture = new ImageView(this);
webPicture.setImageDrawable(getPicture(post.getString("fromWebPicture")));
//webPicture.setBackgroundColor(0xFFFFFFFF);
// for pictures you need to manage the LayoutParameter in a smartest way
LinearLayout.LayoutParams params = getParams(WRAP, WRAP);
// define its gravity and its margins
params.gravity = Gravity.CENTER_HORIZONTAL;
params.setMargins(50, 50, 50, 50);
// add the webpicture
lilPost.addView(webPicture, params);
// and return
return lilPost;
}
/**
* Build the Gui associated to a message. This GUI is within a LinearLayout
*
* @param message
* the JSONObject we have to build the GUI for
* @return the linearlayout that shows the JSONObject
* @throws JSONException
*/
/**
* This method retrieve the drawable associated to the URL
*
* @param urlPath
* the url of the picture
* @return the drawable downloaded from the url
*/
private Drawable getPicture(String urlPath) {
// the drawable to return
Drawable drawable = null;
try {
// retrieve the URL
URL url = new URL(urlPath);
// Open an input stream on it
InputStream is = (InputStream) url.getContent();
// build the drawable from that input stream
drawable = Drawable.createFromStream(is, "src");
} catch (IOException e) {
Log.e(tag, e.toString());
e.printStackTrace();
}
return drawable;
}
/******************************************************************************************/
/** Layout parameters management **************************************************************************/
/******************************************************************************************/
/**
* Constant layout get calls on layout method shorter To say Fill
*/
private final static Integer FILL = 0;
/**
* Constant layout get calls on layout method shorter To say MATCH
*/
private final static Integer MATCH = 1;
/**
* Constant layout get calls on layout method shorter To say WRAP
*/
private final static Integer WRAP = 1;
/**
* The layout that Fill Weight Match Height (FWMH)
*
* @param weight
* can be either Fill or Match constant from this, or
* LinearLayout.LayoutParams.WRAP_CONTENT
* @param height
* can be either Fill or Match constant from this, or
* LinearLayout.LayoutParams.WRAP_CONTENT
* @return a layout parameter that fill_parent in width and wrap_content in height
*/
private LinearLayout.LayoutParams getParams(int width, int height) {
int widthLayout = 0;
int heightLayout = 0;
// first retrieve the parameter of the layout for the width
if ((width == FILL) || (width == LinearLayout.LayoutParams.FILL_PARENT)) {
widthLayout = LinearLayout.LayoutParams.FILL_PARENT;
} else if ((width == WRAP) || (width == LinearLayout.LayoutParams.WRAP_CONTENT)) {
widthLayout = LinearLayout.LayoutParams.WRAP_CONTENT;
} else {
widthLayout = LinearLayout.LayoutParams.MATCH_PARENT;
}
// second retrieve the parameter of the layout for the height
if ((height == FILL) || (height == LinearLayout.LayoutParams.FILL_PARENT)) {
heightLayout = LinearLayout.LayoutParams.FILL_PARENT;
} else if ((height == WRAP) || (height == LinearLayout.LayoutParams.WRAP_CONTENT)) {
heightLayout = LinearLayout.LayoutParams.WRAP_CONTENT;
} else {
heightLayout = LinearLayout.LayoutParams.MATCH_PARENT;
}
return new LinearLayout.LayoutParams(widthLayout, heightLayout) {
};
}
} |
Partager