How to give border to a text field and other decoration to the borders like round edges.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our W3Make Forum to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
1.) Create a drawable XML file in your project’s “res/drawable” directory, and create a new XML file to define the border’s appearance.
2.) Open the XML file and define the shape of the drawable. For a rounded border, you can use the “” tag and set the shape like a rectangle
3.) You can also specify the desired border width, colour, and corner radius using attributes such as “android:strokeWidth”, android:strokeColor, and android:corners
4.) Then apply the changes activity_main.xml file, in the Edittext add the “android:background” attribute and set it to the reference of the drawable XML file you have made.
Start Android Studio and open your project.
Add the following code to your layout XML file, activity_main.xml, to create the TextField:
<EditText
android:id=“@+id/editText”
android:layout_width=“match_parent”
android:layout_height=“wrap_content” android:background=“@drawable/text_field_border”
android:padding=“8dp” android:hint=“Enter text”/>
For the border drawable, make a new XML file. Drawable resource file by selecting “New” from the context menu of the res directory. Click OK after giving the file a name, such as text_field_border.xml.
Add the following code to the text_field_border.xml file by opening it:
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<solid android:color=”#FFFFFF”/>
<stroke android:color=”#000000″ android:width=”2dp”/>
<corners android:radius=”8dp”/> <!– Rounded corner radius –>
</shape>
Make the attributes your own according to your preferences. The background colour, border colour, width, and corner radius can all be modified.