Getting the coordinates of the camera on idle - kotlin

I am trying to get the center coordinates of the camera once the camera is on idle state after movement but its not working or logging the coordinates. Here is the code
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_shifting_map)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map_fragment) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.setOnCameraIdleListener { OnCameraIdleListener {
val centerLatLang = mMap.projection.visibleRegion.latLngBounds.center
Log.d("##", "lat{$centerLatLang.longitude} long{$centerLatLang.longitude}")
} }
}

setOnCameraIdleListener is a function and should use parentheses rather than brackets.
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap#public-final-void-setoncameraidlelistener-googlemap.oncameraidlelistener-listener
OnCameraIdleListener should require the same fix.

Related

How to pass Firebase Realtime database images from reyclerview to image view in another activity. KOTLIN

I have Firebase Realtime Database images displayed in a reyclerview. When users click that image, I want it to send the image to another activity's IMAGEVIEW and open that activity at the same time.
These are pictures of an example I saw on Youtube
As you can see in the second picture. It sent the data from the recyclerview to the imageview in the activity and opened that activity.
My adapter class
class AbstractAdapter(private val mContext: Context, private val abstractList: ArrayList<Abstract>) :
RecyclerView.Adapter<AbstractAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.abstract_image_view, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.imageView.setOnClickListener {
val intent = Intent(mContext, PreviewActivity::class.java)
intent.putExtra("abstract", abstractList[position].abstract.toString())
mContext.startActivity(intent)
}
holder.download_btn.setOnClickListener { }
Glide.with(mContext)
.load(abstractList[position].abstract)
.into(holder.imageView)
}
override fun getItemCount(): Int {
return abstractList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.abstractImageView)
val download_btn: Button = itemView.findViewById(R.id.abstractDownloadBtn)
}
companion object {
private const val Tag = "RecyclerView"
}
Activity to receive image
class PreviewActivity : AppCompatActivity() {
private lateinit var previewImage: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_preview)
previewImage = findViewById(R.id.preview_image)
val previewImage: ImageView = findViewById(R.id.preview_image)
val bundle :Bundle? = intent.extras
val preview = bundle!!.getString("abstract")
}
Data model
class Abstract(var abstract: String? = null) {
}
I've tried running this code, but it doesn't show my image in the next activity. The Logcat doesn't give me any error, because according to it my code is running perfectly except that it's not showing the image in the next activity. I must be missing something, but I don't know what it is.
You have to set the image url to ImageView using Glide in second activity like this:
class PreviewActivity : AppCompatActivity() {
private lateinit var previewImage: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_preview)
previewImage = findViewById(R.id.preview_image)
val previewImage: ImageView = findViewById(R.id.preview_image)
val bundle :Bundle? = intent.extras
val preview = bundle!!.getString("abstract")
Glide.with(this)
.load(preview)
.into(previewImage)
}

Can i use Jsoup in Jetpack Compose in kotlin

In traditional xml way , i use GlobalScope.launch{} with runOnUiThread {} to work with Jsoup.But in jetpack Compose this not work anymore. It just instant closed when run it.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GlobalScope.launch{
val url="somewebsite.com"
var doc= Jsoup.connect(url).get()
runOnUiThread {
}
}
setContent {
WannaJsoupTheme {
Surface(color = MaterialTheme.colors.background) {
Greeting("Android")
}
}
}
}
}
Explanation
Based on the above code, apparently you're using the main thread to perform I/O operations. Main thread are generally used for user interface operations. It's better to use background threads for I/O operations. - They are more efficient.
Here's demo app which I created to demonstrate use of JSoup with MVVM architecture.
Link - https://github.com/TheCodeMonks/NYTimes-App
Solution - https://gist.github.com/Spikeysanju/111e061ad82f3b7be6beb419fb18bca4
Quick Solution
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Initiate Coroutine scope
val scope = rememberCoroutineScope()
var text by remember {
mutableStateOf("")
}
JsoupTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
// Initiate coroutine scope will will run on IO thread & write method to get the text from the website
scope.launch(Dispatchers.IO) {
val url = "https://en.wikipedia.org/wiki/Steve_Jobs"
Jsoup.connect(url).get().also {
text = it.text()
}
}
// Fetch result from the state variable [Text]
Text(text = "Result: $text")
}
}
}
}
}

onMapReady function seems to not be called on android app

The map seems to be created but the marker does not get placed. Most questions relating to this problem seems to be missing getmapasync but th,s one seems to have it
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Retrieve the content view that renders the map.
setContentView(R.layout.fragment_dashboard)
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val sydney = LatLng(-33.852, 151.211)
mMap.addMarker(
MarkerOptions()
.position(sydney)
.title("Marker in Sydney")
)
// [START_EXCLUDE silent]
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
// [END_EXCLUDE]
}
// [END maps_marker_on_map_ready_add_marker]
Is your map fragment nested in a fragment?
If you are calling getMapAsync from a fragment that contains the actual map, you need to use childFragmentManager instead of supportFragmentManager to find the map
(childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync(this)

How can I do the kotlin mediaPlayer shuffle play

I am trying to create shuffle mode for the music-player application. The problem is; The sound on my list only plays once and stops when finished. but I want all the sounds on my list to be shuffled and played automatically. I am very new to Android programming, I tried hard but failed. I also tried the setOnCompletionListener {} method but it didn't work. I need help. thanks everyone
here are my sample codes;
class MainActivity : AppCompatActivity() {
var mediaPlayer = MediaPlayer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val Sound1 = R.raw.sound01
val Sound2 = R.raw.sound02
val Sound3 = R.raw.sound03
val Sound4 = R.raw.sound04
val Sound5 = R.raw.sound05
val soundList = ArrayList<Int>()
soundList.add(Sound1)
soundList.add(Sound2)
soundList.add(Sound3)
soundList.add(Sound4)
soundList.add(Sound5)
shuffleBtn.setOnClickListener {
val randomList = Random.nextInt(soundList.size)
val sound = soundList.get(randomList)
mediaPlayer = MediaPlayer.create(this, sound)
mediaPlayer.start()
}
}
}
Add a completion listener that releases the original player and creates a new one.
If you want to play shuffled, you need a variable to store the shuffled list and remove tracks as you play them so you know when to reshuffle. The way you're picking random sounds, you could play the same sound twice in a row sometimes.
By the way, you can create your list more concisely.
I didn't test this code. Sorry for any errors.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sounds = listOf(
R.raw.sound01,
R.raw.sound02,
R.raw.sound03,
R.raw.sound04,
R.raw.sound05
)
var shuffledSounds = sounds.shuffled()
fun newTrack() {
if (shuffledSounds.isEmpty()) {
shuffledSounds = sounds.shuffled()
}
val nextSound = shuffledSounds.first()
shuffledSounds = shuffledSounds - nextSound
mediaPlayer = MediaPlayer.create(this, nextSound).apply {
setOnCompletionListener{
it.release()
newTrack()
}
start()
}
}
shuffleBtn.setOnClickListener {
newTrack()
}
}

How I get data from fun onResult to fun onMapReady in kotlin

I use retrofit for get data From API. i will displays data ini the maps. data was successfull get from retrofit, but i found problem get data response to fun onMapReady. i don't know to be it
class HomeMapsActivity : AppCompatActivity(), OnMapReadyCallback,
MapHomeContract.View {
private lateinit var mMap: GoogleMap
lateinit var presenter: MapHomePresenter
lateinit var prefsManager: PrefsManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_maps)
supportActionBar!!.title = "Posisi driver"
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
prefsManager = PrefsManager(this)
presenter = MapHomePresenter(this)
presenter.getAnak(prefsManager.prefsIdUser)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
for (dataAnak in anak) {
// Add a marker in Sydney and move the camera
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
}
override fun onResult(dataAnakResponse: AnakResponse) {
dataAnakResponse.anak
}
}
please help
onMapReady() is called when you initially start the Map Activity. You can set the map data in onResult.
override fun onResult(dataAnakResponse: AnakResponse) {
val anak = dataAnakResponse.anak
for (dataAnak in anak) {
// Add a marker in Sydney and move the camera
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
}