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

Android Build a Simple Android App (retired 2014) Pretty Little Things Animating the Answer

Ricky Sparks
Ricky Sparks
22,249 Points

Been lost :( my text Answer in emulator is always transparent and cannot animate.

My button is also still standard text and not the fancy purple font/color

8 Answers

Ricky Sparks
Ricky Sparks
22,249 Points

package com.example.crystalball;

import android.graphics.drawable.AnimationDrawable; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.animation.AlphaAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private CrystalBall mCrystalBall = new CrystalBall();
private TextView mAnswerLabel;
private Button mGetAnswerButton;
private ImageView mCrystalBallImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Assign the Views from the layout file
    mAnswerLabel = (TextView) findViewById(R.id.textView1);
     mGetAnswerButton =  (Button) findViewById(R.id.button1);
     mCrystalBallImage = (ImageView) findViewById (R.id.imageView1);

    mGetAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String answer = mCrystalBall.getAnAnswer();


            // Update the label with our dynamic answer 
            mAnswerLabel.setText(answer);

            animateCrystalBall();
            animateAnswer();
            playSound();
        }
    });
}

private void animateCrystalBall (){
    mCrystalBallImage.setImageResource(R.drawable.ball_animation);
    AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable();
    if (ballAnimation.isRunning()) {
        ballAnimation.stop();
    }
    ballAnimation.start();
}

private void animateAnswer () {
    AlphaAnimation fadeInAnimation = new AlphaAnimation (0 , 1);
    fadeInAnimation.setDuration(1500);
    fadeInAnimation.setFillAfter(true);

    mAnswerLabel.setAnimation(fadeInAnimation);
}

private void playSound(){
    MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
    player.start();
    player.setOnCompletionListener( new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Ricky Sparks
Ricky Sparks
22,249 Points

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.crystalball.MainActivity$PlaceholderFragment" android:background="@android:color/black" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/ball01" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@android:drawable/btn_default"
    android:text="Enlighten me!" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:weightSum="1" >

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.2" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:textSize="32sp" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.2" />

</LinearLayout>

</RelativeLayout>

Charlie Thomas
Charlie Thomas
40,856 Points

Sorry have you tried testing on a different emulator / device

Ricky Sparks
Ricky Sparks
22,249 Points

Yeah but I did fix the button font problem from the html code you gave earlier. let me check the " button's popup text answers" one in my emulator.

Charlie Thomas
Charlie Thomas
40,856 Points

You have not got any shadow on your text view so try modifying your button code to look like this:

<TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:textSize="32sp" 
        android:textColor="@android:color/white"
        android:gravity="center_horizontal"
        android:shadowColor="@android:color/white"
        android:layout_weight="0.6"
        android:width="0dp"
        android:shadowRadius="25"/>
Ricky Sparks
Ricky Sparks
22,249 Points

later in the lesson the button is instructed to be deleted. But my pop answer text is still dark?

Charlie Thomas
Charlie Thomas
40,856 Points

Sorry don't modify your button with code above it should be TextView. I have edited it above

Ricky Sparks
Ricky Sparks
22,249 Points

Thanks the code should work when I shake it on a android device can't test it now since my windows phone is a different OS. You added two android:layout_weight="0.6" though. Anyways thank you!!! :D

Charlie Thomas
Charlie Thomas
40,856 Points

No problem you should look at genymoition (in the Android Tools project) as I think it allows you it simulate shaking not sure though.