I'm trying to find a way to create a custom views dynamically, but I didn't find a useful post for my problem.
I have the next view.
And this is the tree of the layout:
I want to create as many copies as items received, but I need to change the textviews inside per each item.
I don't know If I have explained me well. Thank you.
EDIT:
I follow some of your advises, and I make this:
private void addInputHeader(int timestamp)
{
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
View mChildView = getLayoutInflater().inflate(R.layout.program_event_day_header, mRootLayout, false);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tv_day = (TextView) mChildView.findViewById(R.id.tv_day);
tv_day.setText(getDayForHeader(timestamp) + getResources().getString(R.string.of) + getMonthForHeader(timestamp));
}
private void addInputDescription(int timestamp, String description)
{
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
View mChildView = getLayoutInflater().inflate(R.layout.program_event_day_header, mRootLayout, false);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tv_event_hour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
TextView tv_event_message = (TextView) mChildView.findViewById(R.id.tv_event_message);
tv_event_hour.setText(getHour(timestamp) + " - ");
tv_event_message.setText(description);
}
private String getDayForHeader(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("cccc dd").format(df);
}
private String getMonthForHeader(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("MM").format(df);
}
private String getHour(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("HH:mm").format(df);
}
private int getD(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("dd").format(df));
}
private int getM(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("MM").format(df));
}
private int getY(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("yyyy").format(df));
}
But I'm getting a NullPointerException when the addInputDescription tries to set text on tv_event_hour.
// Add row view layout that represent ui row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/leftTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Top" />
<TextView
android:id="#+id/RightTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Top" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/leftBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Bottom" />
<TextView
android:id="#+id/RightBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Bottom" />
</LinearLayout>
</LinearLayout>
// Add view as row with msg
private void addMsg() {
View msgView = getViewWithMsg("TopLeft", "TopRight", "LeftBottom", "Right Bottom");
// msgView represent one row
// You can add it to linearlayout desendent to scrollview
}
// This method create one msg row layout and update msg
private View getViewWithMsg(String TopLeft, String TopRight, String LeftBottom,
String RightBottom) {
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View root = inflator.inflate(R.layout.row, null);
TextView topLeft = (TextView) root.findViewById(R.id.leftTop);
TextView topRight = (TextView) root.findViewById(R.id.RightTop);
TextView bottomLeft = (TextView) root.findViewById(R.id.leftBottom);
TextView botomRight = (TextView) root.findViewById(R.id.RightBottom);
topLeft.setText(TopLeft);
topRight.setText(TopRight);
bottomLeft.setText(LeftBottom);
botomRight.setText(RightBottom);
return root;
}
Create an VO for your items and add it in to the layout.
Like, layout.add(item)
See the following code if you're trying to add a child view (or multiple) to your root LinearLayout:
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.ll_root);
View mChildView = getLayoutInflater().inflate(R.layout.ll_child, mRootLayout,
false);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tvEventHour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
TextView tvEventMessage = (TextView) mChildView.findViewById(R.id.tv_event_message);
tvEventHour.setText("10:30");
tvEventMessage.setText("Fugia corum...");
Note: If the list is about to get very long you should consider using a ListView with an Adapter since this is alot better performance-wise.
You can inflate from xml:
View placeHolderView = inflater.inflate(R.layout.view_header_placeholder, mListView);
What do you mean by "change the textviews inside per each item."?
If you need the textview inside the inflated layout try this:
TextView myTextView = (TextView)placeHolderView.findViewById(R.id.my_text_view);
Related
I have a layout where I want to imitate a listview by adding items programmatically one below the other. So I created an .xml for the layout of these items that are something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_lista"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/rellay_btn"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Some name"
android:textColor="#3F3F3F"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/iv_follow"
android:text="Some text"
android:textColor="#3F3F3F"
android:textSize="13sp" />
</LinearLayout>
In the activity I would like to generate n of this layout, fill the textviews with text and also I would like to set an onClickListener on them. n is the size of an array. Please help
Note: Once the activity loads, the number of layouts will not change, nor can a layout be removed.
This is what I have now but the layout is displayed on top of the activity instead of below a Textview:
LayoutInflater inflater = LayoutInflater.from(BucketProfileActivity.this);
for (int i = 0; i < 3; i++) {
View view = inflater.inflate(R.layout.bucketprofile_tips, null);
view.setId(i);
TextView tv_username = (TextView) view.findViewById(R.id.tv_username);
tv_username.setText(String.valueOf(i));
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.BELOW, R.id.tv_nemkell);
addContentView(view, rl);
}
If your posted xml file is bucketprofile_tips.xml then you need to change here from
TextView tv_username = (TextView) view.findViewById(R.id.tv_title);
to
TextView tv_username = (TextView) view.findViewById(R.id.tv_username);
Because in bucketprofile_tips.xml tv_title id not found for your TextView. So you are getting Null Pointer Exception.
Not sure why you would want to do this but you can just use a LayoutInflater to inflate x of this view and add them to a designated ViewGroup. For example:
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < numberOfViews; i++) {
View v = inflater.inflate(R.layout.view_to_add, parentView, false);
setup(v);
parentView.addView(v);
}
Where setup() is a function you define that does the setup work such as setting the text of a TextView etc.
For instance, in your setup function might look something like this:
private void setup(View v) {
TextView tv = (TextView) v.findViewById(R.id.name);
// set the text of the text view
tv.setText("Hello!");
// set the click listener for the view...
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff here...
}
});
}
LinearLayout parentLayout=findViewById(R.id.parentLayout);
LayoutInflater inflater = LayoutInflater.from(BucketProfileActivity.this);
View view = inflater.inflate(R.layout.bucketprofile_tips, null);
parentLayout.addView(view);
LinearLayout textList=(LinearLayout) parentLayout.getChildAt(0);
for(int i=0; i<textList.getChildCount(); i++){
TextView tv_username=(TextView) textList.getChildAt(i);
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
tv_username.setText(String.valueOf(i));
}
}
}
Here is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
PlannerMonth m = getItem(position);
ArrayList<PlannerMonthDebt> debts = new ArrayList<PlannerMonthDebt>();
debts = m.debtList;
int debtSize = debts.size();
if (rowView == null) {
rowView = inflater.inflate(R.layout.row_planner_month, null, true);
holder = new ViewHolder();
holder.tv1 = (TextView) rowView.findViewById(R.id.tvPlannerMonth);
holder.tv2 = (TextView)
holder.ll = (LinearLayout) rowView.findViewById(R.id.rlInnerDebtHolder);
for (int i = 0; i < debtSize; i++) {
View custom = inflater.inflate(R.layout.inner_debt_row, null);
TextView tvName = (TextView) custom.findViewById(R.id.tvInnerDebtName);
tvName.setTag(String.valueOf("tvName" + i));
TextView tvPayment = (TextView) custom.findViewById(R.id.tvInnerDebtPayment);
tvPayment.setTag(String.valueOf("tvPayment" + i));
TextView tvDebt = (TextView) custom.findViewById(R.id.tvInnerDebtBalance);
tvDebt.setTag(String.valueOf("tvDebt" + i));
TextView tvBalance = (TextView) custom.findViewById(R.id.tvInnerDebtFee);
tvBalance.setTag(String.valueOf("tvBalance" + i));
holder.ll.addView(custom);
}
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
String month = m.date;
String debtToal = m.debtTotal;
String intTotal = m.intTotal;
holder.tv1.setText(month);
holder.tv2.setText(debtToal);
// now inner debts
int size = 0;
for (PlannerMonthDebt d : debts) {
TextView tvName = (TextView) convertView.findViewWithTag(String.valueOf("tvName" + size));
tvName.setText(d.name);
TextView tvPayment = (TextView) convertView.findViewWithTag(String.valueOf("tvPayment" + size));
tvPayment.setText(d.payment);
TextView tvDebt = (TextView) convertView.findViewWithTag(String.valueOf("tvDebt" + size));
tvDebt.setText(d.balance);
TextView tvBalance = (TextView) convertView.findViewWithTag(String.valueOf("tvBalance" + size));
tvBalance.setText(d.fees);
size++;
}
return rowView;
}
Here is the row I am trying to dynamically add:
inner_debt_row.xml:
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvInnerDebtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/tvInnerDebtFee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/tvInnerDebtPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/tvInnerDebtBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
I have a LinearLayout (holder.ll) where I add an X number of custom rows (based on the inner_debt_row layout pasted above).
I know how to show the rows, but I am having trouble using setTag() to find and retrieve them so I can put values to them (via setText().
For the tag, I basically use a same name for each TextView, but ALSO add a unique position to the name. I use the position (or size) to keep each of the dynamically added text fields unique for searching.
In my LogCat, I get a NullPointer on this line:
TextView tvName = (TextView) convertView.findViewWithTag(String.valueOf("tvName" + size));
This is the first encounter with assigning a value to a dynamic TextView so I assume they all can not be found.
How can I locate the correct TextView instance whit by tag?
The findViewWithTag() code looks correct. Did you check that convertView isn't null?
convertView will be null when creating a new row (i.e. not reusing a previous one), but you are assigning rowView only, and not convertView.
You have different debtSize in each position. When convertView != null and have less debtSize you have NullPointer's, when more - you have empty textviews.
This is Relative Layout view which is created dynamically like this :
...
So my question is how to get the values from those dynamic elements and perform some calculation to show result on textview which is also dynamically created.
Thanks in advance guys!!
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numberofPlayersTolayout = (Integer) Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
}
}
}
for (int i = 0; i < maalContainer.getChildCount(); i++) {
View view = maalContainer.getChildAt(i);
EditText ed_item = (EditText) view
.findViewById(R.id.edittext1);
EditText ed_value = (EditText) view
.findViewById(R.id.edittext2);
EditText ed_value1 = (EditText) view
.findViewById(R.id.edittext3);
ed_item.getText().toString().trim();
ed_value.getText().toString().trim();
ed_value1.getText().toString().trim();
}
No need to assign/get ID of any View but the best way is to iterate through the child views of LinearLayout:
int childcount = myLinearLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = myLinearLayout.getChildAt(i);
// do whatever you would want to do with this View
}
A few things here:
You shouldn't be using the Application Context to get the LayoutInflater. You should get that from the Activity Context, otherwise your themes and styles will not be respected.
You shouldn't inflate with null as the second parameter (except in special instances where you don't know the View's parent). Just put the android:layout_width="match_parent" and android:layout_height="wrap_content" attributes in player_entry_item.xml and they will be respected upon inflation.
Store the EditText objects upon inflation as a private array.
With that said, my suggested revision:
private EditText[] mEditTextPlayers;
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = LayoutInflater.from(view.getContext());
mEditTextPlayers = new EditText[numPlayers];
for (int i = 0; i < numPlayers; i++) {
//Pass the parent as the second parameter to retain layout attributes
mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
maalContainer.addView(dynamicEntryView);
}
}
public void goButtonClicked(View view) {
int numberofPlayersTolayout = Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
maalContainer.removeAllViews();
maalText = new EditText[numberofPlayersTolayout];
points = new EditText[numberofPlayersTolayout];
result = new TextView[numberofPlayersTolayout];
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
playerName.setText("Player :" + (i + 1));
maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}
You can do it by using getId() and setId() methods of the View.
dynamicEntryView.setId(i);
and access the view as below by getting the
dynamicEntryView.getId(i);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/player_name_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:text="Player name"
android:textColor="#202020"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/player_item_edittext_point"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Points"
android:inputType="number"
android:textColor="#202020" />
<EditText
android:id="#+id/player_item_edittext_maal"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Maal"
android:inputType="number"
android:textColor="#202020" />
<TextView
android:id="#+id/player_item_textview_result"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00$"
android:textSize="22sp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
Only one id is used. It will loop through according to number of players. Hope it will help you. #user2731584
I add a ListView on runtime like this:
MainMenue = getResources().getStringArray(R.array.Unit);
// remove all controls
LinearLayout formLayout = (LinearLayout)findViewById(R.id.submenue);
formLayout.removeAllViews();
menueview = new ListView(getApplicationContext());
menueview.setVisibility(ListView.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
menueview.setLayoutParams(params);
menueview.setAdapter(new submenueadapter(menueview.getContext(), MainMenue));
// Set the on Item
SetMenueOnClick() ;
formLayout.addView(menueview);
and then I add a item click listener like this:
public void SetMenueOnClick() {
menueview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String text = (String) ((TextView)view).getText();
}
});
}
But then I have an error:
06-03 10:59:25.862: E/AndroidRuntime(14732): at android.view.ViewRoot.handleMessage(ViewRoot.java:2109)
android.widget.LinearLayout cannot be cast to android.widget.TextView
at this line:
final String text = (String) ((TextView)view).getText();
Any idea how to get the text in this issue? the adapter looks like this:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.shortmenue, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.contents);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
rowView.setBackgroundResource(R.drawable.alternate_list_color);
return rowView;
}
and R.layout.shortmenue is simple, only a TextView like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contents"
android:textSize="34dp"
/>
</LinearLayout>
Your row is a TextView wrapped by a LinearLayout so you might want to do this:
LinearLayout ll = (LinearLayout) view; // get the parent layout view
TextView tv = (TextView) ll.findViewById(R.id.contents); // get the child text view
final String text = tv.getText().toString();
If you are running on a similar problem but you sure you have targeted a linearLayout:
Delete the file gen/your.app.package/R.java.
This happens because of a xml bug, when you delete R.java it will be recreated on the next build/run.
I just added android:id="#+id/my_layout" to LinearLayout that wrapped TextView and that solved similar problem.
I had the same problem and I just renamed inserted view object.
Like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newTextViewName"
android:textSize="34dp"
/>
In my case an XML contained a TextView, but I made a mistake writing
final TextView text = (TextView) inflater.inflate(R.layout.item_text, layout);
instead of
final TextView text = (TextView) inflater.inflate(R.layout.item_text, layout, false);
You can resolve this by replacing
final String text = (String) ((TextView)view).getText();
with
TextView temp= (TextView) view.findViewById(R.id.textView);
This works for me.
i have a complex xml layout which has list views..a row in the list view contains several text fields which are spaced evenly. i am using textview to store the text and then finally add all the items to the row...its working perfectly fine.
but now i have case where in i am not sure, how many text fields i might get from a webservice. therefore i need to create the textview dynamically on run time, populate them and then insert into the list..
is there anyway to declare,add and populate new textview fields on runtime?
or is there is anyway to implement the spacing between the two fields?
result of first call
__________________________
|_____|_____|_____|______|
result of second call
________________________________
|_____|_____|_____|______|______|
I tried implementing the solution that was provided below (Kenny), but for some reason I am unable to add views into the list.. below is my code
public class HeaderAdapter extends ArrayAdapter<Header> {
final Header[] listSymbols;
private TextView textView;
private LinearLayout row;
public HeaderAdapter(Context context, int symResourceID,
Header[] objects) {
super(context, symResourceID, objects);
listSymbols = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.header_view, parent, false);
Header headerRec = listSymbols[position];
for(int i = 0 ; i < listSymbols.length;i++){
textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
textView.setId(i);
row.add??
}
}
The main activity that calls this
setContentView(R.layout.main);
headerList.add(new Header("Symbol","Quantity","Price","Price Yesterday","52 Week High","52 Week Low","Change","Days Gain","Days Gain %","Returns"));
Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]);
ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList);
headerListView.setAdapter(headerAdapter);
xml layout file..the main file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scrollbars="none"
android:id="#+id/headerHv">
<ListView android:id="#+id/header_listView1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#color/white" android:cacheColorHint="#00000000"
android:smoothScrollbar="true" android:scrollbars="none" />
</HorizontalScrollView>
</LinearLayout>
the file in which the template for the row is defined
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="#+id/headerList" android:layout_width="80dp"
android:layout_height="wrap_content" android:textColor="#000000"
android:typeface="sans" android:textStyle="normal" />
</LinearLayout>
Here is the way i dynamically generate custom buttons from a list, you could do the same thing with textViews:
//Setup Buttons
layout = (LinearLayout)findViewById(R.id.layoutBars);
int count = lBars.size();
for(int i = 0; i< count;i++){
final Bar b = lBars.get(i);
BarButton button = new BarButton(DDTBars.this, R.drawable.barsmall , b.getName().toUpperCase());
button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
button.setId(i);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Run activity passing name of the bar to retrieve data
Intent i = new Intent(DDTBars.this, DDTBar.class);
i.putExtra("name", b.getName());
startActivity(i);
}
});
layout.addView(button);
}
So you could try:
//Setup TextViews
layout = (LinearLayout)findViewById(R.id.mylayout);
int count = myTextList.size();
for(int i = 0; i< count;i++){
TextView txtView = new TextView(this);
txtView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
txtView.setId(i);
layout.addView(txtView);
}
You could do it in code. Declare TextView 's in a loop and use RelativeLayout to position them wrt each other.