Skip to content

Instantly share code, notes, and snippets.

@NaserKhoshfetrat
Last active January 22, 2022 14:21
Show Gist options
  • Save NaserKhoshfetrat/917c310727f658da9170ecedab7390aa to your computer and use it in GitHub Desktop.
Save NaserKhoshfetrat/917c310727f658da9170ecedab7390aa to your computer and use it in GitHub Desktop.
import android.content.Intent
import android.text.Html
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class ShareUtils constructor(val activity: AppCompatActivity) {
companion object : SingletonHolder<ShareUtils, AppCompatActivity>(::ShareUtils)
fun shareHtmlMessage(shareSubject: String?, shareBodyHtml: String?) {
shareTextMessage(shareSubject, Html.fromHtml(shareBodyHtml).toString())
}
fun shareTextMessage(shareSubject: String?, shareBody: String?) {
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubject)
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody)
if (sharingIntent.resolveActivity(activity.packageManager) != null) {
activity.startActivity(Intent.createChooser(sharingIntent, activity.getString(R.string.lblShareUsing)))
} else {
Toast.makeText(activity, activity.getString(R.string.txtShareError), Toast.LENGTH_SHORT).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment