Search code examples
androidandroid-layouttextviewandroid-custom-view

Custom Textview in Android with height = fill_parent but having the background fill only wrap_content


So, the title is a little confusing, I know, but what I want is basically a notification box that has a colored background and borders that appears on the center of the screen, but is not stretched.

I already have the custom view, making the background and borders, but if I make "width: fill_parent" and "height: wrap_content", it will appear on the top of the screen, and if I put height to fill_parent, the entire screen will be filled with the background (which I kinda knew it would happen).

I also tried a simpler approach, setting layout_gravity to center, but didn't help. The view is inside a vertical linear layout and it's the only visible view within it (all the other elements have visibility = View.GONE).

How should I code the onDraw() of the custom view to be able to put height: fill_parent and have the background to obey the wrap_content rule? A simpler, better solution is always welcome as well.


Solution

  • I think the problem is that it's nested in a LinearLayout.

    Try it in a FrameLayout which

    "is designed to block out an area on the screen to display a single item"

    You should be able to then center your custom view over a transparent background FrameLayout

    EDIT: To clarify why I think the LinearLayout is the problem. A LinearLayout fills up like a stack which is why when you tried to center by gravity the item was at the top. If you imagine a stack of plates... you can't start in the middle :-)