How to Share text of TextView on Button click - Android Studio
In this tutorial we will learn how to share text of a TextView on button click. When the button is clicked bottom shit will appear listing the apps that can share that text...Step 1: Create a new project OR Open your project
Step 2: Code
activity_main.xml<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..." /> <Button android:id="@+id/btnShare" android:text="Share Text" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
MainActivity.java
package com.blogspot.devofandroid.myapplication;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final TextView mTextView = findViewById(R.id.textView);Button mBtnShare = findViewById(R.id.btnShare);mBtnShare.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String s = mTextView.getText().toString();Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);sharingIntent.setType("text/plain");sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT , "Subject Here");sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT , s);startActivity(Intent.createChooser(sharingIntent , "Share text via"));}});}}
0 Response to "Share Text Of Textview On Button Click - Android Studio"