Android TextureView Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.
Hello Readers! In this post, we are going to learn about android TextureView using kotlin in any android application. We will also learn about different attributes of android TextureView that can be used to customise this widget.

It would be helpful for you if you already know about TextView Widget.

Output

Tutorialwing TextureView Output of textureview using kotlin tutorial with example

TextureView Output

Source Code in Java And Kotlin

You can download source code of this tutorial on android TextureView using Kotlin by entering the details –

Getting Started

Android TextureView can be defined as below –

A TextureView is subclass of View Class that can be used to display content stream such as video or or OpenGL scene. Content steam can come from application’s process or remove process.

Note – TextureView can only be used in hardware accelerated window.

How to use TextureView

All you have to do is get it’s surfaceTexture. Then, surfaceTexture can be used to render the content. You can use getSurfaceTexture() or TextureView.SurfaceTextureListener to get the surfaceTexture.

Please note that surfaceTexture is available only after textureView is attached to window. So, it is advisable to use listener to be notified when surfaceTexture is available.

Also, only one producer can use textureView. So, you are using textureView for camera preview, you can not use lockCanvas() to draw onto textureView at same time.

Different Attributes of Android TextureView Widget

Some of the popular attributes of TextureView are –

Sr. XML Attributes Description
1 android:background Sets background of the view.
2 android:backgroundTintMode Sets blending mode used to apply the background tint.
3 android:clickable Sets whether view is clickable or not
4 android:elevation Sets z-depth of the view
5 android:id Sets id of the view
6 android:padding Sets padding of the view



Example of Android TextureView Using Kotlin

At first, we will create android application. Then, we will use textureView using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as TextureView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use textureView using kotlin in the application.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">TextureView</string>
    <string name="permission_required">Camera permission required to run application.</string>
</resources>

3. Modify Layout in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</FrameLayout>

Here, we have modified the layout in activity_main.xml file. We have added textView that will be used to display message if necessary.

4. Setup For Camera Permission At Runtime.

You need to add camera permission for this app because you must have access to camera of the device for this application to work.

So, add below code in AndroidManifest.xml to get permission to access camera by the user.

<uses-permission android:name="android.permission.CAMERA"/>

Also, you need to write code to get permission at runtime (for device having android version >= Marshmallow).

So, open src/main/java/com.tutorialwing.textureview/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.textureview

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.graphics.SurfaceTexture
import android.hardware.Camera
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.TextureView
import android.widget.FrameLayout
import android.widget.TextView

import java.io.IOException

import android.Manifest.permission.CAMERA

class MainActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {

    private var mTextureView: TextureView? = null
    private var mCamera: Camera? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val result = checkPermission()
        if (result) {
            // perform something if permission is granted.
        }
    }

    private fun checkPermission(): Boolean {
        val currentAPIVersion = Build.VERSION.SDK_INT
        if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, CAMERA) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA)) {
                    showPermissionAlert()
                } else {
                    ActivityCompat.requestPermissions(this@MainActivity, arrayOf(CAMERA), CAMERA_REQUEST_CODE)
                }
                return false
            } else {
                return true
            }
        } else {
            return true
        }
    }

    private fun showPermissionAlert() {
        val alertBuilder = AlertDialog.Builder(this)
        alertBuilder.setCancelable(true)
        alertBuilder.setTitle("Permission Required")
        alertBuilder.setMessage("Permission to access camera is needed to run this application")
        alertBuilder.setPositiveButton(android.R.string.yes) { dialog, which -> ActivityCompat.requestPermissions(this@MainActivity, arrayOf(CAMERA), CAMERA_REQUEST_CODE) }
        val alert = alertBuilder.create()
        alert.show()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            CAMERA_REQUEST_CODE -> if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Write code to setup textureView if permission is granted by user.
            } else if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                // // Write code to perform other task if permission is not granted by user.
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    companion object {

        val CAMERA_REQUEST_CODE = 100
    }
}

As you can see, we have written code to get camera permission at runtime. We will setup textureView only if permission is granted. Otherwise, we will show some message to the user.



5. Create TextureView Widget in Kotlin file

Now, we will write code to show textureView if permission is granted. We will also set a listener that is needed to perform some action when surfaceTexture is available. We can do these as below –

TextureView mTextureView = TextureView(this)
mTextureView!!.surfaceTextureListener = this
setContentView(mTextureView)

Also, implement the listener as below –


class MainActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {

    // Some other code..
    // ...

    override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, i: Int, i1: Int) {
        // Perform action when surfaceTexture is available. For example, start camera etc. 
    }

    override fun onSurfaceTextureSizeChanged(surfaceTexture: SurfaceTexture, i: Int, i1: Int) {
        // Ignored, Camera does all the work for us
    }

    override fun onSurfaceTextureDestroyed(surfaceTexture: SurfaceTexture): Boolean {
        // Perform action when surfaceTexture is destroyed. For example, stop camera, release resources etc.
        return true
    }

    override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) {
        // Invoked every time there's a new Camera preview frame.
    }

Finally, our MainActivity.kt file will look like below –
Open src/main/java/com.tutorialwing.textureview/MainActivity.kt file and check the file.

package com.tutorialwing.textureview

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.graphics.SurfaceTexture
import android.hardware.Camera
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.TextureView
import android.widget.FrameLayout
import android.widget.TextView

import java.io.IOException

import android.Manifest.permission.CAMERA

class MainActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {

    private var mTextureView: TextureView? = null
    private var mCamera: Camera? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val result = checkPermission()
        if (result) {
            setTextureView()
        }
    }

    private fun setTextureView() {
        mTextureView = TextureView(this)
        mTextureView!!.surfaceTextureListener = this
        setContentView(mTextureView)
    }

    private fun setMessageView() {
        setContentView(R.layout.activity_main)
        val textView = findViewById<TextView>(R.id.info)
        textView?.setText(R.string.permission_required)
    }

    private fun checkPermission(): Boolean {
        val currentAPIVersion = Build.VERSION.SDK_INT
        if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, CAMERA) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA)) {
                    showPermissionAlert()
                } else {
                    ActivityCompat.requestPermissions(this@MainActivity, arrayOf(CAMERA), CAMERA_REQUEST_CODE)
                }
                return false
            } else {
                return true
            }
        } else {
            return true
        }
    }

    private fun showPermissionAlert() {
        val alertBuilder = AlertDialog.Builder(this)
        alertBuilder.setCancelable(true)
        alertBuilder.setTitle("Permission Required")
        alertBuilder.setMessage("Permission to access camera is needed to run this application")
        alertBuilder.setPositiveButton(android.R.string.yes) { dialog, which -> ActivityCompat.requestPermissions(this@MainActivity, arrayOf(CAMERA), CAMERA_REQUEST_CODE) }
        val alert = alertBuilder.create()
        alert.show()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            CAMERA_REQUEST_CODE -> if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setTextureView()
            } else if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                setMessageView()
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, i: Int, i1: Int) {
        mCamera = Camera.open()

        val previewSize = mCamera!!.parameters.previewSize
        mTextureView!!.layoutParams = FrameLayout.LayoutParams(previewSize.width, previewSize.height, Gravity.CENTER)

        try {
            mCamera!!.setPreviewTexture(surfaceTexture)
            mCamera!!.startPreview()
        } catch (ioe: IOException) {
            // Something bad happened
        }

        mTextureView!!.alpha = 1.0f;
        mTextureView!!.rotation = 90.0f;
    }

    override fun onSurfaceTextureSizeChanged(surfaceTexture: SurfaceTexture, i: Int, i1: Int) {
        // Ignored, Camera does all the work for us
    }

    override fun onSurfaceTextureDestroyed(surfaceTexture: SurfaceTexture): Boolean {
        mCamera!!.stopPreview()
        mCamera!!.release()
        return true
    }

    override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) {
        // Invoked every time there's a new Camera preview frame.
    }

    companion object {

        val CAMERA_REQUEST_CODE = 100
    }
}

Here, we are showing how to use textureView using kotlin in android application. As you can see, how we are getting the permission at runtime. Then, we are setting up textureView and showing the camera when surfaceTexture is available. Also, releasing the resources when surfaceTexture is destroyed.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Finally, code inside main/AndroidManifest.xml file will look like below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.textureview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<uses-permission android:name="android.permission.CAMERA"/>

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android TextureView using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners