Bitmap decodefile() by filepath displays empty - android

I'm new to Android development using Kotlin. I'm trying to practice uploading image from a mobile phone to my app using some methods as codes in the code below. everything works just fine with some processes such as I can select on the imageview then screen popup that can let me select image in my phone. unfortunately, after selecting the image, It shows empty/blank, shows nothing in the app. I've tried to search for help but It didn't work out. Hopefully, I can get a hand here. thanks in advance!
Activity code
package student
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.example.amazontutoringcenter.databinding.ActivityMainBinding
import com.example.amazontutoringcenter.databinding.ActivityStudentClassroomBinding
import com.squareup.picasso.Picasso
class StudentClassroomActivity: AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.imageView.setOnClickListener {
checkPermission()
}
}
val READ_IMAGE:Int=338
private fun checkPermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE)!=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf( android.Manifest.permission.READ_EXTERNAL_STORAGE),READ_IMAGE)
return
}
}
loadImage()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode) {
READ_IMAGE->{
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
loadImage()
}else{
Toast.makeText(applicationContext,"Cannot access your images",Toast.LENGTH_LONG).show()
}
}
else-> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
val PICK_IMAGE_CODE=123
private fun loadImage(){
var intent=Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,PICK_IMAGE_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode==PICK_IMAGE_CODE && data!=null && resultCode == RESULT_OK){
val selectedImage=data.data
val filePathColum= arrayOf(MediaStore.Images.Media.DATA)
val cursor= contentResolver.query(selectedImage!!,filePathColum,null,null,null)
cursor!!.moveToFirst()
val columnIndex=cursor!!.getColumnIndex(filePathColum[0])
val picturePath=cursor!!.getString(columnIndex)
cursor!!.close()
//.load(picturePath).into(binding.imageStudentProfile)
//Picasso.get().load(picturePath).fit().into(binding.imageStudentProfile)
binding.imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath))
}
}
}
XML File
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Layout"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:src="#drawable/ic_attendant"/>
</androidx.constraintlayout.widget.ConstraintLayout>

ImageView has a member to load an image from an uri.
And you have an uri data.data.
So use it.

Related

How is defined MediaRecorder( )?

i make program for record audio for android but i find MediaRecorder() Deprecated
kotlin code
package noteapp.notesnotesnotescairo.mynoteapp
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.icu.text.SimpleDateFormat
import android.media.MediaRecorder
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.os.Looper.prepare
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.NonCancellable.start
import java.io.IOException
import java.util.*
const val REQUEST_CODE=200
private var permission = arrayOf(Manifest.permission.RECORD_AUDIO)
private var permissionGranted=false
private lateinit var recorder : MediaRecorder
private var dirPath=""
private var filename=""
private var isRecording=false
private var isPaused=false
class MainActivity : AppCompatActivity(){
#RequiresApi(Build.VERSION_CODES.S)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
permissionGranted=ActivityCompat.checkSelfPermission(this, permission[0])==PackageManager.PERMISSION_GRANTED
if(!permissionGranted)
ActivityCompat.requestPermissions(this, permission, REQUEST_CODE)
btnRecord.setOnClickListener{
when{
isPaused->resumeRecorder()
isRecording->pauseRecorder()
else->startRecording()
}
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode== REQUEST_CODE)
permissionGranted=grantResults[0]==PackageManager.PERMISSION_GRANTED
}
private fun pauseRecorder(){
recorder.pause()
isPaused=true
btnRecord.setImageResource(R.drawable.ic_record)
}
private fun resumeRecorder(){
recorder.resume()
isPaused=false
btnRecord.setImageResource(R.drawable.ic_pause)
}
#RequiresApi(Build.VERSION_CODES.S)
private fun startRecording(){
if(!permissionGranted){
ActivityCompat.requestPermissions(this, permission, REQUEST_CODE)
return
}
recorder = MediaRecorder( this)
dirPath="${externalCacheDir?.absolutePath}/"
var simpleDateFormat= SimpleDateFormat("yyyy.mm.dd.hh.mm.ss")
var date :String=simpleDateFormat.format(Date())
filename="audio_record_$date"
recorder.apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setOutputFile("$dirPath$filename.mp3")
try{
prepare()
}catch (e:IOException){}
start()
}
btnRecord.setImageResource(R.drawable.ic_pause)
isRecording=true
isPaused=false
}
}
xml code
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tvTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00:00"
android:textSize="56sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="parent"
>
<ImageButton
android:id="#+id/btnDelete"
android:layout_width="#dimen/btn_size"
android:src="#drawable/ic_delete_disable"
android:layout_height="#dimen/btn_size"
android:background="#drawable/ic_ripple"/>
<ImageButton
android:id="#+id/btnRecord"
android:layout_width="66dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_height="66dp"
android:background="#drawable/ic_record"/>
<ImageButton
android:id="#+id/btnList"
android:layout_width="#dimen/btn_size"
android:src="#drawable/ic_list"
android:layout_height="#dimen/btn_size"
android:background="#drawable/ic_ripple"/>
<ImageButton
android:id="#+id/btnDone"
android:layout_width="#dimen/btn_size"
android:src="#drawable/ic_done"
android:visibility="gone"
android:layout_height="#dimen/btn_size"
android:background="#drawable/ic_ripple"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
what is new defind for MediaRecorder() not Deprecated ?
give me error
java.lang.NoSuchMethodError: No direct method (Landroid/content/Context;)V in class Landroid/media/MediaRecorder; or its super classes (declaration of 'android.media.MediaRecorder' appears in /system/framework/framework.jar!classes2.dex)
Use an if/else to set the property using the supported constructor:
recorder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) MediaRecorder(this) else MediaRecorder()
Down the road it is possible Jetpack will add a helper function that would do this for you, something like MediaRecorderCompat.newInstance(this). However, it has been so long I think we can assume they will probably not be adding it.
I used the following script in an app published on the Playstore
fun prepare_audio_and_directories(v : View) {
var tempDir : File
// current_filename = (v.findViewById(R.id.audio_filename_xml) as TextView).text.toString()
current_dir = main?.dir!!
tempDir = current_dir
main?.filenameCompletePath = tempDir?.toPath().toString() + "/" +current_filename + "."+ main?.ext
main?.filename_text = current_filename
Log.d("$TAG:prepare_audio_and_directories:main?.filenameCompletePath=", main?.filenameCompletePath!!
)
Log.d("$TAG:prepare_audio_and_directories:main?.filename_text=", main?.filename_text!!)
Log.d("NoiseSuppressor.isAvailable()=", NoiseSuppressor.isAvailable().toString() )
mediaRecorder = MediaRecorder()
mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
mediaRecorder?.setAudioEncodingBitRate(128000)
mediaRecorder?.setAudioSamplingRate(96000);
mediaRecorder?.setOutputFile(main?.filenameCompletePath)
}
The versions I tested it with are API 28,29,30 and 31.

how to exclude service numbers from an android contacts search

I have a Galexy S10+ cell phone. In the contacts app, there are preloaded AT&T service numbers. On the internet, I was able to find instructions on how to disable these numbers from loading, but the instructions are for an old cell phone model. So first anyone know how to keep these from loading?
Here is the old http instructions:
https://www.google.com/search?q=samsung+cell+phone+delete+att+contacts+2020&rlz=1C1CHBF_enUS977US977&oq=&aqs=chrome.0.69i59i450l8.27328431j0j15&sourceid=chrome&ie=UTF-8#kpvalbx=_Dbo4Y9nqDf-j5NoPuZGegA8_21
Alternatively, I would like to do this programmatically, but excluding a string such as "AT&T" from the search in my app. So based upon my code, see below, either on the search line on the cell phone or embedded in the code, I need to alter what is contained in the search string "%$p0%" in the code. Any suggestions. Thanks.
...
package com.example.myapplication
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.ContactsContract
import android.widget.ListView
import androidx.appcompat.widget.SearchView
import android.widget.SimpleCursorAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import java.io.IOException
class ConnectActivity : AppCompatActivity() {
private lateinit var searchView: SearchView
private lateinit var listView: ListView
var cols = listOf<String> (
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID
).toTypedArray()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_connect)
try {
//Toast.makeText(getApplicationContext(), "got here", Toast.LENGTH_SHORT).show();
searchView = findViewById(R.id.searchView)
listView = findViewById(R.id.listView)
if (ActivityCompat.checkSelfPermission( this#ConnectActivity, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this#ConnectActivity, Array(1){Manifest.permission.READ_CONTACTS}, 111)
}
else {
readContact()
}
} catch (e: Exception) {
Toast.makeText(getApplicationContext(), e.message, Toast.LENGTH_LONG).show()
}
}
override fun onRequestPermissionsResult(requestCode: Int,permissions: Array<out String>,grantResults: IntArray){
super.onRequestPermissionsResult(requestCode,permissions,grantResults)
Toast.makeText(getApplicationContext(), "then here", Toast.LENGTH_SHORT).show()
if (requestCode==111 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
readContact()
}
private fun readContact() {
var from = listOf<String>(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER).toTypedArray()
var to = intArrayOf(android.R.id.text1,android.R.id.text2)
var rs = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
cols,null,null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
var adapter = SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,
rs,from,to,0)
listView.adapter =adapter
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(p0: String?):Boolean {
return false
}
override fun onQueryTextChange(p0:String?):Boolean{
rs = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
cols,"${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} LIKE ?",
Array(1){"%$p0%"},
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
adapter.changeCursor(rs)
return false
}
})
}
...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:queryHint="hi"
/>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_below="#+id/searchView"
/>
</RelativeLayout>
...

How to display a text view on CameraX preview view?

I want to display a text view in the preview view of camera. It will appear like a label on the camera preview. It should not affect the image. Just appear like a label on preview view. I tried using frame layout but the text view disappears on the preview view
Here's my layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<Button
android:id="#+id/camera_capture_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/ic_baseline_camera_24"
android:elevation="2dp"
android:layout_marginBottom="20dp"
android:scaleType="fitCenter"
android:layout_gravity="bottom|center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.926" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello"
android:textSize="25dp" />
<androidx.camera.view.PreviewView
android:id="#+id/viewFinder"
android:layout_width="379dp"
android:layout_height="576dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.camera.view.PreviewView>
</FrameLayout>
Main Activity
Here's the main logic->
package com.arpit.cameraxdemo
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
private var imageCapture: ImageCapture? = null
private lateinit var cameraExecutor: ExecutorService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
camera_capture_button.setOnClickListener {
takePhoto()
}
cameraExecutor = Executors.newSingleThreadExecutor()
}
private fun takePhoto() {
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(viewFinder.surfaceProvider)
}
imageCapture = ImageCapture.Builder().build()
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture
)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}
private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}
// checks the camera permission
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults:
IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
startCamera()
} else {
Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
companion object {
private const val TAG = "CameraXGFG"
private const val REQUEST_CODE_PERMISSIONS = 20
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
}
}
Try below code with RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<androidx.camera.view.PreviewView
android:id="#+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="CameraX" />
<Button
android:id="#+id/imageCaptureBtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:gravity="center"
android:text="Click Image" />
</RelativeLayout>
Thanks for your help everyone. I have found the answer to my question. Actually what I was looking for is called overlay
viewFinder.overlay.add(tvText)
use the element "elevation" with 2dp

How to show a window overlay over the accessibility menu in android 10?

I have done some code which is working fine everywhere except settings app -> accessibility menu or device admin apps menu
I need to display overlay in settings -> accessibility and device admin apps menu
I have a application where i see this type of overlay you can see below image
First image from settings app
Second image from settings app
You can see above second image when i open -> device admin app -> then my app overlay got hidden but another app overlay is still visible I don't know why?
Please help me guys to fix this issue??
**AndridManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxxx">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<application
.....
.....
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".OverlayService" />
</application>
</manifest>
import android.content.Intent
import android.graphics.PixelFormat
import android.os.Build
import android.os.IBinder
import android.view.*
class OverlayService : Service() {
private var windowManager: WindowManager? = null
private var view: View? = null
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
val layoutParamsType: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
val params = WindowManager.LayoutParams().apply {
width = WindowManager.LayoutParams.MATCH_PARENT
height = WindowManager.LayoutParams.WRAP_CONTENT
type = layoutParamsType
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
format = PixelFormat.TRANSLUCENT
}
params.gravity = Gravity.CENTER or Gravity.START
val inflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = inflater.inflate(R.layout.overlay_view, null)
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
windowManager!!.addView(view, params)
}
override fun onDestroy() {
super.onDestroy()
if (view != null) {
windowManager!!.removeView(view)
view = null
}
}
}
MainActivity.kt
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
if (Settings.canDrawOverlays(this)) {
startService(Intent(this, OverlayService::class.java))
} else {
startManageDrawOverlaysPermission()
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQUEST_CODE_DRAW_OVERLAY_PERMISSION -> {
if (Settings.canDrawOverlays(this)) {
startService(Intent(this, OverlayService::class.java))
} else {
Toast.makeText(this, "Permission is not granted!", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun startManageDrawOverlaysPermission() {
Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, "package:${packageName}".toUri()).apply {
startActivityForResult(this, REQUEST_CODE_DRAW_OVERLAY_PERMISSION)
}
}
companion object {
private const val REQUEST_CODE_DRAW_OVERLAY_PERMISSION = 5
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show !"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
overlay_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#00897B"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overlay Example View"
android:textColor="#color/white" />
</LinearLayout>

change youtube video on click of listitem in recyclerview

I am using YoutubeplayerApi in my android app
what I have done is that there is a youtubeplayerView and a recycler view below the player
I want to implement the functionality that once a user click on the item view of recycler view the youtubeplayer should play that particular video but I am not able to implement that
any help would be appreciated
activity_music.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UI.MusicActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:layout_width="450dp"
android:layout_height="384dp"
android:id="#+id/youtube_player"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="24dp"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/songs_recycler_view"
app:layout_constraintTop_toBottomOf="#+id/youtube_player"/>
</android.support.constraint.ConstraintLayout>
MusicActivity.kt
package com.example.ashish.batmn.UI
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Toast
import com.example.ashish.batmn.Adapters.SongsAdapter
import com.example.ashish.batmn.Config
import com.example.ashish.batmn.Models.Songs
import com.example.ashish.batmn.R
import com.google.android.youtube.player.YouTubeBaseActivity
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubeStandalonePlayer
import kotlinx.android.synthetic.main.activity_music.*
class MusicActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListener {
val songsList = listOf<Songs>(
Songs("Girls Like You","cBVGlBWQzuc","${Config.IMAGE_BASE_URL}cBVGlBWQzuc${Config.IMAGE_PIC_END}"),
Songs("The National - Fake Empire","KehwyWmXr3U","${Config.IMAGE_BASE_URL}KehwyWmXr3U${Config.IMAGE_PIC_END}"),
Songs("Halka Halka","nZhLM-FeV9A","${Config.IMAGE_BASE_URL}nZhLM-FeV9A${Config.IMAGE_PIC_END}"),
Songs("Camila Cabello - Real Friends","66rxB7_zzs8","${Config.IMAGE_BASE_URL}66rxB7_zzs8${Config.IMAGE_PIC_END}"),
Songs("Darya","wHHCO29mqiA","${Config.IMAGE_BASE_URL}wHHCO29mqiA${Config.IMAGE_PIC_END}"),
Songs("Naina Da Kya Kasoor","BJWTzYPWINw","${Config.IMAGE_BASE_URL}BJWTzYPWINw${Config.IMAGE_PIC_END}")
)
override fun onInitializationSuccess(provider: YouTubePlayer.Provider?, player: YouTubePlayer?, wasRestored: Boolean) {
if (!wasRestored){
player!!.cueVideo(Config.VIDEO_CODE)
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == 1){
getYoutubePlayerProvider().initialize(Config.VIDEO_CODE,this)
}
}
override fun onInitializationFailure(p0: YouTubePlayer.Provider?, errorResult: YouTubeInitializationResult?) {
if (errorResult!!.isUserRecoverableError){
errorResult.getErrorDialog(this,1).show()
}
else{
Toast.makeText(this,"Some unforseen error has occured",Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_music)
youtube_player.initialize(Config.API_KEY,this)
val songAdapter = SongsAdapter(songsList,this){
// what should be added in here?
}
songs_recycler_view.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
songs_recycler_view.adapter = songAdapter
}
fun getYoutubePlayerProvider():YouTubePlayer.Provider{
return youtube_player
}
}
Songs.kt // Model class
package com.example.ashish.batmn.Models
class Songs(val mSongName:String, val mVideoCode:String,val mSongPic:String) {
}
First of all in onInitializationSuccess set player instance to a global field. Then Add a onClickListener to listview. After that in onClick method call player.setCue() method with the video id that you want to play. And at the last play the video with player.play().

Categories

Resources