Adding Shape to LinearLayout Android

35,723

Solution 1

You should use a ShapeDrawable for the background of the component you want to add the style to.

A shape drawable is created in XML, with the following syntax (this one shows a rectangle, with the rounded corners, but there is lots you can do to customize this to look however you want). This file (named background_square.xml or whatever) should be put in your drawable folder:

 <?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <solid
        android:color="@color/primary_grey" /> 
</shape>

Then, when you want to add it to a View, you can use the following syntax in your XML:

android:background="@drawable/background_square"

Solution 2

You can do something like below

if you want to add rectangle simply add nested layout(say linearlayout) within your layout and set android:background="yourcolor" //you can add color using hash color values

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:padding="5dp"
android:id="@+id/layout">

<AutoCompleteTextView android:id="@+id/autocompleteCountry"
  android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/CONTRY_LABEL"
   />
<AutoCompleteTextView android:id="@+id/locationAutoCompleteFrom"
  android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/from"
    android:visibility="gone"
   />
//suppose you want to add your rectangle here
<LinearLayout 
android:id="@+id/rectangle"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"    
android:background="yourcolor"
>
</LinearLayout>

you change all the relative properties as you want say size,margins,etc

Share:
35,723

Related videos on Youtube

Khayam Gondal
Author by

Khayam Gondal

Updated on July 09, 2022

Comments

  • Khayam Gondal
    Khayam Gondal almost 2 years

    I have a linearLayout having some autocomplete and textboxes. I want to insert a shape (rectangle) in linearlayout. How can i achieve this. I am new comer to android.

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    
        android:padding="5dp"
        android:id="@+id/layout">
    
        <AutoCompleteTextView android:id="@+id/autocompleteCountry"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/CONTRY_LABEL"
           />
        <AutoCompleteTextView android:id="@+id/locationAutoCompleteFrom"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/from"
            android:visibility="gone"
           />
       <AutoCompleteTextView android:id="@+id/locationAutoCompleteTO"
          android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/to"
            android:visibility="gone"
           />
     <!--    <Button android:id="@+id/buttonRoute"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/buttonRouteText"
           android:enabled="false"
           android:clickable="true"
           />
       -->
    

  • Mario Lenci
    Mario Lenci about 11 years
    Actually your on the right path but there is no point in using a LinearLayout if you just what to add a rectagle. Use View instead, it has all the main attributes you'd use to style a LineraLayout and it's probably lighter in terms of computation in the layout process
  • arne.jans
    arne.jans almost 11 years
    I used this shape as the source of an ImageView, but otherwise it is fine! Thanks!
  • Booger
    Booger almost 11 years
    You can add this background to any view - ImageView, TextView, of LinearLayout (in this case, all Views are the same as far as Android is concerned).