Background: I have previously asked this question on the similar topic on how to pass an instance variable from a Java class to it corresponding layout file in Android development. It turns out there is no easy way to do this, so the following is a follow-up question finding a good way to let the xml file and the class talk to each other.
Question: I am developing an game (nearly complete in Java Swing, now turning it into an Android app). It contains multiple levels, each with a differenly sized chess board and chess pieces - for all intents and purposes, it is a chess puzzle app that displays a new puzzle on a differently sized board after the player has solved the previous puzzle. The business logic is complete but I'm working on the graphics. At the game start, a static class BoardFragment
(contained by BoardContainer.class) with its corresponding layout xml file fragment_board.xml
should display the board size (as GridLayout) and chess pieces corresponding to the first level, which then updates as the level is completed. For clarification, the code looks somewhat like this (skip to the end to see my actual problem):
//Here is BoardFragment.class, static class contained by BoardContainer:
public static class BoardFragment extends Fragment {
public BoardFragment() {
}
public int level=0;
public void buttonPressed(View view) {
//method that will advance the game and update "level"
//depending on which button was clicked
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_board,
container, false);
return rootView;
}
}
//This is fragment_board.xml:
<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"
tools:context="com.example.exampletest.MainGame$BoardFragment" >
<android.support.v7.widget.GridLayout
xmlns:app="schemas.android.com/apk/res-auto"
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:columnCount="4" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="49dp"
android:layout_height="49dp"
android:contentDescription="@null"
android:onClick="buttonPressed"
android:src="@drawable/grid11" />
</android.support.v7.widget.GridLayout>
</RelativeLayout>
Note that for simplicity and space, only one image button is currently in the xml file, but with app:columnCount equaling 4, there would be 16. In fact, the number should depend on the "level" instance variable in the corresponding class (so that if level==5
then perhaps app:columnCount==6
and so forth) but I'm not sure how I would commmunicate that from the class file to the xml. Is it possible at all? In fact, when the level is completed, both the board size and number of pieces should change. Should this be done by
Having one xml fragment file for each level? Then the previous question is solved, but for many levels, it would lead to many fragment files - is that best practice?
Having one fragment xml file that updates as the level is completed?
I appreciate any help.