Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Border only on corners

Tags:

android

In my android app, I need border only on four corners as shown in fig (white colored).

enter image description here

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="6">
        <VideoView
            android:id="@+id/video1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" />
        <View
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:background="@drawable/cornerbg" />
    </RelativeLayout>

cornerbg.xml

        <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

        <item>
            <shape android:shape="rectangle" >
                <solid android:color="@color/transparent" />
                <stroke
                    android:width="1dp"
                    android:color="@color/btn_border"
                    android:dashWidth="50dp" // I tried this but not getting expected 
                    android:dashGap="60dp"/>

            </shape>
        </item>
        </layer-list>

How can I write xml for this?

Please help, Thanks

like image 980
Erma Isabel Avatar asked Apr 05 '14 07:04

Erma Isabel


2 Answers

Below is the drawable that doesn't depends on the width and height of the rectangle, you can use this drawable for portrait and landscape. Please adjust the width and height of the border :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="8dp"
        android:height="48dp"
        android:gravity="top|left">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="48dp"
        android:height="8dp"
        android:gravity="top|left">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="8dp"
        android:height="48dp"
        android:gravity="top|right">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="48dp"
        android:height="8dp"
        android:gravity="top|right">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="8dp"
        android:height="48dp"
        android:gravity="bottom|left">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="48dp"
        android:height="8dp"
        android:gravity="bottom|left">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="8dp"
        android:height="48dp"
        android:gravity="bottom|right">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item
        android:width="48dp"
        android:height="8dp"
        android:gravity="bottom|right">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>
like image 57
kenn3th Avatar answered Oct 01 '22 18:10

kenn3th


I know it's a question of a long time ago, but I had the same problem.

I solved by creating secondary layout and including in my main layout.

I hope this solution can help somebody.

main_layout.xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="6">

    <VideoView
        android:id="@+id/video1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

    <include layout="@layout/camera_box_layout" />

</RelativeLayout>

camera_box_layout.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="40dp"
    android:background="@android:color/transparent">

    <!-- Top horizzontal lines -->
    <View
        android:layout_width="100dp"
        android:layout_height="2dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/darker_gray" />

    <View
        android:layout_width="100dp"
        android:layout_height="2dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/darker_gray" />

    <!-- Top vertical lines -->
    <View
        android:layout_width="2dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/darker_gray" />

    <View
        android:layout_width="2dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/darker_gray" />

    <!-- Bottom horizzontal lines -->
    <View
        android:layout_width="100dp"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/darker_gray" />

    <View
        android:layout_width="100dp"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/darker_gray" />

    <!-- Bottom vertical lines -->
    <View
        android:layout_width="2dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/darker_gray" />

    <View
        android:layout_width="2dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/darker_gray" />
</RelativeLayout>
like image 28
Lorenzo Vincenzi Avatar answered Oct 01 '22 18:10

Lorenzo Vincenzi