1

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?

Sid
  • 563
  • 1
  • 10
  • 28

1 Answers1

1

Unfortunately XML files can not read and understand the variables from its corresponding class. Instead we can alter them programatically by obtaining a handle to components contained within XML files in a class file and altering them like this:

ImageButton imgHandle = (ImageButton) findViewById(R.id.imageButton1);

if(nButtons == 7) {
    imgHandle.setImageResource(R.id.grid7);
}

In a fragment you're going to need to use rootView inside of your onCreateView method:

ImageButton imgHandle = (ImageButton)rootView.findViewById(R.id.imageButton1);
bwegs
  • 3,769
  • 2
  • 30
  • 33
  • Great, I got this to work, sad though that xml files can't understand variables from their corresponding class. Suppose, for example, that I had a boolean variable in the class and if it was true, I would have a certain ImageButton in my fragment file, otherwise not. Is it possible to do that or would I just need to have a bunch of different fragment xml files? – Sid Jul 26 '14 at 15:48
  • One way you could handle this is to put the ImageButton in your xml file and if you decide you dont wont it to show you can get a reference to it in your class code and use the setVisibility function to the GONE constant so it doesn't appear – bwegs Jul 26 '14 at 16:13
  • Thanks for your help. I am trying to understand how to implement a complex layout system that changes depending on user input. For example: A game with levels, with each level using a differently sized chessboards and different chess pieces. What would be the professional way to go about turning that into an app - to use different layout files for each level, or to use methods like you suggest here? If I were to chose the latter option, I need a solution to this problem: how do I set "app:columnCount" in my GridLayout depending on an instance variable in the corresponding class file? – Sid Jul 26 '14 at 21:49
  • I haven't developed a game in Android so I'm probably not the best person to be giving advice on it, feel free to ask another question here on SO and I'm sure someone more qualified will be able to help. – bwegs Jul 26 '14 at 22:03
  • Here is my follow-up question: http://stackoverflow.com/questions/24976986/communicating-between-xml-file-and-corresponding-class-in-android-development – Sid Jul 27 '14 at 01:11