I have a small dilemma with the app I am making. Basically, the app is for a game, to display information about each creature in the game. There are at least 30 creatures in the game, so I made a class and layout file for each creature, so I could display different information for each one. The question I am asking, is, is there a way for me to have like one static class file and when I clicked a button to go to one creatures' page, instead of creating a whole class and layout file for that one creature, it would just change the all the strings in the one static class to the information I need? I am very new to Java programming so I only know a few things. I was just wanting to make the code a little cleaner. Thanks for the help!
Here is one of the Creature's class files (there are over 20 of these doing absolutely nothing). Here is an example of Bat.java:
public class Bat extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userTheme = prefs.getString("theme", "main");
if (userTheme.equals("main"))
setTheme(R.style.MainTheme);
else if (userTheme.equals("light"))
setTheme(R.style.HoloLight);
else if (userTheme.equals("lightdark"))
setTheme(R.style.HoloLightDark);
else if (userTheme.equals("dark"))
setTheme(R.style.HoloTheme);
setContentView(R.layout.bat);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
inflater.inflate(R.menu.options, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.statistics:
Intent intent0 = new Intent(Bat.this, Statistics.class);
startActivity(intent0);
return true;
case R.id.funfacts:
Intent intent1 = new Intent(Bat.this, FunFacts.class);
startActivity(intent1);
return true;
case R.id.home:
Intent intent2 = new Intent(Bat.this, Home.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The real info for the creatures goes on my xml files (which I also have over 20 of) Here is an example of Bat.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="144dp"
android:src="@drawable/batmc" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overview"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bats are the second smallest mob in the game. They will spawn only in large caves, or with a spawn egg. They may also spawn in a house, considering its dark, and big enough."
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Behavior"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bats sleep during the day, and become active at night. While idle, a bat will hang upside down until a player approaches, which it will then fly away. They cannot hang on non-solid or transparent blocks. If you place a bat in a minecart, it will move the minecart on its own."
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:visibility="invisible" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drops"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="None"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_column="1"
android:text="Spawn - Light level 10 or less"
android:padding="3dip"
android:gravity="left"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_column="1"
android:text="Health - 6"
android:padding="3dip"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_column="1"
android:text="First Appearance - 12w28a"
android:padding="3dip"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Network ID - 65"
android:padding="3dip"
android:textSize="18dip" />
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</LinearLayout>
</ScrollView>