Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package yo.share2nd;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final String value1 = "bread";
- final String value2 = "water";
- final short value3 = 5;
- final char value4 = 'A';
- Button button = (Button) findViewById(R.id.button);
- // Create StringBuilder and add four values to it.
- final ArrayList<String> builder = new ArrayList<String>();
- final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
- checkBox1.setChecked(false);
- checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (checkBox1.isChecked()) {
- builder.add((String.valueOf(value1)));
- builder.add("\n");
- Toast.makeText(MainActivity.this, "item selected", Toast.LENGTH_SHORT).show();
- }
- else{
- builder.remove(String.valueOf(value1));
- builder.remove("\n");
- Toast.makeText(MainActivity.this, "item deselected", Toast.LENGTH_SHORT).show();
- }
- }
- });
- final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
- checkBox2.setChecked(false);
- checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (checkBox2.isChecked()) {
- builder.add(String.valueOf(value2));
- builder.add("\n");
- Toast.makeText(MainActivity.this, "item selected", Toast.LENGTH_SHORT).show();
- }
- else{
- builder.remove(String.valueOf(value2));
- builder.remove("\n");
- Toast.makeText(MainActivity.this, "item deselected", Toast.LENGTH_SHORT).show();
- }
- }
- });
- button.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View v) {
- //create the send intent
- String result = builder.toString();
- result.replaceAll("\\[", "").replaceAll("\\]", "");
- Intent shareIntent =
- new Intent(Intent.ACTION_SEND);
- //set the type
- shareIntent.setType("text/plain");
- //add a subject
- shareIntent.putExtra(Intent.EXTRA_SUBJECT,
- "This is your list");
- //add the message
- shareIntent.putExtra(Intent.EXTRA_TEXT, result);
- //start the chooser for sharing
- startActivity(Intent.createChooser(shareIntent, "Choose how to share your list"));
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment