I have a view, inflated from custom layout, which contains a TextView.
I tried to set TextView text both from xml via string resource and programmatically after layout inflating – I’m still getting only android.widget.RelativeLayout@4166f2d8 instead of normal text. What am I doing wrong?
Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layers_list_item_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<CheckBox
android:id="@+id/layers_list_item_switch"
blablabla... />
<TextView
android:id="@+id/layers_list_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_toLeftOf="@id/layers_list_item_switch"
android:selectAllOnFocus="true"
android:text="@string/layers_list_default_text"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:scrollHorizontally="true"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold"
android:typeface="serif"
android:clickable="true" />
</RelativeLayout>
View inflating method:
public View createLayerListItem (String text) {
View v = View.inflate(this, R.layout.layers_list_item, null);
TextView text_view = (TextView) v.findViewById(R.id.layers_list_item_text);
text_view.setText(text);
return v;
}
And this views are used to fill some ListView:
ListView layers_list = (ListView)layers_dialog.findViewById(R.id.layers_list);
ArrayAdapter<View> adapter = new ArrayAdapter<View>(this, R.layout.layers_list_item, R.id.layers_list_item_text);
adapter.add(createLayerListItem("Test 1"));
adapter.add(createLayerListItem(""));
adapter.add(createLayerListItem("Very very very very very very long line"));
All my TextViews are wrong, showing android.widget.RelativeLayout@4166f2d8. I also tried to use default text from xml, not changing it from code at all – still I didn’t got my text from resource, only address.
Array adapter convert your views to string array because of this, it set textview text as its class name. So you should use like below,