I am trying to resolve an issue that is stopping my development.
I want to position an imageview so it is in the center of a frame layout using code.
I have been trying various solutions but do not seem to be able to achieve my goal.
Code
package com.example.testmovingobject
import android.content.Context
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var star: ImageView
lateinit var directionText: TextView
lateinit var background: ImageView
lateinit var outMetrics : DisplayMetrics
lateinit var context: Context
private val testDirections = arrayOf(
"North",
"South",
"East",
"West",
"North",
"South",
"East",
"West"
)
private var topBorderHeight = 50f
private var startX : Float = 0f
private var startY : Float = 0f
private var height = 13f
private var width = 7f
private var left = 0f
private var top = 0f
private var screenHeight = 0f
private var screenWidth = 0f
private var backgroundPos = IntArray(2)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
context = getApplicationContext()
setContentView(R.layout.main)
Start()
}
private fun Start()
{
outMetrics = context.resources.displayMetrics
screenHeight = outMetrics.heightPixels.toFloat()
println("Start.metrics height="+screenHeight)
screenWidth = outMetrics.widthPixels.toFloat()
println("Start.metrics width="+screenWidth)
background = findViewById(R.id.background)
println("Start.background size="+background.getWidth()+" "+background.getHeight())
star = findViewById(R.id.star)
star.setBackgroundResource(R.drawable.star)
println("Start.star parent="+star.parent)
println("Start.star pos b4 x="+star.x+" star pos y="+star.y )
background.getLocationOnScreen(backgroundPos)
println("Start.background pos x="+backgroundPos[0]+" pos y="+backgroundPos[1])
directionText = findViewById(R.id.messages)
height = background.getHeight().toFloat()
println("Start.background height="+height)
width = background.getWidth().toFloat()
println("Start.background width="+width)
left = backgroundPos[0].toFloat()
top = backgroundPos[1].toFloat()
println("Start.background bounds left="+left+" top="+top+" width="+width+" height="+height)
println("Start.star pos b4 x="+star.x+" star pos y="+star.y )
startX = width / 2
startY = height / 2
star.x = startX
star.y = startY
println("Start.star pos aftr x="+star.x+" star pos y="+star.y )
println("Start.bounds left="+left+" top="+top+" width="+width+" height="+height)
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_overlay"
android:theme="@style/Base.Theme.TestMovingObject"
tools:context=".MainActivity">
<ImageView
android:id="@+id/TopBackground"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/light_blue_600"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="@+id/back_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/grey_overlay"
android:contentDescription="back"
android:src="@drawable/ic_back"
app:layout_constraintEnd_toEndOf="@+id/TopBackground"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/TopBackground"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/messages"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="40dp"
android:layout_marginBottom="265dp"
android:elevation="30dp"
android:gravity="center"
android:keepScreenOn="true"
android:text="Messages"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="background" />
<ImageView
android:id="@+id/star"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_marginTop="372dp"
android:background="@drawable/star"
android:contentDescription="star"
android:elevation="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/messages">
</ImageView>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot when run
This image show the view of some of the xml and the device emulator and it can been seen that the star object is not postioned in the center.
Logcat output from printlns
-------------- PROCESS STARTED (5791) for package com.example.testmovingobject ----------------------------
2026-02-19 06:56:45.282 Start.metrics height=2028.0
2026-02-19 06:56:45.283 Start.metrics width=1080.0
2026-02-19 06:56:45.283 Start.background size=0 0
2026-02-19 06:56:45.283 Start.star parent=android.widget.FrameLayout{7f6878b V.E...... ......I. 0,0-0,0 #7f0800c8 app:id/frame}
2026-02-19 06:56:45.283 Start.star pos b4 x=0.0 star pos y=0.0
2026-02-19 06:56:45.283 Start.background pos x=0 pos y=0
2026-02-19 06:56:45.283 Start.background height=0.0
2026-02-19 06:56:45.283 Start.background width=0.0
2026-02-19 06:56:45.283 Start.background bounds left=0.0 top=0.0 width=0.0 height=0.0
2026-02-19 06:56:45.283 Start.star pos b4 x=0.0 star pos y=0.0
2026-02-19 06:56:45.283 Start.star pos aftr x=0.0 star pos y=0.0
2026-02-19 06:56:45.283 Start.bounds left=0.0 top=0.0 width=0.0 height=0.0
---------------- PROCESS ENDED (5791) for package com.example.testmovingobject ----------------------------