I have a LinearLayout that contains a TextView and will always be. There will always be at least one button below the TextView, but under certain circumstances there may be several.
I can successfully create and add as many buttons as I need programmatically. I can also successfully configure any appearance-related parameters / parameters that I require for these buttons programmatically.
The problem is that I donβt know how to tell a programmatically created button that it should use an XML resource file that contains the appearance and layout options, instead of setting those options programmatically.
I looked at similar questions and spent time using the API itself, but to no avail.
Edit:
Here's an approximation of what I'm trying to do, we hope to make the explanations a bit clearer for me:
private TextView textView; private SomeObject someObject; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View scrollView = inflater.inflate(R.layout.fragment_play_game, container, false); textView = (TextView) scrollView.findViewById(R.id.game_data_text); textView.setText(someObject.getTextForTextView()); LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.game_data_container); for (String optionText : someObject.getTextForButtons()) { layout.addView(createOptionButton(optionText, layout)); } return scrollView; } private View createOptionButton(String optionText, LinearLayout layout) { Button optionButton = new Button(this.getActivity());
My layout XML file for the fragment looks like this (this is LinearLayout, to which I am trying to add buttons):
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/game_data_container" etc... > <TextView android:id="@+id/game_data_text" etc... /> </LinearLayout> </ScrollView>
Also, if I have to create an XML layout file for the button (let's call it custom_button.xml), if it looks something like this:
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/play_game_option_button" etc... />
Update:
Just to talk a bit about what MrFox @ says, what I did to get it working is to replace this line:
Button optionButton = new Button(this.getActivity());
with this:
Button optionButton = (Button) inflater.inflate(R.layout.play_game_option_button, layout, false);
... which inflates an xml file containing only the button layout (button template). In this case, it returns the root view of this file, which is only a button, since there is no parent in the file above the button.
However, if I set the last boolean (attachToParent) value to true, it would return the root container in which the button will reside (which is just the "layout" variable that was passed to the call).
Now I can create as many buttons as I want using this template.