How to Add Image on EditText in Android?
Last Updated :
18 Feb, 2022
In Android, An EditText is an overlay over a TextView that makes it editable using text by invoking a soft keyboard in run time. It is generally implemented to collect text data from the user. EditText is most commonly used in forms that require users to fill in details and for passing a search query.
Add Image on EditText
In this article, we will show you how you could add an Image inside an EditText in Android. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add an Image in res > drawable folder
Navigate to the app > res > drawable folder and add any image or vector of your choice. In this demonstration, we added a search vector from vector assets. If you are new to adding a vector asset, then refer to the following article that explains creating and adding a vector asset in your Android project: How to Add Vector Assets in Android Studio?
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add an EditText as shown below.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text_1"
android:layout_width="match_parent"
android:layout_height="50sp"
android:hint="Type something..."/>
</RelativeLayout>
Method 1: Adding EditText attribute to add a drawable
Add drawableRight attribute to add the image to the right of the EditText.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text_1"
android:layout_width="match_parent"
android:layout_height="50sp"
android:hint="Type something..."
android:drawableRight="@drawable/ic_baseline_search_24"/>
</RelativeLayout>
Method 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.geeksforgeeks.addimageinedittext
import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
class MainActivity : AppCompatActivity() {
@SuppressLint("UseCompatLoadingForDrawables")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring and initializing the
// EditText from the layout file
val mEditText = findViewById<EditText>(R.id.edit_text_1)
// Initializing the drawable and
// setting it inside the EditText
val mDrawable: Drawable = getDrawable(R.drawable.ic_baseline_search_24)!!
mEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, mDrawable, null)
}
}
Output:
In both cases, the output is the same as shown below.
Similar Reads
How to Create an ImageButton in Android? Nowadays, image buttons play a big role in making the android application more interactive and user-friendly. Be it any social media app like Instagram or Facebook or any shopping app or streaming app, each application uses this feature widely. In this article, we will take a look at the implementat
3 min read
How to Create an ImageButton in Android? Nowadays, image buttons play a big role in making the android application more interactive and user-friendly. Be it any social media app like Instagram or Facebook or any shopping app or streaming app, each application uses this feature widely. In this article, we will take a look at the implementat
3 min read
Android EditText in Kotlin EditText is a widget in Android, that is used to get input from the user. EditText is commonly used in forms and login or registration screens. EditText extends the TextView class and provides more functionalities including handing text inputs such as cursor control, keyboard display and text valida
3 min read
How to Rotate an Image to a Certain Angle in Android? In Android, ImageView is used to display images. Images can be of any type and can be fetched locally or from a network. Images are displayed without any operations in the ImageView. However, images can be rotated to a certain angle and displayed in ImageView. So in this article, we will show you ho
2 min read
Working With the EditText in Android EditText is one of the basic UI widgets, which is used to take the input from the user. The EditText is derived or is the extension of the TextView in Android. This article its been discussed in detail about the EditText in Android. The article also contains some of the redirections to other article
9 min read
How to Resize a Bitmap in Android? ImageViews are used within the android application to display different types of images. Many times images are displayed within the ImageView using bitmap instead of drawable files. In this article, we will take a look at How to Resize Bitmap in the android application to resize our image to be disp
3 min read