Add preference fragment and monitor for clicks

- Posted in User Interface by

Add a preference fragment where we inflate our settings screen, then do something if the user clicks on an item inside that screen.

1 
2 
3 
4
5 
6
7
8 
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MySettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

        setPreferencesFromResource(R.xml.settings, rootKey)

        val sharedPref = activity?.getPreferences(AppCompatActivity.MODE_PRIVATE) ?: return
        val vNormalMap: Preference? = findPreference(getString(R.string.pNormal_map))

        vNormalMap?.onPreferenceClickListener = Preference.OnPreferenceClickListener {

            it.setIcon(R.drawable.icon_radio_grey)

            with (sharedPref.edit()) {
                putString(getString(R.string.pNormal_map), "newHighScore")
                apply()
            }

            val defaultValue = "resources"
            val highScore = sharedPref.getString(getString(R.string.pNormal_map), defaultValue)
            println("The current value is $highScore")

            true
        }
        
    }
}


Where:

  • 4️⃣ - Create the preference screen from an XML resource
  • 6️⃣ - Declare a preference file where we can save the user's choices
  • 7️⃣ - Specify an item in our XML resource as a preference object
  • 9️⃣ - Listen for clicks on this item

If user clicks, do the following:

  • 1️⃣1️⃣ - Change the icon of the resource
  • 1️⃣3️⃣ ▸ 1️⃣6️⃣ - Set the value of the "pNormalMap" key as "newHighScore"
  • 1️⃣8️⃣ - Set the default value as "resources"
  • 1️⃣9️⃣ - Get the current value of "pNormalMap". If no value set, use the default value
  • 2️⃣0️⃣ - Print the current value to logcat

Click button to open URL

- Posted in Functions by

Click a button to open URL.

1
2
3
4
5
6
7
8
hBottom.kStreetViewLink.setOnClickListener { 

    val hStreetURL = "YOUR-URL-HERE"
    val openURL = Intent(Intent.ACTION_VIEW) 
    openURL.data = Uri.parse(hStreetURL) 
    startActivity(openURL) 

  }

Where:

1️⃣   Specify the element to monitor, and use the .setOnClickListener event to check if the user clicks the element specified.

3️⃣   Provide the target URL here.


Expected outcome:

  1. Launch the user's default browser.
  2. Go to the target URL using the default browser.

Place a Circle shape and show toast if clicked

- Posted in Markers by

If user clicks a circle, show a toast message.

1️⃣ Declare variables for a circle. Ensure that the clickable property is set to true.

1
2
3
    val circles = CircleOptions()
        .radius(10.0) 
        .clickable(true)   


2️⃣ Place the circle on the map, and add a tag to identify each placement.

1
 mMap.addCircle(circles.center(LatLng(25.16828, 121.44430))).tag = "haha"


3️⃣ Initialize setOnCircleClickListener to listen for user clicks, then show a toast message if the clicked circle matches the tag.

      mMap.setOnCircleClickListener {

          if (it.tag == "haha") {

              Toast.makeText(
                  applicationContext,
                  "haha message haha",
                  Toast.LENGTH_SHORT
              ).show()
          }
          else {
              Toast.makeText(
                  applicationContext,
                  "This is a test message",
                  Toast.LENGTH_SHORT
              ).show()
          }
      }

Place Ground Overlay and show toast if clicked

- Posted in Markers by

If user clicks a ground overlay, show a toast message.

1️⃣ Declare variables for a ground overlay to be placed on the map. Ensure that the clickable property is set to true.

    val mGOLatLng = LatLng(25.12299, 121.46196)
    val mGOMapOpt = GroundOverlayOptions()
        .image(BitmapDescriptorFactory.fromResource(R.drawable.SampleImage))
        .position(mGOLatLng, 100f)
        .clickable(true)


2️⃣ Place the ground overlay on the map, and add a tag to identify each placement.

    mMap.addGroundOverlay(mGOMapOpt)?.tag = "haha"


3️⃣ Initialize setOnGroundOverlayClickListener to listen for user clicks, then show a toast message if the clicked overlay matches the tag.

      mMap.setOnGroundOverlayClickListener {

       if (it.tag == "haha") {

           Toast.makeText(
               applicationContext,
               "This is a haha message",
               Toast.LENGTH_SHORT
           ).show()
       }
    }