I have a xml file which contains basic layout for each row of ListView(which is a realtive layout and has TextView inside it).
I want to change the attributes of this layout for each row of ListView like different layout width and height of each row. I want to set the values of width and height dynamically.
Is there any way around to do this?
My xml file which I want to change, height and weight dynamically, for each view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="@+id/item1"
android:layout_width="wrap_content">
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="text"
android:visibility="visible"
android:layout_width="wrap_content"
android:textColor="#FF200010"
android:background="#FFFCCCFF" />
</LinearLayout>
And my full file is
package com.test.list;
import java.util.ArrayList;
import java.util.TreeSet;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
public Context context =getApplicationContext();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 1; i < 50; i++) {
mAdapter.addItem("item " + i);
if (i % 4 == 0) {
mAdapter.addSeparatorItem("separator " + i);
}
}
setListAdapter(mAdapter);
}
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1,null);
Log.e("Ronak", "Here1");
TextView t= (TextView)convertView.findViewById(R.id.text);
t.setWidth(100);
t.setHeight(600);
t.setText("This is first type of view");
holder.textView = (TextView)convertView.findViewById(R.id.text);
Log.e("Ronak","reached here3");
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item1, null);
TextView t2= (TextView)convertView.findViewById(R.id.text);
t2.setWidth(200);
t2.setHeight(500);
t2.setText("This is second type of view");
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
wrap_content
and change thewith
andheight
of the text view dynamically – RoxannaroxannefindViewById
. First you have to do theconvertView = mInflater.inflate(R.layout.item2,null);
to inflate the xml structure into yourconvertView
and you should usefindViewById
on that viewTextView t2= (TextView)convertView.findViewById(R.id.text);
– Roxannaroxannepublic Context context =getApplicationContext();
you are not using it anyway. And take care when you choose to initialize a field like that. – Roxannaroxanne