Note that there are some explanatory texts on larger screens.

plurals
  1. POChange Image on Correct Password
    text
    copied!<p>So I am a complete newb, and am currently taking an intro to Mobile Programming course in which we use Android (I have some experience with Java). I am trying to do a simple assignment which displays a text field and an image, and upon entering the correct "password" and pressing enter, the image changes.</p> <p>Should be so simple! But I am having a really hard time with this and can't figure out what I am doing wrong, even after doing a good bit of searching (I assume it is something super obvious and I'm missing it).</p> <p>Here is my code:</p> <pre><code>package CS285.Assignment1; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.ImageView; public class DisplayImage extends Activity implements OnKeyListener{ private EditText enteredText; private String pass = "monkey"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); enteredText = (EditText)findViewById(R.id.passField); enteredText.setOnKeyListener(this); } public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) &amp;&amp; (keyCode == KeyEvent.KEYCODE_ENTER)){ // Perform action on key press switchImage(); return true; } return false; } public void switchImage(){ if(enteredText.getText().toString() == pass){ ImageView imgView = (ImageView)findViewById(R.id.Image); imgView.setImageResource(R.drawable.marmoset); } } } </code></pre> <p>and my main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/textPrompt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff993300" android:text="Please enter password to see my real picture:" &gt; &lt;/TextView&gt; &lt;EditText android:id="@+id/passField" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;/EditText&gt; &lt;ImageView android:id="@+id/Image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:adjustViewBounds="true" android:src="@drawable/airplane" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>I thought at first that I was not properly extracting the String from "enteredText" so the comparison to the "password" wasn't happening correctly, but I have since tried just printing the enteredText and it works fine.</p> <p>Totally frustrated--Any help would be greatly appreciated!</p> <p>Daniel</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload