I am reading data from a SOAP service and then using that data to load images from the res folder locally. I am using a loop because there will always be six images being loaded. This is my code :
final TableLayout tblLay = (TableLayout)findViewById(R.id.lottotl);
final LayoutParams params = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
for(int i=2;i<9;i++) {
final ImageView image = new ImageView(LastDraw.this);
image.setLayoutParams(trparams);
image.setMaxHeight(20);
image.setMaxWidth(20);
String imgName = "img_" + split[i].substring(split[i].indexOf("=") + 1);
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
image.setImageResource(id);
row2.setLayoutParams(params);
row2.addView(image);
}
tblLay.addView(row2);
The issue I am having is that there is a gap between the first image and the consecutive images.
It looks like this (each number representing an image):
1.........23456
I am thinking it has to do with the layout of the row in the tablelayout, I could be wrong.
Thank you
Anyone?
Figured it out ... feel kind of stupid but, I learnt something! I load a textview into the first row and then the imageviews into the second row. The textview is in the first column and its width is making the first column stretch ... THAT is why there is a gap.
Related
I am using the reddit API and trying to load the comment tree of a thread. My problem is that I am using a recursive function to do that. I am building a view and then adding it programmatically. And it works for small threads but when it has to load a large comment tree I get stackoverflow.So my main question is: what is a good way to load nested comments programmatically and what is the best practice to go around the stack overflow? I have debated adding a counter for comments added and when they exceed some number I might break the loop, but that still doesn't guarantee me a "stackoverflow" free program. Generally speaking how can I keep track of the stack and the heap? Also as a follow up question: my dynamic view gets destroyed on rotation and I recreate it every time. The problem with that is recreating is slow and it slows the rotation. So is there an easy way to keep/save the view on rotation and add it again (setRetainInstance(true) has no effect on the dynamic view, just on the main layout).
private void addTextTree(JSONObject j, LinearLayout layoutparent) throws JSONException {
JSONObject j2 = j.getJSONObject("data");
JSONArray j3 = j2.getJSONArray("children");
LinearLayout currentparent = layoutparent;
Log.d("ADDINGVIEW", j3.length() + "");
for (int i = 0; i < j3.length(); i++) {
JSONObject j4 = j3.getJSONObject(i);
JSONObject j5 = j4.getJSONObject("data");
addComment(j5, currentparent);
}
}
private void addComment(JSONObject j, LinearLayout parent) throws JSONException {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
LinearLayout LLHmain = new LinearLayout(getActivity());
LinearLayout LLVsecond = new LinearLayout(getActivity());
LLHmain.setOrientation(LinearLayout.HORIZONTAL);
LLHmain.setLayoutParams(lp);
LLVsecond.setOrientation(LinearLayout.VERTICAL);
LLVsecond.setLayoutParams(lp1);
TextView author = new TextView(getActivity());
TextView content = new TextView(getActivity());
ImageView bar = new ImageView(getActivity());
bar.setBackgroundResource(R.drawable.lines);
LLHmain.addView(bar, lp1);
author.setText(" " + j.optString("author") + " " + j.optInt("score") + " points");
author.setBackgroundResource(R.drawable.border);
String temp = j.optString("body_html");
String temp1 = Html.fromHtml(temp.substring(22, temp.length() - 12)).toString();
content.setText(Html.fromHtml(temp1));
content.setAutoLinkMask(Linkify.WEB_URLS);
content.setMovementMethod(LinkMovementMethod.getInstance());
LLVsecond.addView(author, lp);
LLVsecond.addView(content, lp);
LLHmain.addView(LLVsecond, lp1);
parent.addView(LLHmain, lp);
if (!j.optString("replies").equals("")) {
JSONObject replies = j.getJSONObject("replies");
addTextTree(replies, LLVsecond);
}
}
Summed up:
What is the best way to handle loading trees of textviews and preventing stack overflow.
Keeping track of the stack and the heap
Saving dynamic view on rotation
Disclaimer: I am new to any sort of programming and by no means do I believe this is the best or only way to do it. I am open to any possible solutions regarding my problem.
I'll give the very short version of the answer (because I'm kinda busy at work) but you follow it that you'll get what you want:
The issue is that View are memory heavy objects and shouldn't be allocating that many. What you do to avoid that is using a ListView that can recycle views.
So instead of recursively create and add views you'll create a data class, for example:
public class DataItem{
String author;
String content;
}
then you build an ArrayList<DataItem> and recursively add all the items to this array. Then you use this array in an adapter in the listview.
that way you can have a thread with thousands of items without issue.
I am working on a project that will display math problems to the screen such as:
10 + 5 =
and then the user will need to guess the answer. I am doing this as more of way for me to learn how android ticks.
I have images from 0 to 9 saved in the drawable folder for each of the dpi setting I need to account. I also have the operators (+,-,*,/,=) also saved.
My questions:
How easy is this to do?
How would I go about doing the above dynamically?
Thanks
--EDIT--
The images and operators are stored as .9.png files in my drawable folder. I have string-array contain my problems that I will randomly pull from and then display them to the screen.
In the past I would do the following:
public View buildProblem()
{
LinearLayout rtnView = new LinearLayout(ctx);
rtnView.setOrientation(LinearLayout.HORIZONTAL);
rtnView.setLayoutParams(new GridLayout.LayoutParams());
// Build LeftSide
Iterator<Integer> itor = buildLeftSide();
ImageView iv;
// loop through of iterator
while(itor.hasNext())
{
iv = new ImageView(ctx);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setImageResource(itor.next());
itor.remove();
rtnView.addView(iv);
}
// Space
rtnView.addView(space);
// Operator
rtnView.addView(plusSign);
// Build LeftSide
itor = buildRightSide();
// another loop
while(itor.hasNext())
{
iv = new ImageView(ctx);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setImageResource(itor.next());
itor.remove();
rtnView.addView(iv);
}
// Space
rtnView.addView(space);
// Equal Sign
rtnView.addView(equalSign);
// Space
rtnView.addView(space);
// return the view
return rtnView;
}
I was thinking of using ImageViews for the numbers to be displayed to the screen which I have used in the past but not sure if that is the best way to do it.
https://github.com/barakisbrown/MathTest -- Is one attempt at doing the above but I have now started to rewrite it from scratch so which is why I am asking for help.
I have a code rite now that just generates a random drink combination from an array, what I need to do is have a different image assigned to each choice and have it display that image.
Here is my code for a random drink:
if(Vodka.equals(true)){
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(array_city.Vodka.length);
text2.setText(array_city.Vodka[randomIndex]);
}
Say this code spits out "Smirnof" then i display a picture of the bottle, it spits out "Sky" then changes to a picture of that bottle. How would i do this without making an if statement for each option, my arrays are very long and that would be alot of if statements i was just Hoping that there is an easier way to do it?
Thanks anybody for your help! it is very much appreciated I have been stuck on this for a while.
===============================================================
#Joan
Here is what i am trying to put together using your code:
//Run option Vodka
if(Vodka.equals(true)){
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(array_city.Vodka.length);
text2.setText(array_city.Vodka[randomIndex]);
final ImageView image = (ImageView) findViewById(R.id.imageView1);
int Cimage = getResources().getIdentifier(array_city.Vodka[randomIndex], null, "com.famousmods.what.should.i.drink");
image.setImageResource(Cimage);
}
Here is what my array looks like (a smaller example):
public static final String[] Vodka = {"Absolut Vodka","Finlandia","Ketel One","Polmos Krakow","Skyy","smirnoff vodka",
"Stolichnaya","Fleischmann's","Gilbey's","Gordon's","Wolfschmitt","Five-O-Clock"};
I have put the file "smirnoff_vodka.png" into my res/drawables as an example but it doesnt work?
You can use getResources().getIdentifier("image_name", null, "your_application_package"); on your context to retrieve the image id. Then you can use this id as you would use R.id.image_name.
EDIT: It needs to be "drawable" instead of null. See below.
I have tried the following code. It loads images quite randomly but some images appear to be the same. Can my code be modified to have all the images uniquely and randomly loaded?
int[] imageViews = {
R.id.ImageView2, R.id.ImageView2,
R.id.ImageView3, R.id.ImageView4,
R.id.ImageView5, R.id.ImageView6,
R.id.ImageView7, R.id.ImageView8,
R.id.ImageView9
};
int[] images = {
R.drawable.m1, R.drawable.m2,
R.drawable.m3, R.drawable.m4,
R.drawable.m5, R.drawable.m6,
R.drawable.m7, R.drawable.m8,
R.drawable.m9
};
Random random = new Random(System.currentTimeMillis());
for(int v : imageViews) {
ImageView iv = (ImageView)findViewById(v);
iv.setImageResource(images[random.nextInt(images.length - 1)]);
check about this solution , this was worked for me..Random images without repetition
Edit: Remove the images from the array as you're displaying them. Recalculate your random bounds to use a shorter array bound.
I have a a set of 10 imageviews in my layout. I have given them sequential id's also as
android:id="#+id/pb1"
android:id="#+id/pb2"
Now I want to change background dynamically.
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(R.drawable.progressgreen);
}
Now inside the for loop I want to set the image view background dynamically. i,e if the currentpoi value is 3, background of 3 image views should be changed. What ever the times the for loop iterates that many image view's background should be changed. Hope the question is clear now.
Note : I have only 1 image progressgreen that need to be set to 10 image views
Finally I did this in the following way,
I placed all the id's in the array as
int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};
Now:
int pindex = 0;
for (pindex; pindex <currentPoi; pindex++) {
ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
img.setImageResource(R.drawable.progressgreen);
}
Now, I am able to change the images dynamically.
#goto10. Thanks for your help. I will debug your point to see what went wrong in my side
Create an ImageView array:
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.pb1);
...
views[9] = (ImageView)findViewById(R.id.pb10);
Now iterate the loop to set the background of images like this:
for (i=1;i<=currentPoi;i++)
{
views[i-1].setBackgroundResource(R.drawable.progressgreen);
}
you can do this by setting the name of drawables something like:
img_1, img_2, img_3...
for (i=1;i<=currentPoi;i++)
{
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable", getPackageName()));
}
Try this code.....
Create image Array..
private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };
And than modify your code
int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
imageview.setBackgroundResource(mThumbIds[i]);}
You could make an array of your ImageViews and then change them in your for loop.
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.imageView0);
...
views[9] = (ImageView)findViewById(R.id.imageView9);
and then change your for loop to:
for (i=1;i<=currentPoi;i++) {
views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
}
Arrays start at index 0, so make sure there's not an off-by-one error in here.
You'll need to give your ImageViews sequential ids, such as "#+id/pb1" and "#+id/pb2", etc.. Then you can get each of them in the loop like this:
for (i=1;i<=currentPoi;i++) {
// Find the image view based on it's name. We know it's pbx where 'x' is a number
// so we concatenate "pb" with the value of i in our loop to get the name
// of the identifier we're looking for. getResources.getIdentifier() is able to use
// this string value to find the ID of the imageView
int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");
// Use the ID retrieved in the previous line to look up the ImageView object
ImageView imageView = (ImageView) findViewById(imageViewId);
// Set the background for the ImageView
imageView.setBackgroundResource(R.drawable.progressgreen);
}
Replace com.your.package.name with your application's package.