TextView is an essential object of an Android application. It comes in the list of some basic objects of android and used to print text on the screen. In order to create better apps, we should learn that how can we create a TextView which have a background with rounded corners. We can implement this skill in a practical manner for any android application. For creating professional-looking user interfaces, we need to take care of these small things.
In this article, You will be learning how to create a TextView with Rounded Corners. First of all, we need to create a drawable resource file, which has a proper definition to make TextView corners rounded, and then we have to add a background attribute to that special TextView object. Let’s do it in steps!
Step by Step Implementation
Step 1: Create a new android studio project, and select an empty activity. You can also refer to this GFG Tutorial for Creating a new android studio project.
Step 2: Make sure that you have selected the Android option for project structure on the top left corner of the screen, then go to the res/drawable folder.
Step 3: Here right-click on the drawable folder and click on new and select drawable resource file. Give it a name of your choice, we are giving it rounded_corner_view.
Note: In android studio you can’t give a name of a resource file (Layout file, Color File, Image File, or any XML file) in uppercase and camelCase, you have to follow the lowercase letters only and it’s a good habit to use lowercase letters with underscores in place of spaces.
Step 4: Now in the drawable resource file, which you have just created, remove all the default code and paste the following code.
Step 5: Now go to the activity_main.xml file and add an attribute to that TextView, for which you want to add rounded corners. The attribute is android: background=”@drawable/rounded_corner_view”.
Tip: Don’t forget to replace your drawable resource file name if you have given a different name and you don’t need to add xml extension after file name.