• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android width of textview in listview

Hi guys,

I've been looking for the solution of my problem for a while.
I have an app.

Config.xml has some buttons and a listview. This is the listview:
Code:
<ListView android:id="@+id/ListView01" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:layout_below="@+id/linlay">
</ListView>

In the ListView i created two TextViews. For this i am using another xml, called row.xml:
Code:
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:id="@+id/linlay0">
 <LinearLayout android:orientation="horizontal"
    android:id="@+id/linlay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <TextView android:id="@+id/left"
         android:layout_width="160px"
         android:layout_height="wrap_content"
         android:textColor="#FF0000"
         android:paddingLeft="5px"/>	
     <TextView android:id="@+id/right"
         android:layout_width="140px"
         android:layout_height="wrap_content"
         android:maxWidth="140px"
         android:singleLine="false"/>
  </LinearLayout>
</LinearLayout>

So these two TextViews are like columns in the ListView.

I manage config.xml in MainActivity.java: button listeners etc. In case of a button listener i want to change the width of one of the TextViews in the ListView, but i can't.

Code:
txt1 = (TextView) findViewById(R.id.left); 
txt2 = (TextView) findViewById(R.id.right);

Button sortdate = (Button)findViewById(R.id.Button01);
	  sortdate.setOnClickListener(new View.OnClickListener() 
      {
      	public void onClick(View v) 
      	{
      		lv1.setAdapter(simpleAdapter);
        	//here	
      	}
      });

I cannot use txt1.setWidth(60) as the TextView is in row.xml, while the button is in config.xml. I tried to do it by using RemoteViews, it didn't work, and i don't know if i can use RemoteViews here.
So basically the
Code:
txt1 = (TextView) findViewById(R.id.left);
is wrong as it makes Android look for the TextView in config.xml.

Any ideas?
 
I am not sure I understand your question....

have you tried creating custom adapter with custom object which holds the width?

Something like:

Code:
public class CustomAdapter extends ArrayAdapter<CustomObj>{
        private Context context;
	private ArrayList<Budget> items;
	private LayoutInflater vi;
	

    public CustomAdapter(Context context, int textViewResourceId,  ArrayList<CustomObj> items) {
        super(context, textViewResourceId, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
	}

   @Override
    public View getView(int i, View convertView, ViewGroup parent) {
       //expand view...etc	

       View v = convertView;
	
	CustomObj obj = items.get(i);
        v = vi.inflate(R.layout.row, null);

       TextView tv = (TextView) v.findViewById(R.id.textview1);
       tv.setWidth(obj.getWidth())

    }
}


and then set the width in that customObj on button click in your activity perhaps?
 
Back
Top Bottom