Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share Intent text using Kotlin on Android

I want to share text in my CardView using share Intent using kotlin but there is a problem with last line in the code in kotlin the code

 val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND
            shareIntent.putExtra(Intent.EXTRA_STREAM, "ali")
            shareIntent.type = "text/plain"
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))

here is the problem in the code

startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)))

please help me

see the images to understand me

images

https://ibb.co/jQwYXw

https://ibb.co/id0tXw

https://ibb.co/fbCU5G

the adapter full code

class MyAdapter(context: Context, listItem: ArrayList<com.EliteTeam.comedytaste.Model.Movie>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

var Context = context
var movieList = listItem;
var layoutInflator = LayoutInflater.from(context)

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {

    var inflateView = layoutInflator.inflate(R.layout.single_item, parent, false)

    return MyViewHolder(inflateView)
}

override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {

    holder?.moviewTitle?.text = movieList[position].name
    holder?.movieDescription!!.text=   movieList[position].description
    //holder!!.cardImageView!!.background=   movieList[position].image

    holder?.onclick(Context, position)
    holder!!.cardImageView.setBackgroundResource(movieList[position].image)
}

override fun getItemCount(): Int {

    return movieList.size
}

class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

    var moviewTitle: TextView = itemView?.findViewById(R.id.movieTitleTextView)!!
    var movieDescription: TextView = itemView!!.findViewById(R.id.movieDescriptionTextView)
    var cardImageView: CardView = itemView!!.findViewById(R.id.imageCard)
    var share: ImageButton = itemView!!.findViewById(R.id.share)

    fun onclick(context: Context, position: Int) {
        cardImageView.setOnClickListener {

    }

       share.setOnClickListener {

           val shareIntent = Intent()
         shareIntent.action = Intent.ACTION_SEND
           shareIntent.putExtra(Intent.EXTRA_TEXT, "ali")
          shareIntent.type = "text/plain"


           startActivity(Intent.createChooser(shareIntent,"send to"))

}} }}
like image 475
Ali Alsaadi Avatar asked Dec 02 '17 19:12

Ali Alsaadi


People also ask

How do I share text with intent?

Sending text contentIntent sendIntent = new Intent(); sendIntent. setAction(Intent. ACTION_SEND);

How do I send intent to Kotlin?

Add the following code in the MainActivity.The putExtra(key, value) method of Intent class send the data to the SecondActivity. kt class. The startActivity() method starts the Intent. Create another activity class named as SecondActivity.

How do I send intent to another app?

Intent chooser = Intent. createChooser(intent, title); startActivity(chooser); This displays a dialog with a list of apps that respond to the intent passed to the createChooser() method and uses the supplied text as the dialog title.

How to share intent from intentservice in Android?

This example demonstrate about How to share intent from intentservice. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view.

How to detect an unsafe intent in Kotlin?

In Kotlin, the following code is a programmatic way to detect an unsafe intent: Note: In newer versions of Android i.e., version 12, when using the detectAll () method in declaring VmPolicy, detectUnsafeIntentLaunch is involuntary invoked.

How do I share an Android sheet with an intent?

For all types of sharing, create an intent and set its action to Intent.ACTION_SEND. In order to display the Android Sharesheet you need to call Intent.createChooser() , passing it your Intent object. It returns a version of your intent that will always display the Android Sharesheet.

How to share a text message with an intent?

By Creating an Intent using ACTION_SEND you will be able to put extra its type is Intent.EXTRA_TEXT, the second argument is the text you want to share. Then by setting the share type as text/plain, the Intent service will bring you all apps that support sharing text


2 Answers

Try this :- Use Intent.EXTRA_STREAM only when you have to send Binary data like images , if you want to sent text use Intent.EXTRA_TEXT

    val shareIntent = Intent()
    shareIntent.action = Intent.ACTION_SEND
    shareIntent.type="text/plain"
    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    startActivity(Intent.createChooser(shareIntent,getString(R.string.send_to)))

if using this code in adapter then last line should be context.startActivity(Intent.createChooser(shareIntent,getString(R.string.send_to)))

like image 151
Suraj Nair Avatar answered Sep 19 '22 03:09

Suraj Nair


Share text in activity

    val intent = Intent(Intent.ACTION_SEND)
    intent.putExtra(Intent.EXTRA_TEXT, "shareTextHere")
    intent.type = "text/plain"
    startActivity(Intent.createChooser(intent, "Share Via"))

Share text in adapter

    val intent = Intent(Intent.ACTION_SEND)
    intent.putExtra(Intent.EXTRA_TEXT, "shareTextHere")
    intent.type = "text/plain"
    context.startActivity(Intent.createChooser(intent, "Share Via"))
like image 28
Sumit Ojha Avatar answered Sep 23 '22 03:09

Sumit Ojha