Android Open Source - android Quote Activity






From Project

Back to project page android.

License

The source code is released under:

MIT License

If you think the Android project android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2014 Rizart Dokollari/* w w w  .  j a v a 2 s.c  om*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliancex with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package me.rdokollari.funququiz;

import java.util.ArrayList;
import java.util.Random;

import me.rdokollari.geoquiz.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author Rizart Dokollari @ rdokollari.me
 * @since Mar 19, 2014
 * 
 */
public class QuoteActivity extends Activity {

  private static final String TAG = "QuoteActivity";
  private static final String KEY_INDEX = "index";

  private Button mFunnyButton;
  private Button mNotFunnyButton;
  private ImageView mNextButton;
  private ImageView mPreviousButton;
  private TextView mQuoteTextView;
  private Random random = new Random();

  private ArrayList<Quote> mQuoteBank = getQuotes();
  private int mCurrentIndex = 0;

  /**
   * Called when an instance of the activity subclass is created. When an
   * activity is created, it needs a user interface to manage.
   */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");

    // get the activity its user interface / inflates a layout and puts it
    // on screen
    setContentView(R.layout.activity_quote);

    mQuoteTextView = (TextView) findViewById(R.id.quote_text_view);
    mQuoteTextView.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        updateQuote();
      }
    });

    //updateQuote();

    // store references of inflated widgets
    mFunnyButton = (Button) findViewById(R.id.funny_button);
    mFunnyButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        checkAnswer(true);
      }
    });

    mNotFunnyButton = (Button) findViewById(R.id.not_funny_button);
    mNotFunnyButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        checkAnswer(false);
      }
    });

    mNextButton = (ImageView) findViewById(R.id.next_image_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        nextQuote();
      }
    });

    mPreviousButton = (ImageView) findViewById(R.id.previous_image_button);
    mPreviousButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        previousQuote();
      }
    });

    if (savedInstanceState != null) {
      mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
    } // end if
    
    updateQuote();

  } // end method onCreate

  /**
   * Save data across a runtime configuration change, like rotation
   */
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
  }

  /**
   * shows previous question. question index is sorted on mIndexes array list.
   */
  protected void previousQuote() {
    mCurrentIndex--;

    if (mCurrentIndex < 0) {
      mCurrentIndex = mQuoteBank.size();
    } // end if

    int question = mQuoteBank.get(mCurrentIndex).getQuote();
    mQuoteTextView.setText(question);
  }

  private void updateQuote() {
    int question = 0;

    question = mQuoteBank.get(mCurrentIndex).getQuote();

    mQuoteTextView.setText(question);
  }
  
  private void nextQuote() {
    mCurrentIndex++;

    if (mCurrentIndex > mQuoteBank.size()) {
      mCurrentIndex = 0;
    }

    int question = 0;

    question = mQuoteBank.get(mCurrentIndex).getQuote();

    mQuoteTextView.setText(question);
  }

  private ArrayList<Quote> getQuotes() {
    ArrayList<Quote> funnyList = new ArrayList<Quote>();

    funnyList.add(new Quote(R.string.quote_0, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_1, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_2, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_3, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_4, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_5, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_6, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_7, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_8, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_9, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_10, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_11, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_12, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_13, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_14, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_15, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_16, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_17, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_18, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_19, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_20, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_21, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_22, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_23, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_24, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_25, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_26, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_27, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_28, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_29, random.nextBoolean()));
    funnyList.add(new Quote(R.string.quote_30, random.nextBoolean()));

    return funnyList;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
  } // end method onCreateOptionsMenu

  /**
   * prints a text message indicating correct/incorrect answer
   * 
   * @param userPressedTrue
   */
  private void checkAnswer(boolean userPressedTrue) {
    boolean answerTrue = mQuoteBank.get(mCurrentIndex).isFunny();

    int messageResId = 0;

    if (userPressedTrue == answerTrue) {
      messageResId = R.string.positive_ai_feedback;
    } else {
      messageResId = R.string.negative_ai_feedback;
    } // end else

    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
  } // end function checkAnswer

  @Override
  public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart() called");
  }

  @Override
  public void onPause() {
    super.onPause();
    Log.d(TAG, "Log.d(TAG, onStart() called");
  }

  @Override
  public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume() called");
  }

  @Override
  public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop() called");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
  }
} // end class QuoteActivity





Java Source Code List

me.rdokollari.funququiz.QuoteActivity.java
me.rdokollari.funququiz.Quote.java
me.rdokollari.photogallery.FlickrFetchr.java
me.rdokollari.photogallery.GalleryItem.java
me.rdokollari.photogallery.PhotoGalleryActivity.java
me.rdokollari.photogallery.PhotoGalleryFragment.java
me.rdokollari.photogallery.SingleFragmentActivity.java