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. */
    public void onCreate(Bundle savedInstanceState)

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

            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.putExtra(Intent.EXTRA_TEXT, selectedText); 


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

  • 1
    \$\begingroup\$ While Code Review does focus on reviewing working code, I think this specific question would be better answered by Android programmers, it's more about correctly using the API. \$\endgroup\$ – Quentin Pradet Dec 2 '13 at 10:19

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?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.