Android Dynamic Layout Size Setting

In an Activity with many components, I have a RelativeLayout that has a WebView (it shows plain text that I don't know its size).

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 android: layout_height of RelativeLayout attribute is 125dip, because if the text is too large, I want to delimit 125dip. If the text is large, I will see the text with a scroll. Fine!

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

One solution is to change android: layout_height RelativeLayout to wrap_content. If the text is short, the component will have exact pixels, but if the text is too large, I cannot distinguish it.

BIG PROBLEM: I cannot calculate the height of the WebView. If I do: descripcion_web.getHeight (), it returns 0.

If I call this method here, it does not return the well 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 am trying to call a method in onResume (), but it does not work.

Another attempt to solve the problem is to set android: layout_height for match_parent and use the View setMaximumHeight () method, but it does not exist. However, there is setMinimumHeight () ...

How can i solve this? Thank you very much!

+5
source share
1 answer

Unfortunately, for most Android views, there is no "setMaximumHeight". But you can implement such inheritance from WebView. Here is an example of how you can do this:

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!

+7
source

All Articles