2

I am converting a large amount of written content to a easy to read page on an android app. This content mainly consists of text and images but also includes tables.

The content is downloaded as html so I am using the Html.fromHtml() method along with a URLImageParser class to display the content.

I am using a TableLayout in my app. My code works like so:

  • If there are no tables in the content just put it all in one row and add this row to the tablelayout
  • If there is one table, get all of the content before the table, put this in a row and add the row to the tablelayout. Now for each row in the table, get it's contents and add this as a row in the tablelayout. Then add all of the content after the table into a row and add it to the tablelayout.
  • (and so on for multiple tables)

This code was working fine, the problem I have run into is if the content before the table includes images. In this case, the initial content is placed in the TableLayout as a row before the images have finished loading. When the images have finished loading this tablerow is resized to include the images. However the next row in the tablelayout is not moved down accordingly. This means that the row below overlaps the first tablerow.

Ideally I would like the rows below to adjust themselves when the row above changes size, is this possible? I have been trying to look online and could not find anything similar.

I have been trying a different approach - I wanted to detect when the URLImageParser has finished loading the image before I added the row to the tablelayout (I accept this will mean the app will pause for a while when opening a page) but I cannot get this to work either. For some reason the onPostExecute() method of my URLImageParser AsyncTask is never being called.

Does anyone know if it is possible/how to make tablerows readjust themselves if the row above changes it's size? Alternatively, Can anyone see where I am going wrong with my attempt to detect load finish. I am simply using a boolean flag finishedExecuting but it is never set to true. I have included the class below.

Thank you for your time, any replies will be much appreciated.

URLImageParser.java

public class URLImageParser implements ImageGetter {
Context c;
TextView container;
Activity a;
int intrinsicHeight;
int intrinsicWidth;
boolean finishedExecuting;

public URLImageParser(TextView t, Context c, Activity a) {
    this.c = c;
    this.container = t;
    this.a = a;
    intrinsicHeight =0;
    intrinsicWidth = 0;
    finishedExecuting = false;
}

public Drawable getDrawable(String source) {
    System.out.println("getDrawable() was called");
    URLDrawable urlDrawable = new URLDrawable();

    ImageGetterAsyncTask asyncTask = 
        new ImageGetterAsyncTask( urlDrawable);

    asyncTask.execute(source);

    return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {
    URLDrawable urlDrawable;
    boolean usesImageNotFoundDrawable = false;

    public ImageGetterAsyncTask(URLDrawable d) {
        this.urlDrawable = d;
    }

    @Override
    protected Drawable doInBackground(String... params) {
        String source = params[0];
        return fetchDrawable(source);
    }

    @Override
    protected void onPostExecute(Drawable result) {
        System.out.println("ENTERED ON POST EXECUTE");
        //check to see if an image was found (if not could
        //be due to no internet)
        if(result ==null){
            usesImageNotFoundDrawable = true;
            //the drawable wasn't found so use the image not found
            //png
            Drawable imageNotFound = a.getResources().getDrawable(R.drawable.image_not_found);
            result = imageNotFound;
        } else {
            usesImageNotFoundDrawable = false;
        }

        intrinsicHeight = result.getIntrinsicHeight();
        intrinsicWidth = result.getIntrinsicWidth();

        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels -50;
        int height = width * intrinsicHeight / intrinsicWidth;

        result.setBounds(0, 0, 0 + width, 0 
                + height);

        urlDrawable.setBounds(0, 0, 0+width, 0+height);  

        urlDrawable.drawable = result; 

        URLImageParser.this.container.invalidate();


        if(usesImageNotFoundDrawable == true){
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                    + height*4));
        } else {
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                    + height));
        }

        // Pre ICS
        URLImageParser.this.container.setEllipsize(null);

        setFinishedExecuting(true);

       System.out.println("END OF ON POST EXECUTE");

    }

    public Drawable fetchDrawable(String urlString) {
        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");             
            return drawable;
        } catch (Exception e) {
            return null;
        } 
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }
}

public boolean getFinishedExecuting(){
    return finishedExecuting;
}

public void setFinishedExecuting(boolean bool){
    finishedExecuting = bool;
}

}

1 Answer 1

1

Figured this out now. It was not a problem with TableRow. It was a problem with my URLImageParser class.

I removed

if(usesImageNotFoundDrawable == true){
                  URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                + height*4));
    } else {
        URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                + height));
    }

and replaced it with

container.setText(container.getText());

Now the height is detected correctly for images and all content is displayed properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.