Skip to main content

How to create a circular EditText View in android

You obsrved several app use the Circlular View with rounded corner for taking user input edittext. In this post we are going to explore to create a Circular EditText view for user input. Let's start the creation of the Circular EditText View.




Create The Circlular EditText Demo Project





Creating the new project is everyday work if you are a mobile app developer, but just adding steps who is new in the android mobile app development.





For creating the new project go to the File menu in android studio and click on File -> New-> New Project. In the next windows select the Activity Type I will in this demo will select Empty Activity. Choose the Application name like Circle EditText Demo and Finish.





Attach the EditText and Button to layout





Now, We need to create an EditText and Button into the main layout located inside the res->layout directory.





Circular Image View Project






Now, add the EditText and button to the activity_main.xml. In this example, androidx is used so you can migrate to androidx or modify the layout like below.





<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.appcompat.widget.AppCompatEditText
android:layout_marginTop="16dp"
android:hint="Ready to be Circular"
android:id="@+id/circular_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="12"
android:textSize="24sp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button

android:text="Click Me"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_click_me"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:textSize="18sp"
android:textColor="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="@+id/circular_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>




Circular Edit Text without change




Create Background Drawable Resource for Circular EditText





So far we have created simple EditText and button and we need to create a background drawable resource to convert the normal editText to a Circular EditText.





First of all, create a background resource file with rectangle shape like below.





<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
</shape>




Create the border of the shape. If you want no border just set the width to 0dp.





  <stroke
android:width="2dp"
android:color="@color/colorPrimary"
/>




Round the corner of the rectangle. You can change the radius as per requirement.





 <corners android:radius="360dp"/>




Now, We will add some padding to the shape.





<padding
android:left="10dp"
android:right="10dp"
android:bottom="5dp"
android:top="5dp"
/>




Set the background color of the background drawable.





<solid
android:color="#FFFFFF"
/>




Below is the complete circular_edit_text.xml





<!-- circular_edit_text.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/colorPrimary"
/>
<corners
android:radius="360dp"
/>
<padding
android:left="10dp"
android:right="10dp"
android:bottom="5dp"
android:top="5dp"
/>
<solid
android:color="#FFFFFF"
/>
</shape>




So, we have created our background resource for our circular EditText now set it as the background of the EditText.





 android:background="@drawable/circular_edit_text"




Circular EditText  Wtih Border




Let's run the project and check the view now we have circular EditText as per the above image. Some description of the background resource file as given below so that you can customize it as per requirement.





Borderless Circular EditText





As described above we can create the borderless Circular EditText as well by just making the stoke to 0dp.









Create Circular TextView





For creating the circular text view we just need to set the created drawer as background like below.





<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/dummy_string"
android:id="@+id/circular_text"
android:ems="12"
android:gravity="center"
android:textSize="16sp"
android:layout_marginStart="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/circular_edit_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="24dp"/>








Wrapping Up





So for creating the custom background of any type of view we need to create the background drawable and set it as the background of the View. What we have done is create a TextView, EditText, and Button as normal View. Then we have to change the background of that view in the XML layout file.


Comments

  1. You have shared a nice article here about the migrate to androidx. Your article is very informative and useful for those who are interested to know more about the process of creating a circular EditText View in android. Thank you for sharing this article here.

    ReplyDelete

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s...

How to Read and Write JSON data in Kotlin with GSON

Kotlin is now official language for Android development and it is well supported in Android Studio. So here in this tutorial, we are going to learn about how to read and write JSON data in Kotlin using GSON. If you would like to learn the java version of this tutorial check out the last tutorial " How to Read and Write JSON data using GSON ". Introduction In this tutorial, we will write two methods. In the first method, we will create a JSON file and in second method we will read the file and print in a text box.  If you like to know more about the basic entity of JSON you can check out Basic JSON entity here . What is JSON? JSON stands for J ava S cript O bject N otation JSON is a lightweight data-interchange format It is "self-describing" and easy to understand JSON is language independent and can we used in any language JSON Syntax Rules Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold ...

Scan and List All Available WiFi Network In Android

In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. You May Like Below Topic: How to Read and Write JSON data using GSON WP Android App using REST and volley WP Android App using REST and volley part2 Implementation of SwipeRefreshLayout in RecyclerView Create Amazing Bottom Navigation Bar Without Any External Library Introduction In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. We will add this functionality to our existing Demo app " Video Gallery ". If you would like to check out t...