I have an xml file (option_element.xml) that contains ImageView and TextView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="-18dp" > <ImageView android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/option_button" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignRight="@+id/button" android:layout_alignTop="@+id/button" android:layout_centerHorizontal="true" android:gravity="center" /> </RelativeLayout>
I have to populate LinearLayout with this view based on the contents of the array
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:id="@+id/options_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" /> </LinearLayout>
I add it as
LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list); String[] options = getActivity().getResources().getStringArray(R.array.options); for (int i = 0; i < options.length; i++) { View to_add = inflater.inflate(R.layout.options_element, options_layout); TextView text = (TextView) to_add.findViewById(R.id.text); text.setText(options[i]); text.setTypeface(FontSelector.getBold(getActivity())); }
But something went wrong. I expect options.length ImageView with a relative TextView populated with [i] text options, but I get options.length ImageView, but only the first one has text (and the text is not a parameter [0], but the last one).
For example, if the option contains {"one", "two", "three"} inside the first image, I get "three" and the others are empty. How to put each line inside each textview?
source share