Program-Me-Rev
Program-Me-Rev

Reputation: 6634

How to add a border to a particular side of a view

This is how I go about adding a border to a EditText. How can I go about adding a border only on one side of a EditText, and define the color and width of the border?

EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);

GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF);  // white background
border.setStroke(1, 0xFF000000);  // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    editText.setBackgroundDrawable(border);
} else {
    editText.setBackground(border);
}

Vielen dank im voraus.

Upvotes: 0

Views: 443

Answers (2)

Casablancais
Casablancais

Reputation: 243

To get border on one side, you can create your own drawable like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#FF0000" />
    </shape>
  </item>
  <item android:right="5dp">
    <shape android:shape="rectangle">
      <solid android:color="#FFFF" />
    </shape>
  </item>
</layer-list>

And set this drawable as background to your EditText.

Upvotes: 1

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8315

If you want to add border only one side of EditText then you should use View tag in your layout file to draw a simple line and place it near your EditText and use background property to set color of line. To draw horizontal line use this:

 <View
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="2dp" />

Upvotes: 0

Related Questions