1

I have a class that extends RelativeLayout. Inside this I have another relative layout which has a certain height and width and inside that I have a webview and a button. This button is positioned at the top right corner of the 2nd relative layout. I am setting the webview's position at the top, left, right, bottom and center of the 2nd relative layout based on an enum that is passed in a switch case. The problem I'm facing is that when I set the webview at the top it is overlapping the button and the button is not visible. This is my code:

enum position {

    Top, Bottom, Left, Right, Center
};

public class Sample extends RelativeLayout {
RelativeLayout layout = new RelativeLayout(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                width, height);
        addView(layout, params);

        closeButton = new Button(context);
        closeButton.setId(1);
        closeButton.setText("X");
        RelativeLayout.LayoutParams child1Params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        child1Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        child1Params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layout.addView(closeButton, child1Params);

        closeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                closePressed();
            }
        });

        RelativeLayout.LayoutParams child2Params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        switch (position) {

        case Top:


            webView = new WebView(context);
            child2Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layout.addView(webView, child2Params);
            break;

        case Bottom:


            webView = new WebView(context);
            child2Params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            layout.addView(webView, child2Params);
            break;

        case Left:


            webView = new WebView(context);
            child2Params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layout.addView(webView, child2Params);
            break;

        case Right:


            webView = new WebView(context);
            child2Params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layout.addView(webView, child2Params);
            break;

        case Center:


            webView = new WebView(context);
            child2Params.addRule(RelativeLayout.CENTER_VERTICAL);
            layout.addView(webView, child2Params);
            break;

        default:


            webView = new WebView(context);
            child2Params.addRule(RelativeLayout.CENTER_VERTICAL);
            layout.addView(webView, child2Params);
            break;
        }
}

Could you please tell me as to how to set the webview below the button always such that it is displayed always below the button and the button is not hidden below it? Thank you.

Ingrid Cooper
  • 1,191
  • 3
  • 16
  • 27
  • check this out..http://stackoverflow.com/questions/2305395/laying-out-views-in-relativelayout-programmatically – hemanth kumar Aug 21 '12 at 09:54
  • Thank you for your reply. I tried giving RelativeLayout.LayoutParams child2Params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); child2Params.addRule(RelativeLayout.BELOW, closeButton.getId()); But its still not working. The webview is overlapping the button. – Ingrid Cooper Aug 21 '12 at 10:09

0 Answers0