Android sharing selected text to other apps

Posted on

Problem

I am somewhat new to Java. I want to know if this is the proper way to share text with other Android applications, and if I am properly determining if the text is selected. Is there a better way to share the selected text? Is my decision statement proper? The app works just fine.

package com.kylelk.sharedemo;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    private Button shareBtn;
    private EditText textToShare;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        shareBtn = (Button) findViewById(R.id.shareText);   
        shareBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v){

                //Get the selected text
                EditText et=(EditText)findViewById(R.id.textToShare);
                int startSelection=et.getSelectionStart(); 
                int endSelection=et.getSelectionEnd();
                String selectedText = et.getText().toString().substring(startSelection, endSelection);

                //if no text is selected share the entire text area.
                if(selectedText.length() == 0){
                    textToShare = (EditText) findViewById(R.id.textToShare);
                    String dataToShare = textToShare.getText().toString();
                    selectedText = dataToShare;
                }

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

            }           
        });             
    }
}

Here is the complete project: https://github.com/kylelk/ShareDemo

Solution

Honestly I believe you have answered this question yourself: The app works just fine.

From what I can see, you are using the intent exactly as you are supposed to. You are also using EditText properly. But, because you have come all the way here I guess you want to hear some suggestions/criticism as well so I have tried really hard to come up with something:

  • You’re inconsistent in adding spaces to your code. The best practice is to use VariableType variable = something; but in three lines you are writing VariableType variable=something; which makes the code a little bit harder to read.

  • I don’t think you need to temporarily store the selectionStart and selectionEnd variables. (If you think it looks cleaner to store them in temporary variables, then by all means continue to do so) This code would be enough:

    String selectedText = et.getText().toString().substring(et.getSelectionStart(), et.getSelectionEnd());
    

    Or if you think that line is too long,

    String text = et.getText().toString();
    String selectedText = text.substring(et.getSelectionStart(), et.getSelectionEnd());
    
  • Consider what would happen if there is nothing at all stored in the EditText, do you still want to start an intent even if the string is empty?

Leave a Reply

Your email address will not be published. Required fields are marked *