Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (retired 2014) Pretty Little Things Positioning Text Using a Linear Layout

my app works, but does something else

when i run my app then, the screen shows me the textview - "better not to tell you now", without pressing the button. the sentence apear immediately when i run the app, and when i press the button (enlighten me), then the sentence changes to "yes" i dont know what went wrong... big thanks for the helpers:)

9 Answers

Calvin Nix
Calvin Nix
43,828 Points

Hey,

It sounds like you are setting the textview variable outside of the OnClick method.

Does the textview always show "Better not tell you now"?

Or maybe you forgot to clear the text in your graphical layout editor. I remember in the video that he set the text to "Better not to tell you now" so that he could position the elements properly. Maybe you just forgot to delete this text?

yeah i know he deleted the text before running the app, but when i do that and run it , then i see the app with the word "yes" without pressing any button ,and when i press enlighten me button then nothing happens

HERE - THIS IS MY ACTIVITY_MAIN XML: <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:background="@android:color/black" android:gravity="fill" 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.freeapp.MainActivity$PlaceholderFragment" >

<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:text="enlighten me!"
    android:textColor="#3f0f7f"
    android:textSize="24sp"
    android:textStyle="bold|italic"
    android:typeface="serif" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="horizontal"
    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:gravity="center_horizontal"
        android:shadowColor="@android:color/white"
        android:shadowRadius="25"
        android:textColor="@android:color/white"
        android:textSize="32sp" />

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

</LinearLayout>

</RelativeLayout>

and this is my androidmanifest.xml : <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.freeapp" android:versionCode="1" android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.freeapp.MainActivity"
        android:label="@string/app_name" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Calvin Nix
Calvin Nix
43,828 Points

Ohad,

Could you attach your code from your MainActivity?

thanks

sure: <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:background="@android:color/black" android:gravity="fill" 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.freeapp.MainActivity$PlaceholderFragment" >

<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:text="enlighten me!"
    android:textColor="#3f0f7f"
    android:textSize="24sp"
    android:textStyle="bold|italic"
    android:typeface="serif" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="horizontal"
    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:gravity="center_horizontal"
        android:shadowColor="@android:color/white"
        android:shadowRadius="25"
        android:textColor="@android:color/white"
        android:textSize="32sp" />

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

</LinearLayout>

</RelativeLayout>

Calvin Nix
Calvin Nix
43,828 Points

Hey Ohad,

I'd like to see your java code.

Not the XML.

please can someone help me?

ok after what u asked, i looked at it and i found that some string variable is set to - "yes"!! maybe that was the problem, but i still wanna know what i did to make it happen, so here is the code:

package com.example.freeapp;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView  answerLabel = (TextView) findViewById(R.id.textView1);
        Button  getAnswerButton = (Button) findViewById(R.id.button1);
        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                String answer = "yes";
                answerLabel.setText(answer);

            }
        });



    }


    @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);
    }

}
Calvin Nix
Calvin Nix
43,828 Points

Hey Ohad,

Your application is pretty much set up to when the button is clicked to set the text to "yes".

public void onClick(View v) {
                // TODO Auto-generated method stub
                String answer = "yes";
                answerLabel.setText(answer);

            }
        });

You did not set an array with multiple response options paired with some code to choose a random index of the array to set when the button is pressed.

I would suggest to re-watch the video where he shows how to create an array and how to randomly choose an index of an array.