-1

I implemented Sharing intent button in my application. I have strings in textviews, but it is not always filled with any text. So, I would like to check it is empty or not. If a string is empty, sharing intent should ignore/remove/get ride of this string and go to next string. I tried to do this programmatically, but without success. How can I do this? I need some helpo:

public void buttonClick2(View view) {


            TextView textView1 = (TextView)findViewById(R.id.word);
            TextView textView2 = (TextView)findViewById(R.id.definition);
            TextView textView3 = (TextView)findViewById(R.id.definition2);
            TextView textView4 = (TextView)findViewById(R.id.definition3);




            boolean hasDefinition2 = false;
        if (textView3 !=null){
            String text3 = textView3.getText().toString();
            if(text3 !=null && !text3.isEmpty()){
                hasDefinition2 = true;
            }
        }

        String def2 ="";
        if(hasDefinition2){
            def2 +="\n Definition 2: "+textView3.getText().toString();
        }


        boolean hasDefinition3 = false;
        if (textView4 !=null){
            String text4 = textView4.getText().toString();
            if(text4 !=null && !text4.isEmpty()){
                hasDefinition3 = true;
            }
        }

        String def3 ="";
        if(hasDefinition3){
            def3 +="\n Definition 3: "+textView4.getText().toString();
        }   
            Intent shareIntent1 = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent1.setAction(android.content.Intent.ACTION_SEND);
            shareIntent1.setType("text/plain");
            shareIntent1.putExtra(android.content.Intent.EXTRA_SUBJECT, "Word"); //<=adding blank quotes

            shareIntent1.putExtra(Intent.EXTRA_TEXT, textView1.getText().toString()+
                    "\n"+Definition: +textView2.getText().toString()+
                    "\n"+def2+
                    "\n"+def3+
                    "\n"+"\n"+"@2016 Dictionary"+
                    "\n"+"Visit us:");    
            startActivity(Intent.createChooser(shareIntent1, "Share"));





        }
Holly Ikki
  • 199
  • 1
  • 10

1 Answers1

1

you can try the following to check if textView3/4 is empty or not: -

String extra =textView1.getText().toString()+
                "\n"+Definition: +textView2.getText().toString(); 

if (textView3 !=null){
    String text = textView3.getText().toString();
    if(text !=null && !text.isEmpty()){
        extra +="\n Definition 2:"+ textView3.getText().toString(); 
    }
}

if (textView4 !=null){
    String text = textView4.getText().toString();
    if(text !=null && !text.isEmpty()){ 
       extra +="\n Definition 3:"+ textView4.getText().toString();
    }
} 

extra += "\n"+"\n"+"@2016 Dictionary"+
         "\n"+"Visit us:"

shareIntent.putExtra(Intent.EXTRA_TEXT, extra);
startActivity( ... )

also do consider using StringBuilder instead of String for the extra.

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • Ok, it worked! But when text is empty, the sharing intent gives space as if there is a string in blank there when I share it to someone. I don't want this space. I want this "delete" textView when is empty, as if it doesn't exist and go to the next string. – Holly Ikki May 05 '16 at 12:38