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"
/>
-->
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.
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.
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.
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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With