Created
June 22, 2020 09:32
-
-
Save DHosseiny/9b87fd7e124ee77aceca33a840557ad4 to your computer and use it in GitHub Desktop.
Adding a view to UI from Background Thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.os.Bundle | |
import android.os.Handler | |
import android.os.HandlerThread | |
import android.os.Message | |
import android.view.WindowManager | |
import android.widget.ImageView | |
import androidx.appcompat.app.AppCompatActivity | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val backgroundThread = HandlerThread("backgroundThread name") | |
backgroundThread.start() | |
val workerThreadHandler = Handler(backgroundThread.looper) { | |
val imageView = ImageView(this) | |
imageView.setImageResource(R.mipmap.ic_launcher)//Any image | |
val windowManagerLayoutParams = WindowManager.LayoutParams() | |
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager | |
windowManager.addView(imageView, windowManagerLayoutParams) | |
return@Handler true | |
} | |
workerThreadHandler.sendMessage(Message.obtain()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment