Amos Fox
Amos Fox

Reputation: 310

How to change properties of a view layout programmatically

I have a custom layout xml which i use as a template for creating items in a list dynamically. However I can't seem to change the text and color of elements within this custom layout properly before I add it to the main layout. I need to do this as each item in the list can be different.

If I have added more than one of these custom layouts to the main layout any changes I make to the TextView object always happen on the first item in the list.

My custom layout has a textview and a check box within a relative layout in a file called 'cat_panel.xml'.

My coding to produce the layout is:

LinearLayout rootEl = (LinearLayout) findViewById(R.id.pageWrapper);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View vw;

vw=inflater.inflate(R.layout.cat_panel, rootEl, false);
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
catTitleTv.setText("testing 1");
rootEl.addView(vw);

//the  above is then repeated
vw=inflater.inflate(R.layout.cat_panel, rootEl, false);
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
catTitleTv.setText("testing 2");
rootEl.addView(vw);

Thanks in advance

Upvotes: 0

Views: 712

Answers (1)

kris larson
kris larson

Reputation: 30985

Change

catTitleTv = (TextView) findViewById(R.id.catPanelTitle);

to

catTitleTv = (TextView) vw.findViewById(R.id.catPanelTitle);

Upvotes: 1

Related Questions