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 trialohad shaboody
195 Pointsmy 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
43,828 PointsHey,
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?
ohad shaboody
195 Pointsyeah 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
ohad shaboody
195 PointsHERE - 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>
ohad shaboody
195 Pointsand 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
43,828 PointsOhad,
Could you attach your code from your MainActivity?
thanks
ohad shaboody
195 Pointssure: <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
43,828 PointsHey Ohad,
I'd like to see your java code.
Not the XML.
ohad shaboody
195 Pointsplease can someone help me?
ohad shaboody
195 Pointsok 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
43,828 PointsHey 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.