we are going to create a simple Share Button in the in Android. The Share Button is used to share information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media.
We can share any type of message like text, images, videos, links, etc. Note that we are using Kotlin as the programming language.
val share_text_1_btn = findViewById(R.id.share_text_1_btn)
share_text_1_btn.setOnClickListener {
val intent= Intent()
intent.action=Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT,"Hey Check out this Great app:")
intent.type="text/plain"
startActivity(Intent.createChooser(intent,"Share To:"))
// Or
val intent2 = Intent(Intent.ACTION_SEND)
intent2.putExtra(Intent.EXTRA_TEXT,"Hey Check out this Great app:")
intent2.type="text/plain"
startActivity(Intent.createChooser(intent,"Share To:"))
}
In Java
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
// change the type of data you need to share,
// for image use "image/*"
// intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
startActivity(Intent.createChooser(intent, "Share"));
Dial Phone Call
open phone and and dial number from intent
val intent = Intent(Intent.ACTION_DIAL)
// 10 digit number with country code
val number = "+911234567899"
intent.data = Uri.parse("tel:$number")
startActivity(intent)
Open A Website From URL
val url = "https://www.google.com"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
Open Camera to Capture An Image
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity (intent)