Buttons don't work in new activity crated from service foreground window - kotlin

I have a foreground Service with a button without any activities (initial was closed). When I click button the new activity window is created. The problem is that all buttons in new activity don't work
In foreground service I do:
override fun onClick(p0: View?) {
if (!moving) {
startActivity(Intent(applicationContext, Mmap_activity::class.java)
.setAction(Intent.ACTION_VIEW)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT))
}
}
In Manifest new activity declared:
<activity
android:name=".Mmap_activity"
android:exported="true"
android:theme="#style/ActionBarTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
activity_mmap.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=".Mmap_activity">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/leftBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent=".80"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/bottomBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".10" />
<ImageButton
android:id="#+id/mapexitButton"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFFFF"
android:contentDescription="#string/Enter"
android:src="#drawable/ic_baseline_close_24"
app:layout_constraintBottom_toBottomOf="#id/bottomBorder"
app:layout_constraintLeft_toLeftOf="#id/leftBorder"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mmap_activity.kt:
class Mmap_activity : AppCompatActivity() {
private lateinit var binding: ActivityMmapBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mmap)
binding = ActivityMmapBinding.inflate(layoutInflater)
binding.mapexitButton.setOnClickListener{
println("button pressed")
}
}
}
How to repair button in new activity? Why it doesn't work?

The error was here.
binding = ActivityMmapBinding.inflate(layoutInflater)
setContentView(binding.root)

Related

How to hide the FloatingActionButton that is part of the Activity in the Fragment in Kotlin?

In my 'HomeActivity', I have a FloatingActionButton and four Fragments. I want the FloatingActionButton to be shown only in the 'fragment1' and 'fragment2' make hide it in the 'fragment3' and 'fragment4'. I tried but it didn't work.
Following is what I have in the xml file of the 'HomeActivity'
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_hide_category"
app:layout_anchor="#id/bottom_appbar"/>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation"
app:layout_anchor="#id/bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I added the below code in the 'HomeActivity'
fun hideFabOne(){
binding.fabOne.hide()
}
and below code in the onCreateView of the fragments where I want to hide the FAB. But, it didn't work.
HomeActivity().hideFabOne()
Looks like you are creating new instance of activity , try
(requireActivity() as HomeActivity).hideFabOne()
*Add for comment
You can modify the hideFabOne() something like
fun hideFabOne(){
binding.fabOne.visiblity = if( binding.fabOne.isVisible ) View.VISIBLE else View.GONE
}

Using navigation component Load fragment inside a fragment

In Home Fragment I am having xml as
<androidx.constraintlayout.widget.ConstraintLayout
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottomNavi"
app:navGraph="#navigation/bottm_navi" />
<meow.bottomnavigation.MeowBottomNavigation
android:id="#+id/bottomNavi"
app:mbn_circleColor="#color/white"
app:mbn_backgroundBottomColor="#color/app_dark_blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my bottom navigation is
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/bottm_navi"
app:startDestination="#id/homeBottomOneFragment">
<fragment
android:id="#+id/homeBottomOneFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomOneFragment"
android:label="fragment_home_bottom_one"
tools:layout="#layout/fragment_home_bottom_one" />
<fragment
android:id="#+id/homeBottomTwoFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomTwoFragment"
android:label="fragment_home_bottom_two"
tools:layout="#layout/fragment_home_bottom_two" />
<fragment
android:id="#+id/homeBottomFourFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomFourFragment"
android:label="fragment_home_bottom_four"
tools:layout="#layout/fragment_home_bottom_four" />
<fragment
android:id="#+id/homeBottomThreeFragment"
android:name="com.krassier.customer.ui.home.bottom.HomeBottomThreeFragment"
android:label="fragment_home_bottom_three"
tools:layout="#layout/fragment_home_bottom_three" />
<fragment
android:id="#+id/homeFragment2"
android:name="com.krassier.customer.ui.home.HomeFragment"
android:label="HomeFragment" >
<action
android:id="#+id/action_homeFragment2_to_homeBottomOneFragment"
app:destination="#id/homeBottomOneFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomTwoFragment"
app:destination="#id/homeBottomTwoFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomThreeFragment"
app:destination="#id/homeBottomThreeFragment" />
<action
android:id="#+id/action_homeFragment2_to_homeBottomFourFragment"
app:destination="#id/homeBottomFourFragment" />
</fragment>
</navigation>
in homefragment I wrote
findNavController().navigate(R.id.action_homeFragment2_to_homeBottomOneFragment)
when I clicked on bottomnavigation I am trying to load another fragment in homefragment as bottom navigation is in home fragment, but error is:
java.lang.IllegalArgumentException: Navigation action/destination com.krassier.customer:id/action_homeFragment2_to_homeBottomOneFragment cannot be found from the current destination Destination(com.krassier.customer:id/homeFragment) label=fragment_home class=com.krassier.customer.ui.home.HomeFragment
At last I followed conventional way
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = activity?.supportFragmentManager
val fragmentTransaction = fragmentManager?.beginTransaction()
fragmentTransaction?.replace(R.id.homeFragmentContainer, fragment)
fragmentTransaction?.commit()
}
Since you are in a fragment, you should try finding the navController this way.
val navHostFragment = supportFragmentManager.findFragmentById(R.id. fragment) as NavHostFragment. // the id of your FragmentContainerView
navController = navHostFragment.navController
You should take advantage of the NavigationUI library and let it handle the navigation fragment change logic for you
binding.bottomNavi.setupWithNavController(navController)
You can try this:
currentDestination?.getAction(direction.actionId)?.run { navigate(direction) }
}
fun NavController.safeNavigate(
#IdRes currentDestinationId: Int,
#IdRes id: Int,
args: Bundle? = null
) {
if (currentDestinationId == currentDestination?.id) {
navigate(id, args)
}
}
This crash occurs when you make multiple calls the navigate method.
https://nezspencer.medium.com/navigation-components-a-fix-for-navigation-action-cannot-be-found-in-the-current-destination-95b63e16152e
If you want to only just change the fragment like Fragment Transaction using Navigation component, you could do it like this:
private fun getNavController(): NavController {
val navController = findNavController(R.id.fragment)
return navController
}
private fun changeFragment(#IdRes viewId: Int) {
getNavController().popBackStack()
getNavController().navigate(viewId)
}
// how to use it
changeFragment(R.id.homeBottomFourFragment)

Rounded corners for custom AlertDialog

I have been updating my apps forgot password functionality from Activity to simple custom Alert Dialog. How should I add rounded corners to Dialog window? I have already read multiple tutorials, but none seem to work in my case. I read from somewhere that I should use setBackgroundResources method somewhere, but I'm not sure where.
Kotlin code
// Forgot password textview onClick Listener
binding.tvForgotPassword.setOnClickListener {
// Inflate add_item_dialog.xml custom view
val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_forgot_password, null)
// Add AlertDialog Builder
val dialogBuilder = AlertDialog.Builder(this)
.setView(dialogView)
// Binding add_item_dialog layout
val dialogBinding = DialogForgotPasswordBinding.bind(dialogView)
// Show Forgot password Dialog
val customAlertDialog = dialogBuilder.show()
dialogBinding.ivCloseDialog.setOnClickListener {
customAlertDialog.dismiss()
}
}
dialog_forgot_password.xml
<androidx.cardview.widget.CardView
android:layout_width="350dp"
android:layout_height="500dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="350dp"
android:layout_height="500dp">
<ImageView
android:id="#+id/iv_closeDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.060000002"
app:srcCompat="#drawable/ic_close" />
<TextView
android:id="#+id/tv_forgotPasswordInfo"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:textSize="18sp"
android:text="#string/enter_email"
android:textColor="#color/ColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_forgotPassword" />
<Button
android:id="#+id/submitBtn"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:background="#drawable/button"
android:elevation="4dp"
android:text="#string/submit"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailForgot"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/tv_forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:fontFamily="sans-serif-black"
android:text="#string/forgot_password"
android:textColor="#color/ColorPrimary"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_closeDialog" />
<EditText
android:id="#+id/emailForgot"
android:layout_width="300dp"
android:layout_height="40dp"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:hint="#string/email_address"
android:importantForAutofill="no"
android:inputType="textEmailAddress"
android:paddingStart="15dp"
android:textColorHint="#color/ColorPrimary"
app:layout_constraintBottom_toTopOf="#+id/submitBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_forgotPasswordInfo"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Rounded_framelayout.xml
<solid android:color="#FFFFFF"/>
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/>
<corners android:radius="20dp" />
Please take a look at this guide for how to customize dialogs.
In short, you need to create a dialog like normal, but set the background resource before you show it:
val builder = AlertDialog.Builder(context)
builder.setView(R.layout.item_dialog)
val dialog = builder.create()
dialog.window?.decorView?.setBackgroundResource(R.drawable.dialog_background) // setting the background
dialog.show()
where dialog_background is defined like so:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="16dp" />
<solid android:color="?android:colorBackground" />
</shape>
TRY THIS:
class your_kotlin_filename :DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog!!.window?.setBackgroundDrawableResource(R.drawable.Rounded_framelayout);
return inflater.inflate(R.layout.dialog_forgot_password, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
............your code for forget password...........
}
override fun onStart() {
super.onStart()
val width = (resources.displayMetrics.widthPixels * 0.95).toInt()
val height = (resources.displayMetrics.heightPixels * 0.43).toInt()
dialog!!.window?.setLayout(width, height)
dialog!!.setCanceledOnTouchOutside(true)
}

Attempt to invoke virtual method on a null object reference -> resolve?

I try to do a button for switch from a main activity to an other, but when I try to run the app, it close itself immediatly after click on the app. I find in the command that message of error :
Unable to start activity ComponentInfo{fr.amseu.mystretching/fr.amseu.mystretching.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
My code in the MainActivity is this one :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<ImageButton>(R.id.imageButton)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent);}
That is my Main Activity :
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/item_main"
android:background="#color/light_red">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="100dp"
android:layout_height="120dp"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/lower_legs"
android:contentDescription="#string/home_page_first_button" />
</androidx.cardview.widget.CardView>
<View
android:id="#+id/view_separation"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/name_item"
style="#style/SubTitleTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/default_margin"
android:text="#string/home_page_first_button"
app:layout_constraintBottom_toTopOf="#+id/view_separation"
app:layout_constraintStart_toEndOf="#+id/cardView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/description_item"
style="#style/DefaultTextStyle"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/home_page_description_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/name_item"
app:layout_constraintTop_toBottomOf="#+id/view_separation" />
</androidx.constraintlayout.widget.ConstraintLayout>
And for my recycler view, I take this model in my page and the following item are siblings but I change the image and texts, with an adapter, HomeFragment...
Can someone help me please ?
(It is my first app)
Thanks a lot !

Google map not visible in Fragment Kotlin

i build a Kotlin project with google maps, but the in the map fragment cant load the map, is not visible
Here is a pic with the fragment
And this is my code:
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wiboo.luismtz.logisticaapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.DayNight.NoActionBar"
>
</activity>
<activity android:name=".HomeAct" />
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".OrdenAct"
android:label="#string/title_activity_orden"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.DayNight.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
My Activity:
package com.wiboo.luismtz.logisticaapp
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import kotlinx.android.synthetic.main.activity_orden.*
import java.util.jar.Manifest
class OrdenAct : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_orden)
// 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)
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION), 123)
var list = ArrayList<DetalleSurtido>()
var os = intent.getStringExtra("orden_surtido")
tvTitulo.text = "# De orden: " + os
var url:String = Configuracion.url + "obtenerdetallesurtido.php?orden=" + os
var rq = Volley.newRequestQueue(this)
var jar = JsonArrayRequest(Request.Method.GET, url, null,
Response.Listener { response ->
for(x in 0 until response.length()) {
tvFolioCliente.text = response.getJSONObject(x).getString("orden_cliente")
tvProducto.text = response.getJSONObject(x).getString("producto")
tvCantidad.text = response.getJSONObject(x).getInt("cantidad").toString()
tvUnidad.text = response.getJSONObject(x).getString("unidad")
tvDireccion.text = response.getJSONObject(x).getString("direccion")
var esurtido = response.getJSONObject(x).getString("status")
if(esurtido == "1"){
tvStatus.text = "Estado: Por Surtir"
}else if (esurtido == "2"){
tvStatus.text = "Estado: Surtido"
}
var l = response.getJSONObject(x).getString("latitud")
var ln = response.getJSONObject(x).getString("longitud")
Configuracion.latitud = l.toDouble()
Configuracion.longitud = ln.toDouble()
}
},
Response.ErrorListener { })
rq.add(jar)
}
#SuppressLint("MissingPermission")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode == 123){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
var manager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
var listener = object:LocationListener{
override fun onLocationChanged(p0: Location) {
/*
val myloc = LatLng(Configuracion.longitud, Configuracion.latitud)
mMap.addMarker(MarkerOptions().position(myloc).title("El pedido debe ser surtido aqui"))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myloc, 16f))
*/
}
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
}
override fun onProviderEnabled(p0: String?) {
}
override fun onProviderDisabled(p0: String?) {
}
}
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0f, listener)
}
}
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val sydney = LatLng(21.097153, -101.643837)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
}
And my layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="#drawable/fondolistado"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tvTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff" />
<TextView
android:id="#+id/tvStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="281dp"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
tools:context=".OrdenAct" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Folio cliente: "
android:textColor="#color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<TextView
android:id="#+id/tvFolioCliente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/textView"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<TextView
android:id="#+id/textView3"
android:layout_width="63dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Producto:"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/tvProducto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toBottomOf="#+id/tvFolioCliente" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Cantidad:"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/tvCantidad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toBottomOf="#+id/tvProducto" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Unidad:"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<TextView
android:id="#+id/tvUnidad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="#+id/textView7"
app:layout_constraintTop_toBottomOf="#+id/tvCantidad" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Dirección: "
android:textColor="#color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView7" />
<TextView
android:id="#+id/tvDireccion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="#+id/textView9"
app:layout_constraintTop_toBottomOf="#+id/tvUnidad" />
</android.support.constraint.ConstraintLayout>
i also include in my google_maps_api.xml the key generated in the developers console and add my SHA-1.
Hope you guys help me.
Thank you