Background: I am writing an Android app, mostly following instructions from the official developer guides. I have some experience with writing Java code but little with xml and Android.
Question: I would like to pass information from variables in my static class "PlaceholderFragment" (which is contained by my "BoardContainer" class) to my fragment layout file "fragment_board.xml". PlaceholderFragment looks like this (mostly unedited after Eclipse created it for me):
public static class PlaceholderFragment extends Fragment {
public int nButtons = 2;
public static class PlaceholderFragment extends Fragment {
private int nButtons = 2;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_board,
container, false);
return rootView;
}
}
(other lifecycle callbacks have not yet been implemented).
Now my fragment_board.xml is like this:
<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$PlaceholderFragment" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="49dp"
android:layout_height="49dp"
android:contentDescription="@null"
android:onClick="buttonPressed" //not yet implemented
android:src="@drawable/grid2" />
</RelativeLayout>
Here I would like to use the int
instance variable nButtons
so that, for example, if nButtons==7
, then we get android:src="@drawable/grid7
instead of grid2
, or that the layout file will contain seven ImageButtons instead of just one, and so forth. In other words, how do I make the xml file read and understand the instance variables from its corresponding class?