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

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

TextView not showing

Hello guys,

So, i was following the Android Track on which we had to create the crystall ball app, and when i had to delete the button i think i did something else wrong, because the text is not showing after on the OnShake method. Resuming, when i shake my phone, the text doesn´t show.

Can you help to repair this error? I tried recreating the TextView and the View inside of the LinearLayout but didnt work either. Tried rewriting the OnClickListener method but didn´t work as well.

3 Answers

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

I think i´ve deleted some wrong things on the Graphical Layout when i delete the button to follow the course.

Here it goes:

<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=".MainActivity" >

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        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/View2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

        <TextView
            android:id="@+id/textView1"
            android:textStyle="bold"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.6"
            android:textSize="26sp"
            android:textColor="#3f0f7f"
            android:text="" />

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

    </LinearLayout>

</RelativeLayout>
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Ah, this one took me a few minutes to figure out! There is an issue with the spacer views you're using on the sides of the TextView. You have the widths set to wrap_content, but they need to be 0dp for the weight stuff we're doing to work out properly. The Android system dynamically adjusts them all but they have to be 0dp.

That will get your text to show, but it's a little off. You don't need the margins on the TextView, so you can get rid of:

android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"

Then, if you want to center the text inside the TextView, you'll need to add one more attribute:

android:gravity="center_horizontal"
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you paste all your code from MainActivity.java in here? We can help you troubleshoot from there. :)

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Hey Ben,

Following:

package com.doubloo.crystalball;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.doubloo.crystalball.ShakeDetector.OnShakeListener;




public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

    private CrystalBall mCrystalBall = new CrystalBall();
    private TextView mAnswerLabel;
    private ImageView mCrystalBallImage;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;


    @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);
        mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector (new OnShakeListener() {

            @Override
            public void onShake() {
    handleNewAnswer();
            }
        });
        //Toast.makeText(this, "Yay, Lerdo!", Toast.LENGTH_LONG).show();

Log.d(TAG, "We´re logging from the onCreate() method!"); 
    }

    @Override
    public void onResume(){
        super.onResume();
        mSensorManager.registerListener(mShakeDetector, mAccelerometer, 
                SensorManager.SENSOR_DELAY_UI);

    }
    @Override

    public void onPause(){
        super.onPause();
        mSensorManager.unregisterListener(mShakeDetector);

    }

    private void animateCrystalBall(){
        mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);
        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(){
            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){
        int id = item.getItemId();
        if(id == R.id.action_settings){
            return true;

        }
        return super.onOptionsItemSelected(item);
    }

    private void handleNewAnswer() {
        String answer = mCrystalBall.getAnAnswer();

                    mAnswerLabel.setText(answer);

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

        };
Ben Jakuben
Ben Jakuben
Treehouse Teacher

It looks like this code is okay. Can you paste in the layout code from activity_main.xml? Perhaps something is wrong in there. If you continue to have trouble then I'll work with you to share with me your project files to help with troubleshooting.