Skip to content

Instantly share code, notes, and snippets.

@Edijae
Last active February 24, 2022 18:17
Show Gist options
  • Save Edijae/967e4239d2358093e6b56d1a9b8edda4 to your computer and use it in GitHub Desktop.
Save Edijae/967e4239d2358093e6b56d1a9b8edda4 to your computer and use it in GitHub Desktop.
A simple Activity Written In Kotlin that shows google maps. It also uses data binding
<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="@+id/included_layout"
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.edijae.crusar.reporter.activities.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraint"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>
package com.edijae.crusar.reporter.activities
import android.arch.lifecycle.ViewModelProviders
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.view.Menu
import android.view.MenuItem
import com.edijae.crusar.reporter.R
import com.edijae.crusar.reporter.controllers.LocationController
import com.edijae.crusar.reporter.databinding.ActivityHomeBinding
import com.edijae.crusar.reporter.viewmodels.HomeViewModel
import com.edijae.crusar.reporter.viewmodels.MediaViewModel
import com.google.android.gms.maps.SupportMapFragment
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.app_bar_home.*
class HomeActivity : DaggerAppCompactActivity(){
lateinit var homeBinding: ActivityHomeBinding
lateinit var homeViewModel: HomeViewModel
lateinit var mapFragment: SupportMapFragment
override fun onCreate(savedInstanceState: Bundle?) {
//AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
homeBinding = DataBindingUtil.setContentView(this,
R.layout.activity_home)
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
val includedLayout = homeBinding.includedLayout
setSupportActionBar(includedLayout?.toolbar)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
mapFragment = supportFragmentManager.findFragmentById(R.id.map_fragment)
as SupportMapFragment
LocationController(mapFragment, this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.home, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
}
package com.edijae.crusar.reporter.controllers
import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.places.Places
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
class LocationController :
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,
OnMapReadyCallback {
var map: GoogleMap? = null
lateinit var apiClient: GoogleApiClient
constructor(mapFragment: SupportMapFragment, activity: AppCompatActivity) {
apiClient = GoogleApiClient.Builder(activity)
.enableAutoManage(activity, this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build()
apiClient.connect()
mapFragment.getMapAsync(this)
}
override fun onMapReady(p0: GoogleMap?) {
map = p0
val uiSettings = map?.getUiSettings()
uiSettings?.isMyLocationButtonEnabled = true
uiSettings?.isCompassEnabled = true
uiSettings?.isMapToolbarEnabled = false
uiSettings?.isTiltGesturesEnabled = true
uiSettings?.isZoomControlsEnabled = true
map?.setMapType(GoogleMap.MAP_TYPE_NORMAL)
map?.setTrafficEnabled(true)
map?.setBuildingsEnabled(true)
map?.setIndoorEnabled(true)
map?.setMyLocationEnabled(true)
}
override fun onConnected(p0: Bundle?) {
}
override fun onConnectionSuspended(p0: Int) {
}
override fun onConnectionFailed(p0: ConnectionResult) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment