1. Are you ready for the Galaxy S20? Here is everything we know so far!

Apps Android width of textview in listview

Discussion in 'Android Development' started by erdomester, Feb 12, 2011.

  1. erdomester

    erdomester Newbie
    Thread Starter

    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 (Text):
    1.  
    2. <ListView android:id="@+id/ListView01"
    3.     android:layout_width="wrap_content"
    4.     android:layout_height="wrap_content"
    5.     android:layout_below="@+id/linlay">
    6. </ListView>
    7.  
    In the ListView i created two TextViews. For this i am using another xml, called row.xml:
    Code (Text):
    1.  
    2. <LinearLayout
    3.      xmlns:android="http://schemas.android.com/apk/res/android"
    4.      android:paddingTop="4dip"
    5.      android:paddingBottom="6dip"
    6.      android:layout_width="fill_parent"
    7.      android:layout_height="wrap_content"
    8.      android:orientation="horizontal"
    9.      android:id="@+id/linlay0">
    10.  <LinearLayout android:orientation="horizontal"
    11.     android:id="@+id/linlay"
    12.     android:layout_width="fill_parent"
    13.     android:layout_height="fill_parent">
    14.      <TextView android:id="@+id/left"
    15.          android:layout_width="160px"
    16.          android:layout_height="wrap_content"
    17.          android:textColor="#FF0000"
    18.          android:paddingLeft="5px"/>   
    19.      <TextView android:id="@+id/right"
    20.          android:layout_width="140px"
    21.          android:layout_height="wrap_content"
    22.          android:maxWidth="140px"
    23.          android:singleLine="false"/>
    24.   </LinearLayout>
    25. </LinearLayout>
    26.  
    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 (Text):
    1.  
    2. txt1 = (TextView) findViewById(R.id.left);
    3. txt2 = (TextView) findViewById(R.id.right);
    4.  
    5. Button sortdate = (Button)findViewById(R.id.Button01);
    6.       sortdate.setOnClickListener(new View.OnClickListener()
    7.       {
    8.         public void onClick(View v)
    9.         {
    10.             lv1.setAdapter(simpleAdapter);
    11.             //here 
    12.         }
    13.       });
    14.  
    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 (Text):
    1. txt1 = (TextView) findViewById(R.id.left);
    is wrong as it makes Android look for the TextView in config.xml.

    Any ideas?
     

    Advertisement

  2. hellow

    hellow Newbie

    I am not sure I understand your question....

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

    Something like:

    Code (Text):
    1.  
    2.  
    3. public class CustomAdapter extends ArrayAdapter<CustomObj>{
    4.         private Context context;
    5.     private ArrayList<Budget> items;
    6.     private LayoutInflater vi;
    7.    
    8.  
    9.     public CustomAdapter(Context context, int textViewResourceId,  ArrayList<CustomObj> items) {
    10.         super(context, textViewResourceId, items);
    11.         this.context = context;
    12.         this.items = items;
    13.         vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    14.        
    15.     }
    16.  
    17.    @Override
    18.     public View getView(int i, View convertView, ViewGroup parent) {
    19.        //expand view...etc 
    20.  
    21.        View v = convertView;
    22.    
    23.     CustomObj obj = items.get(i);
    24.         v = vi.inflate(R.layout.row, null);
    25.  
    26.        TextView tv = (TextView) v.findViewById(R.id.textview1);
    27.        tv.setWidth(obj.getWidth())
    28.  
    29.     }
    30. }
    31.  
    32.  

    and then set the width in that customObj on button click in your activity perhaps?
     
Loading...
Similar Threads - Android width textview
  1. Cyberdev
    Replies:
    3
    Views:
    74
  2. Guitarist
    Replies:
    0
    Views:
    57
  3. Sarat chandra Poluri
    Replies:
    1
    Views:
    138
  4. markdoc
    Replies:
    6
    Views:
    125
  5. Rob
    Replies:
    11
    Views:
    232
  6. Kamilek123
    Replies:
    2
    Views:
    336
  7. Android News
    Replies:
    0
    Views:
    122
  8. Android News
    Replies:
    0
    Views:
    112
  9. Android News
    Replies:
    0
    Views:
    202
  10. Android News
    Replies:
    0
    Views:
    94

Share This Page

Loading...