Skip to content

Instantly share code, notes, and snippets.

@Asutosh11
Last active June 19, 2018 10:52
Show Gist options
  • Save Asutosh11/341713d5fe113ec5a839815dff83e317 to your computer and use it in GitHub Desktop.
Save Asutosh11/341713d5fe113ec5a839815dff83e317 to your computer and use it in GitHub Desktop.
Kotlin notes: new things in kotlin and common Android codes in Kotlin
1. Coroutines in Kotlin -
/* Coroutines in Kotlin is the simplest way to do asynchronous tasks which we do in asynctask or using multiple threads
in RxJava.
*/
2(a). Calling a method of Activity inside Fragment -
// i. define mContext like this -
private lateinit var mContext: Context
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
mContext = inflater.context
val view: View = inflater.inflate(R.layout.fragment_text_recognition, container, false)
return view
}
// ii. Call the method of the Ativity like this inside the Fragment
// showDialogFragment is the function in Activity that accepts 'text_recognized' as parameter
(mContext as MainActivity).showDialogFragment(text_recognized)
2(b). Call method of Fragment inside parent Activity -
var fragment = supportFragmentManager.findFragmentById(R.id.frameLayoutCW) as WebViewFragment
fragment.callAboutUsActivity()
3. Save a new blank image file in Documents folder of device -
// Please note this 'Documents folder' local storage directory is a type of phone's exteral storage.
// SD card is also an external storage directory.
// Phone's internal storage directory is the app's directory that is only readable and writable from within the app
/**
When you save a new image to the same file, sometimes the new image will not be seen on checking the image in file manager,
instead the old image will be there because file managers like moto's default file manager use
some kind of cache/offline storage mechanism.
See the changes in a simple file manager like this one -
https://play.google.com/store/apps/details?id=com.simplemobiletools.filemanager
**/
var dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
var path = dir.absolutePath + "/" + "again.png"
var file = File(path)
file.createNewFile()
4. Fragment Transaction in Kotlin -
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
var fragment = FragHome()
fragmentTransaction.replace(R.id.frameLayout, fragment)
/**
* Add to backstack if you want the Activity or Fragment where you are in now
* to load up when you press back when being in the new fragment that you are loading now.
*
* If you don't want this place to backstack, just set like this -
* fragmentTransaction.addToBackStack(null)
*/
fragmentTransaction.addToBackStack("some_tag")
fragmentTransaction.commit()
5. Using BottomNavigationView on an Activity and selecting any of its item programatically. Both Layout and Activity code.
On selecting any item of the BottomNavigationView, it inflates a fragment on a FrameLayout on the Activity.
// activity_main.xml -
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
// MainActivity.kt -
class MainActivity : AppCompatActivity() {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
var fragment = FragHome()
fragmentTransaction.replace(R.id.frameLayout, fragment)
/**
* Add to backstack if you want the Activity or Fragment where you are in now
* to load up when you press back when being in the new fragment that you are loading now.
*
* If you don't want this place to backstack, just set like this -
* fragmentTransaction.addToBackStack(null)
*/
fragmentTransaction.addToBackStack("something")
fragmentTransaction.commit()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
var fragment = FragDash()
fragmentTransaction.replace(R.id.frameLayout, fragment)
fragmentTransaction.addToBackStack("something")
fragmentTransaction.commit()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
var fragment = FragNoti()
fragmentTransaction.replace(R.id.frameLayout, fragment)
fragmentTransaction.addToBackStack("something")
fragmentTransaction.commit()
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/**
* IMPORTANT : Selecting any item of BottomNavigationView programatically
* ---------
*
* This code is for programatically selecting the first item of the BottomNavigationView.
*
* Like wise you can programatically select any item of the BottomNavigationView
* by changing the indexItem values below.
*/
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
val indexItem = 0
navigation.menu.getItem(indexItem).isChecked = true
mOnNavigationItemSelectedListener.onNavigationItemSelected(navigation.menu.getItem(indexItem))
}
}
6. Making a Bitmap object and handling Bitmaps -
/*
Unless you are doing too much of engineering, Google recommends to use Glide for loading Bitmaps as Bitmap objects.
See this stackoverflow question on how to do it -
https://stackoverflow.com/questions/27394016/how-does-one-use-glide-to-download-an-image-into-a-bitmap
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment