MDRams

Untitled

Nov 2nd, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package yo.share2nd;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.CompoundButton;
  10. import android.widget.Toast;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16.  
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22.  
  23. final String value1 = "bread";
  24. final String value2 = "water";
  25. final short value3 = 5;
  26. final char value4 = 'A';
  27. Button button = (Button) findViewById(R.id.button);
  28.  
  29.  
  30. // Create StringBuilder and add four values to it.
  31. final ArrayList<String> builder = new ArrayList<String>();
  32.  
  33.  
  34. final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
  35. checkBox1.setChecked(false);
  36.  
  37. checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  38. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  39. if (checkBox1.isChecked()) {
  40. builder.add((String.valueOf(value1)));
  41. builder.add("\n");
  42. Toast.makeText(MainActivity.this, "item selected", Toast.LENGTH_SHORT).show();
  43. }
  44. else{
  45. builder.remove(String.valueOf(value1));
  46. builder.remove("\n");
  47. Toast.makeText(MainActivity.this, "item deselected", Toast.LENGTH_SHORT).show();
  48. }
  49. }
  50. });
  51.  
  52. final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
  53. checkBox2.setChecked(false);
  54.  
  55. checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  56. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  57. if (checkBox2.isChecked()) {
  58. builder.add(String.valueOf(value2));
  59. builder.add("\n");
  60. Toast.makeText(MainActivity.this, "item selected", Toast.LENGTH_SHORT).show();
  61. }
  62. else{
  63. builder.remove(String.valueOf(value2));
  64. builder.remove("\n");
  65. Toast.makeText(MainActivity.this, "item deselected", Toast.LENGTH_SHORT).show();
  66. }
  67. }
  68. });
  69.  
  70. button.setOnClickListener(new View.OnClickListener()
  71.  
  72. {
  73. public void onClick(View v) {
  74. //create the send intent
  75.  
  76. String result = builder.toString();
  77. result.replaceAll("\\[", "").replaceAll("\\]", "");
  78. Intent shareIntent =
  79. new Intent(Intent.ACTION_SEND);
  80.  
  81. //set the type
  82. shareIntent.setType("text/plain");
  83.  
  84. //add a subject
  85. shareIntent.putExtra(Intent.EXTRA_SUBJECT,
  86. "This is your list");
  87.  
  88. //add the message
  89. shareIntent.putExtra(Intent.EXTRA_TEXT, result);
  90.  
  91. //start the chooser for sharing
  92. startActivity(Intent.createChooser(shareIntent, "Choose how to share your list"));
  93. }
  94. });
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment