Latest News

Share Text Of Textview On Button Click (Kotlin) - Android Studio

In this tutorial we will learn how to share text of a TextView on button click. When the button is clicked bottom sheet will appear listing the apps that can share that text...
Share text of TextView on button click (Kotlin) - Android Studio

Step 2: Code

activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="10dp"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView"android:textAlignment="center"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="This is the text to be shared..." /><Buttonandroid:id="@+id/btnShare"android:text="Share Text"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

MainActivity.kt
package com.blogspot.devofandroid.myapplicationimport android.content.Intentimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)btnShare.setOnClickListener {//Get text from TextView and store in variable "s"val s = textView.text.toString()//Intent to share the textval shareIntent = Intent()shareIntent.action = Intent.ACTION_SENDshareIntent.type="text/plain"shareIntent.putExtra(Intent.EXTRA_TEXT , s);startActivity(Intent.createChooser(shareIntent ,"Share via"))}}}

Step 3: Output

Share text of TextView on button click (Kotlin) - Android Studio
Share text of TextView on button click (Kotlin) - Android Studio
Share text of TextView on button click (Kotlin) - Android Studio
Share text of TextView on button click (Kotlin) - Android Studio


0 Response to "Share Text Of Textview On Button Click (Kotlin) - Android Studio"