Open In App

How to Insert Image to Screen at the Touched Coordinates in Android?

Last Updated : 25 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn to place an image on the touched coordinates on the screen. We can insert multiple images on the screen. onTouchListener will be used to get the coordinates of the touched position on the screen so that the image can be inserted at that position. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Set RelativeLayout in the layout

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.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</RelativeLayout>


Step 3: 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




import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    lateinit var areaLayout: RelativeLayout
  
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        areaLayout = findViewById(R.id.layout)
  
        areaLayout.setOnTouchListener { view, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                val x = event.x.toInt()  // get x-Coordinate
                val y = event.y.toInt()  // get y-Coordinate
                val lp = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT) // Assuming you use a RelativeLayout
                val iv = ImageView(applicationContext)
                lp.setMargins(x, y, 0, 0) // set margins
                iv.layoutParams = lp
                iv.setImageDrawable(resources.getDrawable(R.drawable.flower)) // set the image from drawable
                (view as ViewGroup).addView(iv) // add a View programmatically to the ViewGroup
            }
            true
        }
    }
}


Now, run the app

Output:

Source Code: Click Here



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads