Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Shape to LinearLayout Android

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"
       />
   -->

like image 290
Khayam Gondal Avatar asked Apr 22 '13 14:04

Khayam Gondal


People also ask

How do I add a border to a linear layout?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.

What are android drawables?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is LinearLayout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.


2 Answers

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"
like image 191
Booger Avatar answered Nov 10 '22 02:11

Booger


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

like image 44
karan Avatar answered Nov 10 '22 00:11

karan