how to change the color of textview


  1. rj87

    rj87 Member

    I have two xml files one is having the listview and another is having the layout of the listview vth some texviews, I want to change the color of the textview in the second xml file. This is what i have done so far.

    main1.xml:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:paddingTop="4px">    
    7.     <ListView droid:id="@android:id/list"
    8.         android:layout_width="fill_parent"
    9.         android:layout_height="fill_parent"
    10.         android:layout_below="@id/header"
    11.         android:layout_above="@+id/bottom_control_bar1"
    12.         android:layout_marginBottom="45dip"
    13.         android:drawSelectorOnTop="false"/>
    14.     </RelativeLayout>

    opp_main.xml:


    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="horizontal"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:padding="8px">
    7.  
    8.     <TextView
    9.         android:id="@+id/firstName"
    10.         android:layout_width="wrap_content"
    11.         android:layout_height="wrap_content"/>
    12.        
    13.     <TextView
    14.         android:id="@+id/lastName"
    15.         android:layout_marginLeft="6px"
    16.         android:layout_width="wrap_content"
    17.         android:layout_height="wrap_content"
    18.         android:layout_toRightOf="@id/firstName"
    19.      />
    20.  
    21.     <TextView
    22.         android:id="@+id/title"
    23.         android:layout_width="wrap_content"
    24.         android:layout_height="wrap_content"
    25.         android:layout_below="@id/firstName"
    26.         android:layout_toRightOf="@id/total"
    27.         android:layout_marginLeft="8px"
    28.         android:textColor="@color/closedDate"
    29.         />
    30.         </RelativeLayout>

    opp.java:


    Code (Text):
    1. public class OppMain extends ListActivity {
    2.     /** Called when the activity is first created. */
    3.     final String tag = "output";
    4.     static String Header = "Opportunities";
    5.     static String DBName, Date = null;
    6.     protected Cursor cursor;
    7.     protected ListAdapter adapter;
    8.     SQLiteDatabase db;
    9.    
    10.     @Override
    11.    
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.main1);
    15.      
    16.         TextView hdr = (TextView)findViewById(R.id.header);
    17.         hdr.setText(Header);
    18.  
    19.        
    20.       //database          
    21.        
    22.         db = (new openDataBaseAdapter(this)).getWritableDatabase();
    23.         cursor = db.query("userdata", new String []{"_id", "Name","Name2", "Date","title"},null, null, null, null,null);
    24.         adapter = new SimpleCursorAdapter(
    25.                 this,
    26.                 R.layout.opp_main,  
    27.                 cursor,
    28.                 new String[] {"Name", "Name2", "Date","title"},
    29.                 new int[] {R.id.firstName,R.id.total, R.id.title, R.id.accountname});
    30.                
    31.                 cursor.moveToFirst();
    32.                
    33.                
    34.                 while(cursor.isAfterLast() == false)
    35.                 {
    36.                    
    37.                     CloseDate = cursor.getString(cursor.getColumnIndex("Date"));
    38.                     if(CloseDate.equals("30/09/2011 00:00"))
    39.                     {
    40.                    
    41.                         TextView cl = (TextView)findViewById(R.id.title);
    42.                         Log.d(tag, "cl: " +cl);
    43.                         Log.d(tag, "Date11111: " +Date);
    44.                         cl.setTextColor(Color.CYAN);
    45.                     }
    46.                  
    47.                     cursor.moveToNext();
    48.                    
    49.                    
    50.                 }
    51.                 setListAdapter(adapter);
    52.                
    53.        }
    54.  
    55.  
    56.      }



    I wanted to change the color of textview date which is storing in `R.id.total`.
    here the log cat is giving null for the value cl.....and gives me nullpointer exception...:(
    can any one tell me please.......?
    thank you in advance...:)

    Advertisement
  2. TheCompBoy

    TheCompBoy Well-Known Member

    Try and see if this works:

    Code (Text):
    1. cl.setTextColor(this.getResources().getColor(R.color.orange));
  3. rj87

    rj87 Member

    Hello TheCompBoy,

    thank you for ur reply, I tried and it didnot work either...:( I printed the cl in logcat to see the id of cl but it is showing null.........:(
Loading...

Share This Page