Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting dynamic size of layout in Android

In an Activity with a lot of components, I have a RelativeLayout that contains a WebView (it just shows a TextView, that I don't know the size of).

This is the xml code:

<RelativeLayout android:id="@+id/descripcionRelative" android:layout_below="@+id/descripcion_bold" android:layout_width="match_parent" android:layout_height="125dip" android:background="@drawable/my_border">
    <WebView android:id="@+id/webViewDescripcion" android:layout_width="wrap_content" android:layout_height="wrap_content"/>      
</RelativeLayout>

The attribute android:layout_height of RelativeLayout is 125dip, because if text is too large, I want to delimitate to 125dip. If text is large, I will see text with scroll. Great!

But... if text is short, I see a lot of innecessary space.

One solution is change android:layout_height of RelativeLayout to wrap_content. If text is short, the component will have the exact pixels, but if text is too large, I can't delimitate it.

The BIG PROBLEM is that I can't calculate the WebView's height. If i do: descripcion_web.getHeight() it return 0.

If I call this method here, it doesn't return the right number:

descripcion_web.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView webView, String url) {
            super.onPageFinished(webView, url);
            RelativeLayout marco = (RelativeLayout)findViewById(R.id.descripcionRelative);
            System.out.println("Height webView: "+webView.getHeight());   
            System.out.println("Height relative: "+marco.getHeight());            
        }           
    });

I try to call it in the method in onResume() but it doesn't work.

Another attempt to solve the problem, is set android:layout_height to match_parent and use a View method setMaximumHeight(), but It doesn't exist. However, it does exists setMinimumHeight()...

How can I resolve this? Thank you very much!

like image 736
Josue Avatar asked Aug 31 '11 15:08

Josue


1 Answers

Unfortunately there is no out of box "setMaximumHeight" for the most Android views. But you can implement such inheriting from the WebView. Here is an example how you can do that:

package com.am.samples.maxheight;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class CustomWebView extends WebView {

    private int maxHeightPixels = -1;

    public CustomWebView(Context context, AttributeSet attrs, int defStyle,
            boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomWebView(Context context) {
        super(context);
    }

    public void setMaxHeight(int pixels) {
        maxHeightPixels = pixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (maxHeightPixels > -1 && getMeasuredHeight() > maxHeightPixels) {
            setMeasuredDimension(getMeasuredWidth(), maxHeightPixels);
        }
    }
}

Hope this helps!

like image 188
Denys Nikolayenko Avatar answered Sep 21 '22 16:09

Denys Nikolayenko