Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Juliana Miranda
22,510 PointsHow could I share CrystalBall?
If I wanted to share the answer that Crystal Ball gives me on facebook, twitter or any other, how could I do this?
I tried to search on google, youtube and here, but couldn't find a way to do that, considering that I'm a beginner. I managed to add a share button, But I can't make it work. Is it too hard?
4 Answers
Kate Hrycak
5,542 PointsNot too sure myself, Juliana, but StackOverflow seems to have a plethora of material around this, such as: http://stackoverflow.com/questions/6814268/android-share-on-facebook-twitter-mail-ecc
Cheers,
Juliana Miranda
22,510 PointsThank you, Kate
I'll try this..
Cheers!
Ben Jakuben
Treehouse TeacherWe talk about sharing to apps that handle text sharing in the Blog Reader project. Take a look at the last stage in that one and see if it can help you adapt your Crystal Ball code. :)
Juliana Miranda
263 PointsThanks, I know it's been a long time, but I tried, and I was unable to adapt and build the intent to share... I wanted to share the final result, with the image and the text. I'll paste the code here (it's something that I adapted from CrystalBall...).
public class MainActivity extends Activity {
private PP mPP = new PP();
private TextView mAnswerLabel;
private Button mGetAnswerButton;
private ImageView mPreciosasPromessasImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare our view codes
mPreciosasPromessasImage = (ImageView) findViewById(R.id.imageView1);
mAnswerLabel = (TextView) findViewById(R.id.textView1);
mGetAnswerButton = (Button) findViewById(R.id.button1);
mGetAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String answer = mPP.getAnAnswer();
//Random selection of Verses.
mAnswerLabel.setText(answer);
animatePreciosasPromessas();
animateAnswer();
playSound();
}
});
}
private void animatePreciosasPromessas() {
mPreciosasPromessasImage.setImageResource(R.drawable.animation);
AnimationDrawable ppAnimation = (AnimationDrawable) mPreciosasPromessasImage.getDrawable();
if(ppAnimation.isRunning()) {
ppAnimation.stop();
}
ppAnimation.start();
}
private void animateAnswer() {
AlphaAnimation fadeInAnimation = new AlphaAnimation (0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setStartOffset(1000);
fadeInAnimation.setFillAfter(true);
mAnswerLabel.setAnimation(fadeInAnimation);
}
private void playSound() {
MediaPlayer player = MediaPlayer.create(this, R.raw.dreamharp06);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_share) {
shareVerse();
}
return super.onOptionsItemSelected(item);
}
private void shareVerse() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_chooser_title)));
}
}
Ben Jakuben
Treehouse TeacherHi Juliana,
Next time, add your post as a new one to bring it to the top of the recent page. :)
To share the text, you will need to add the text as an extra to your shareIntent:
shareIntent.putExtra(Intent.EXTRA_TEXT, mUrl);
Now sharing an image is a bit more complex. I'd recommend just getting text to work first, and then trying to do both the text and image. I don't have any code to share for it right now, but you need to use something like this: http://stackoverflow.com/questions/14326313/android-share-text-and-image
Juliana Miranda
263 PointsHi Ben,
Thanks a lot!! It worked for the text, I finally understood the mUrl (I wasn't sure if I should use or how to adapt it to this app by watching the video, but now I figured that out).
I'll try the image later.
I appreciate your help! :)