Search code examples
androidandroid-studioborderandroid-linearlayout

Android: Border for LinearLayout


I have a LinearLayout and I want to put a border for it . It should look like below.

enter image description here

This is my layout,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.AddInfoMainActivity"
    tools:showIn="@layout/app_bar_main">


    <LinearLayout
        android:id="@+id/add_info_layout_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/add_info_layout_one_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/add_info_layout_one_img01"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="24dp"
                android:src="@drawable/sample01" />

            <TextView
                android:id="@+id/add_info_layout_one_txtView01"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:text="@string/add_info_txt_one_01"
                android:textAlignment="center"
                android:textSize="20sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/add_info_layout_one_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/add_info_layout_one_img02"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="24dp"
                android:src="@drawable/sample02" />

            <TextView
                android:id="@+id/add_info_layout_one_txtView02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:text="@string/add_info_txt_one_02"
                android:textAlignment="center"
                android:textSize="20sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp" />

        </LinearLayout>


    </LinearLayout>
</RelativeLayout>

Util to now I haven't use any style for my layout.

Have any ideas ?


Solution

  • To set border you can create an xml file in your drawable with the below code.

    border.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle"> 
            <stroke android:width="5dip" android:color="@android:color/white" />   
    </shape>
    

    and from your linearlayout set your background

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com....
        android:background="@drawable/border"> 
    
        <!--- code here --->
    
    </LinearLayout>
    

    but base on your image you are looking for cardView. Try to search for cardview.

    click this link for example and tutorial for cardview.