Android: Custom border for FrameLayout
I am trying to add a custom border to a framelayout
. framelayout
used as a container for a fragment. Thus, I want to display a fragment with a border. The border must be outside the framelayout and must not consume space inside the framelayout. And it has to adjust as the screen resizes.
What I can think of now is some kind of custom ViewGroup, add an ImageView for the border. Use image processing to get the inner area of the image and inflate the frame in that area.
I'm looking for an easy way out.
The simplest solution for this is to create a 9-patch patch and set it as the background for your FrameLayout. I have created the below 9 patch that you can use. Hopefully this will be enough.
The effect can be achieved with an XML layout using for example LinearLayout
with the correct layout_weight, so your very center FrameLayout will expand to make available space.
There are 4 highlighted elements used: for the top, bottom, left and right parts of the frame.
Check this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/border_left" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/border_top" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- FrameLayout content goes here -->
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/border_top" />
</LinearLayout>
<ImageView
android:layout_width="wrap_conttXY"
android:layout_height="match_parght"
android:scaleType="fient"
android:src="@drwable/border_right" />
</LinearLayout>