Skip to content

Instantly share code, notes, and snippets.

@nuttt
Created June 17, 2013 07:54
Show Gist options
  • Save nuttt/5795300 to your computer and use it in GitHub Desktop.
Save nuttt/5795300 to your computer and use it in GitHub Desktop.
JokeList - Android Lab 2
package edu.calpoly.android.lab2;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class SimpleJokeList extends Activity {
/** Contains the list Jokes the Activity will present to the user. */
protected ArrayList<Joke> m_arrJokeList;
/** LinearLayout used for maintaining a list of Views that each display Jokes. */
protected LinearLayout m_vwJokeLayout;
/** EditText used for entering text for a new Joke to be added to m_arrJokeList. */
protected EditText m_vwJokeEditText;
/** Button used for creating and adding a new Joke to m_arrJokeList using the
* text entered in m_vwJokeEditText. */
protected Button m_vwJokeButton;
/** Background Color values used for alternating between light and dark rows
* of Jokes. */
protected int m_nDarkColor;
protected int m_nLightColor;
protected int m_nTextColor;
protected int m_nCurrentColor;
protected ArrayList<Joke> m_arrjokeList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO
Resources res = this.getResources();
String[] jokeList = res.getStringArray(R.array.jokeList);
initLayout();
m_arrjokeList = new ArrayList<Joke>();
this.m_nDarkColor = res.getColor(R.color.dark);
this.m_nLightColor = res.getColor(R.color.light);
this.m_nTextColor = res.getColor(R.color.text);
this.m_nCurrentColor = this.m_nDarkColor;
for(int i=0; i<jokeList.length; i++){
Log.d("Lab2", "Add Joke " + i + " :"+jokeList[i].toString());
// this.m_arrjokeList.add(new Joke(jokeList[i]));
addJoke(jokeList[i]);
}
}
/**
* Method used to encapsulate the code that initializes and sets the Layout
* for this Activity.
*/
protected void initLayout() {
// TODO
m_vwJokeLayout = new LinearLayout(this);
m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView sw = new ScrollView(this);
sw.addView(m_vwJokeLayout);
setContentView(sw);
}
/**
* Method used to encapsulate the code that initializes and sets the Event
* Listeners which will respond to requests to "Add" a new Joke to the
* list.
*/
protected void initAddJokeListeners() {
// TODO
}
/**
* Method used for encapsulating the logic necessary to properly initialize
* a new joke, add it to m_arrJokeList, and display it on screen.
*
* @param strJoke
* A string containing the text of the Joke to add.
*/
protected void addJoke(String strJoke) {
// TODO
Joke myJoke = new Joke(strJoke);
this.m_arrjokeList.add(myJoke);
TextView tw = new TextView(this);
tw.setText(strJoke);
//Set color
tw.setBackgroundColor(this.m_nCurrentColor);
if(this.m_nCurrentColor == this.m_nDarkColor){
this.m_nCurrentColor = this.m_nLightColor;
} else {
this.m_nCurrentColor = this.m_nLightColor;
}
tw.setTextColor(this.m_nTextColor);
tw.setTextSize(TypedValue.COMPLEX_UNIT_PX, 16);
m_vwJokeLayout.addView(tw);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment