Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I display gradually an string(visibility) from beginning to end?
    text
    copied!<p>I am new t android development and are having some issues with a Hang-Man game.</p> <p>This is how i Want it to work: 1. when the app starts the db is opened (db.open()) and a randomed word is fetched from the db (db.random()) and put in generatedWord String. The generatedWord is then made into the Spannablestring text (SpannableString text = new SpannableString(generatedWord);) and the whole text is made into color white/invisible.</p> <ol> <li><p>when a user enters a char into the textview userInput I want to save it to lastInput vairable by using onKeyListener: lastInput = (char)event.getUnicodeChar();</p></li> <li><p>and then when Enter button is hit the guess() should be called which sends lastInput s an argument. there it checks if lastInput is in indexOf generatedWord and if it is it will make the text of the chars black and visible again and if not it should call showImages() which in turn makes images visible.</p></li> </ol> <p>THING IS! The db is working, I just got the app going with the gui and everything, then I made some small change and now I cant launch the app again and only get FATAL ERRORS. What have I done wrong and what can i think about?</p> <p>Many thanks</p> <p>// MAINACTIVITY</p> <pre><code> package com.emanuelolsson.simplehangman; import database.DBAdapter; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class HangMan extends Activity { // DECLARE NEEDED VARIABLES private String generatedWord = ""; private char lastInput = '\0'; private Button newGameButton, enterLetterButton; private EditText wordHolder, userInput; private ImageView imageOne, imageTwo, imageThree, imageFour, imageFive, imageSix, imageSeven, imageEight, winner, hanged; private SpannableString text = new SpannableString(generatedWord); private int count = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hang_man); DBAdapter db = new DBAdapter(this); // ASSIGN OBJECTS newGameButton = (Button) findViewById(R.id.newGame); enterLetterButton = (Button) findViewById(R.id.enter); wordHolder = (EditText) findViewById(R.id.wordHolder); userInput = (EditText) findViewById(R.id.userInput); imageOne = (ImageView) findViewById(R.id.imageView1); imageTwo = (ImageView) findViewById(R.id.imageView2); imageThree = (ImageView) findViewById(R.id.imageView3); imageFour = (ImageView) findViewById(R.id.imageView4); imageFive = (ImageView) findViewById(R.id.imageView5); imageSix = (ImageView) findViewById(R.id.imageView6); imageSeven = (ImageView) findViewById(R.id.imageView7); imageEight = (ImageView) findViewById(R.id.imageView8); winner = (ImageView) findViewById(R.id.winner); hanged = (ImageView) findViewById(R.id.hanged); imageOne.setVisibility(View.INVISIBLE); imageTwo.setVisibility(View.INVISIBLE); imageThree.setVisibility(View.INVISIBLE); imageFour.setVisibility(View.INVISIBLE); imageFive.setVisibility(View.INVISIBLE); imageSix.setVisibility(View.INVISIBLE); imageSeven.setVisibility(View.INVISIBLE); imageEight.setVisibility(View.INVISIBLE); winner.setVisibility(View.INVISIBLE); hanged.setVisibility(View.INVISIBLE); // ADD LISTENERS newGameButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { } }); enterLetterButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { guess(lastInput); } }); userInput.setOnKeyListener(new View.OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { lastInput = (char)event.getUnicodeChar(); return false; } }); db.open(); db.randomize(generatedWord); SpannableString text = new SpannableString(generatedWord); text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0); wordHolder.setText(text); db.close(); } // What happens when user clicks Enter // If the guess (char) is in the index of generated word and not -1 it will span back hopefully to all char public void guess(char guess) { guess = Character.toUpperCase(guess); if (generatedWord.indexOf(lastInput) != -1) { text.setSpan(new ForegroundColorSpan(Color.BLACK), text.charAt(lastInput), text.charAt(lastInput), 0); //guessedChars.add(guess); } else { showImages(); } } private void showImages() { count ++; if (count == 1) { imageOne.setVisibility(View.INVISIBLE); } else if (count == 2) { imageTwo.setVisibility(View.INVISIBLE); } else if (count == 3) { imageThree.setVisibility(View.INVISIBLE); } else if (count == 4) { imageFour.setVisibility(View.INVISIBLE); } else if (count == 5) { imageFive.setVisibility(View.INVISIBLE); } else if (count == 6) { imageSix.setVisibility(View.INVISIBLE); } else if (count == 7) { imageSeven.setVisibility(View.INVISIBLE); } else if (count == 8) { gameEnd(); } } private void gameEnd() { if (count == 8) { hanged.setVisibility(View.VISIBLE); } else { winner.setVisibility(View.VISIBLE); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_hang_man, menu); return true; } } </code></pre> <p>// DBAdapter</p> <pre><code> package database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DBAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_WORDS = "words"; private static final String TAG = "DBAdapter"; private static final String DATABASE_NAME = "dbnames"; private static final String DATABASE_TABLE = "tblnames"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table tblnames (_id integer primary key autoincrement, " + "words text not null);"; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS tblnames"); onCreate(db); } } // Open DB public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } // Close DB public void close() { DBHelper.close(); } // INsert name public long insertWord(String word) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_WORDS, word); return db.insert(DATABASE_TABLE, null, initialValues); } // Delete a name public boolean deleteWord(long rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) &gt; 0; } // Get all names public Cursor getAllWords() { return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_WORDS}, null, null, null, null, null); } // Get a certain name public Cursor getWord(long rowId) throws SQLException { Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_WORDS}, KEY_ROWID + "=" + rowId, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } // Update name public boolean updateWord(long rowId, String word) { ContentValues args = new ContentValues(); args.put(KEY_WORDS, word); return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) &gt; 0; } // Clears DB public void clear() { db.delete(DATABASE_TABLE, "1", null); } public void randomize(String word) { @SuppressWarnings("unused") Cursor cursor; cursor = this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null); } } </code></pre> <p>// LAYOUT</p> <pre><code> &lt;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" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/one" android:contentDescription="@string/picOne" /&gt; &lt;ImageView android:id="@+id/imageView2" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/two" android:contentDescription="@string/picTwo" /&gt; &lt;ImageView android:id="@+id/imageView3" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:contentDescription="@string/picThree" android:src="@drawable/three" /&gt; &lt;ImageView android:id="@+id/imageView4" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/four" android:contentDescription="@string/picFour" /&gt; &lt;ImageView android:id="@+id/imageView5" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/five" android:contentDescription="@string/picFive" /&gt; &lt;ImageView android:id="@+id/imageView6" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/six" android:contentDescription="@string/picSix" /&gt; &lt;ImageView android:id="@+id/imageView7" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/seven" android:contentDescription="@string/picSeven" /&gt; &lt;ImageView android:id="@+id/imageView8" android:layout_width="150dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="43dp" android:src="@drawable/eight" android:contentDescription="@string/picEight" /&gt; &lt;EditText android:id="@+id/wordHolder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:ems="10" android:inputType="text" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;Button android:id="@+id/enter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageView1" android:layout_below="@+id/wordHolder" android:layout_marginTop="28dp" android:text="@string/enter" /&gt; &lt;EditText android:id="@+id/userInput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/enter" android:layout_alignLeft="@+id/wordHolder" android:layout_alignTop="@+id/enter" android:layout_toLeftOf="@+id/enter" android:ems="10" android:inputType="text" /&gt; &lt;ImageView android:id="@+id/winner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/imageView1" android:layout_centerHorizontal="true" android:src="@drawable/winner" android:contentDescription="@string/winner" /&gt; &lt;ImageView android:id="@+id/hanged" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/imageView1" android:layout_centerHorizontal="true" android:src="@drawable/hanged" android:contentDescription="@string/hanged" /&gt; &lt;Button android:id="@+id/newGame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/enter" android:layout_alignBottom="@+id/enter" android:layout_toRightOf="@+id/enter" android:text="@string/newGame" /&gt; &lt;/RelativeLayout&gt; </code></pre>
 

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