简体   繁体   中英

How to take text from TextView in xml and share using a share button?

It seems as this question was asked before but I did not find the answer that I am looking for. I have two TextViews in .xml file first TextView is for Title and the second one is for Url. These two TextViews take data from realtime firebase database so the Title and the Url varies every time. Below the TextViews I have a share Button. When this button is clicked I want it to take data from two textViews and share via different socials like Gmail, facebook and others.

My .xml file looks like:

<TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:layout_marginTop="15dp"
        android:text="Title"
        android:textSize="30sp"
        android:textStyle="bold"/>

<TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/url"
        android:layout_marginTop="15dp"
        android:text="url"/>

<TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_share"
        android:layout_marginTop="15dp"
        android:text="Share"/>

So every time Url and Title changes and the Share button should be able to take the data that is currently there in the TextViews and share via different platforms

Create a Intent for sharing text and startActivity with that Intent

    buttonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             String title = textViewTitle.getText().toString();
             String uriStr = textViewUri.getText().toString();

             String shareString = title + ", " + uriStr;

             Intent sendIntent = new Intent();
             sendIntent.setAction(Intent.ACTION_SEND);
             sendIntent.putExtra(Intent.EXTRA_TEXT, shareString);
             sendIntent.setType("text/plain");
             startActivity(sendIntent);

        }
    });

Check this link for more.

So you should do this operation inside OnClick of your button ie

     public void onClick(View view)
     {
       String Title = textViewTitle.getText().toString().trim();
       String Url = textViewUrl.getText().toString().trim();

       \\ Share Button Logic Goes Here
     }

and remember to add -

   btnShare.setOnClickListener(this);

and class should implement View.OnClickListener.

This will do your job.

I would change the third TextView to an actual button. But if you do not want to do that, you can setOnClickListener on the third TextView and onClick() call getText.toString() on the first and second textviews. and concatenate them. Finally launch a share intent to share to different social media. Here is the solution to do just that. Use first solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM