Whitespaces in a TextView

165 views Asked by At

I use this parser for downloading images to textview by Html.fromHtml():

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

public int getCounterImages() {
    return counterImages;
}

public void setCounterImages(int counterImages) {
    this.counterImages = counterImages;
}

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) {

    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) {

            if(result ==null){
                usesImageNotFoundDrawable = true;
                //the drawable wasn't found so use the image not found
                //png
                result = a.getResources().getDrawable(R.drawable.image_not_found);
            } else {
                usesImageNotFoundDrawable = false;
            }


            assert result != null;
            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, width, height);

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

            urlDrawable.drawable = result;

            URLImageParser.this.container.invalidate();

                              URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + height));

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

            setFinishedExecuting(true);
    }

    public Drawable fetchDrawable(String urlString) {
        try {
            InputStream is = fetch(urlString);
            return Drawable.createFromStream(is, "src");
        } 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;
}

} And everytime I get a good looking textview, but with additional white space in the end. I've tried to modify my formula, but it also didn't work in all textviews. How can I trim unnesecces rai height or whitespace from TextView in the end?

0

There are 0 answers