Augmented Reality (AR) App in Kotlin: Place a Block Using ARCore
In this tutorial, we'll create a simple Android AR app using Kotlin that uses Google's ARCore to place a 3D block in the center of the camera view. The app will show a "ghost" block until you tap the screen to place it permanently.
🧰 Requirements
- Android Studio (latest version)
- Kotlin plugin enabled
- A physical Android device with ARCore support (list here)
- Google Play Services for AR installed on the device
- Basic understanding of Kotlin and Android development
🛠️ Tools Used
- ARCore: Google’s platform for building AR experiences.
- Sceneform SDK (deprecated but still useful) or Sceneform UX for rendering 3D objects.
- We’ll use Sceneform UX which is part of the newer Google Sceneform Tools.
⚠️ Note: Sceneform was deprecated by Google in 2019, but it's still one of the easiest ways to get started with AR in Java/Kotlin without diving into OpenGL or Unity.
📦 Step 1: Set Up Your Project
1. Create a new project in Android Studio
- Language: Kotlin
- Minimum SDK: API 24 (Nougat) or higher
- Empty Activity
2. Add ARCore Dependency
Open build.gradle (Module) and add:
dependencies {
implementation 'com.google.ar:core:1.35.0'
implementation 'com.google.ar.sceneform:sceneform-base:1.17.1'
}
Make sure to sync Gradle after adding dependencies.
🗺️ Step 2: Update Manifest
Add these lines inside <application> tag in AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.ar" android:required="true"/>
<meta-data
android:name="com.google.ar.core"
android:value="required" />
🖼️ Step 3: Design Layout (activity_main.xml)
Replace content with:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/ux_fragment"
android:name="com.google.ar.sceneform.ux.ArFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tap_to_place_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap to Place Block"
android:textSize="18sp"
android:layout_gravity="center"
android:padding="16dp"
android:background="#88000000"
android:textColor="#FFFFFF"/>
</FrameLayout>
🧱 Step 4: Load a 3D Model (Ghost Block)
We’ll use a .sfb file (Sceneform Binary), which is a compiled 3D asset.
You can either:
- Use a cube model exported from Blender or any 3D tool
- Or download a sample
.sfb cube file
Place your block.sfb file in:
app/src/main/assets/block.sfb
If you don’t have one, you can generate a simple cube using Sceneform Plugin in Android Studio or follow the steps below.
🎮 Step 5: Code – MainActivity.kt
package com.example.arblockplacer
import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.ar.core.Plane
import com.google.ar.sceneform.AnchorNode
import com.google.ar.sceneform.Node
import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.rendering.ModelRenderable
import com.google.ar.sceneform.ux.ArFragment
import com.google.ar.sceneform.ux.TransformableNode
class MainActivity : AppCompatActivity() {
private lateinit var arFragment: ArFragment
private lateinit var ghostBlock: Node
private var placed = false
private var currentAnchorNode: AnchorNode? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
arFragment = supportFragmentManager.findFragmentById(R.id.ux_fragment) as ArFragment
// Show ghost block in the center
setupGhostBlock()
// Handle taps
arFragment.setOnTapArPlaneListener { hitResult, plane, _ ->
if (plane.type != Plane.Type.HORIZONTAL_UPWARD_FACING || placed) return@setOnTapArPlaneListener
// Remove ghost block
ghostBlock.isEnabled = false
ghostBlock.setParent(null)
// Create anchor node at the tapped location
val anchor = hitResult.createAnchor()
val anchorNode = AnchorNode(anchor)
anchorNode.setParent(arFragment.arSceneView.scene)
// Load renderable and attach to anchor
ModelRenderable.builder()
.setSource(this, Uri.parse("block.sfb"))
.build()
.thenAccept { renderable ->
val node = TransformableNode(arFragment.transformationSystem)
node.renderable = renderable
node.setParent(anchorNode)
arFragment.arSceneView.scene.addChild(anchorNode)
node.select()
// Save for future reference
currentAnchorNode = anchorNode
placed = true
}
}
}
private fun setupGhostBlock() {
ModelRenderable.builder()
.setSource(this, Uri.parse("block.sfb"))
.build()
.thenAccept { renderable ->
ghostBlock = Node().apply {
setParent(arFragment.arSceneView.scene)
localPosition = Vector3(0f, -0.5f, -1f) // In front of camera
localScale = Vector3(0.8f, 0.8f, 0.8f)
renderable = renderable
}
arFragment.arSceneView.scene.addChild(ghostBlock)
}
}
}
📦 Optional: Generate .sfb File
If you want to make your own .sfb, install the Sceneform Tools plugin in Android Studio.
Then right-click on your .obj or .gltf file → Sceneform → Generate Renderable (.sfb)
Or just use a basic cube model like this:
- Download:
block.obj
- Convert to
.sfb using Sceneform plugin
✅ Step 6: Run the App
- Connect your ARCore-compatible Android device
- Click Run in Android Studio
- Point the camera around until ARCore detects planes
- You’ll see a ghost block in the center
- Tap the screen to place the block
🧪 Troubleshooting Tips
- If you get
ARCore not supported, check supported devices list
- Make sure you're using a real device — ARCore doesn't work on emulator
- Ensure lighting is sufficient for plane detection
🚀 Bonus Features You Can Add
- Drag and drop placement
- Multiple blocks
- Undo last placement
- Change block type
- Save anchor positions between sessions
🧠 Summary
You've now built an AR app using Kotlin, ARCore, and Sceneform UX that shows a ghost block in the middle of the screen and allows the user to place it once they're ready.
This is a great foundation for more advanced AR applications like interior design apps, games, or educational tools.
🔗 References
Let me know if you'd like a version using Java or want to upgrade to Filament or Unity!