2

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 ?

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61
  • 1
    What have you tried? `android:elevation`? A nine-patch background image? Something else? – Karakuri Oct 14 '16 at 05:12
  • 1
    You have to use card view as parent and in that you have to use linear layout with image and text.set elevation as you want in cardview property – Vishal Thakkar Oct 14 '16 at 05:13
  • 1
    Wrap up your LinearLayout with a `CardView` widget instead. – Paresh P. Oct 14 '16 at 05:14
  • 1
    didn't see your efforts !! anyway ,refer recycle view ,that used with card view .that is the exactly gives same UI as you want. – Radhey Oct 14 '16 at 05:14
  • you can also create a shape for drawable and set it as your `android:background` for the layout – Reginald Oct 14 '16 at 05:16
  • you just need grid view with custom adapter. and set layout in adapter which i was posted – Vishal Thakkar Oct 14 '16 at 05:35
  • I found an answer for the problem. Here https://stackoverflow.com/a/24095309/4924272 and I take it from this question https://stackoverflow.com/questions/24095223/android-linearlayout-add-border-with-shadow-around-a-linearlayout – Terance Wijesuriya Oct 14 '16 at 06:04

3 Answers3

18

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.

Polar
  • 3,327
  • 4
  • 42
  • 77
4

Try like this:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_5sdp"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="@dimen/_3sdp">

        <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.support.v7.widget.CardView>
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
3

Use card view if you want shadow effect . Add card elevation property card_view:cardElevation="4dp" set dp as much as you want.

Or add this drawable file in your drawable folder.

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

    <gradient
        android:angle="0"
        android:centerColor="#ffffff"
        android:centerX="35%"
        android:endColor="#ffffff"
        android:startColor="#ffffff"
        android:type="linear"/>

    <size
        android:width="80dp"
        android:height="80dp"/>
    <stroke
        android:width="2dp"
        android:color="#eaeaea"/>
</shape>
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Gevaria Purva
  • 552
  • 5
  • 15