0

I have a table layout and every row i have a two textview. i want to change the text color of textview when the row is selected. I Also use the selector xml in text color of textview but the color is not changing when the row is selected. Here is the xml

 <TableRow  

        android:id="@+id/row1"
        android:onClick="rowClick"
        android:focusable="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:background="@drawable/selector"  //this selector is use for row when its selected  

        >

       <LinearLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="@drawable/textline"
           android:orientation="horizontal" >

           <TextView
               android:id="@+id/username1" 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="User Name "
               android:textColor="@drawable/text_selector"       
               android:gravity="left|center"
               android:textSize="16dip"

                />

           <TextView
               android:id="@+id/userName"
               android:layout_width="86dp"
               android:layout_height="match_parent"
               android:layout_weight="0.27"
               android:gravity="right|center"
               android:freezesText="true"
               android:text=""
               android:textSize="16dip"
               android:textColor="#c4c0A3" />
       </LinearLayout>

        </TableRow>

Text_selector xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
    <item android:color="#000000" />
</selector>

Here Is the code

row1.setOnClickListener(new OnClickListener()
        {                                   
            public void onClick(View v) 
            {

final EditText m_objText  = (EditText) promptsView.findViewById(R.id.username_pref);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle(sTitle);
        alertDialogBuilder.setView(promptsView);

        // set dialog message
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface dialog, int id) 
                            {
                                objRowTextView.setText(m_objText.getText());
                                HideKeyboard();

                            }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface dialog, int id) 
                            {
                                HideKeyboard();
                                dialog.cancel();
                            }
                });
user3835770
  • 31
  • 12

1 Answers1

0

Add Text_selector xml in your project under res/color directory and then refer from the TextView as

android:textColor="@color/Text_selector"

Hope you can do same in code by setting different color for different action.

TextView text1 = (TextView) view.findViewById(R.id.textview);
text1.setOnTouchListener(new OnTouchListener() {
@Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                text1.setTextColor(Color.parseColor("#6DC066"));
                text1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                    break;
                case MotionEvent.ACTION_UP:
                    v.performClick();
                    break;}
                return true;
            }
        });

let me know anything apart from this.

Madhukar
  • 5
  • 6
  • the color is change when the text is selected but i want to change the text color when the row is selected just like a listview. when u select the list the text color is changed when the list is highlighted – user3835770 Feb 11 '15 at 06:34
  • Have you tried **setOnTouchListener** for table row?. In the ontouch event get the textview and change its color. – Madhukar Feb 11 '15 at 06:43
  • i use this function but the color is changed when the textview is selected – user3835770 Feb 11 '15 at 09:39
  • Please check the links :http://stackoverflow.com/questions/12065366/change-the-color-of-text-in-textview-in-table-row-on-row-click http://stackoverflow.com/questions/4468380/android-textview-change-text-color-on-click – Madhukar Feb 11 '15 at 13:05
  • yes the color is changed when the textview is selected but i want to select the row not the textview only – user3835770 Feb 12 '15 at 05:03
  • You haven't added table row/layout code. Please check with link http://stackoverflow.com/questions/4075356/how-can-i-highlight-the-table-row-on-click and http://stackoverflow.com/questions/11348703/select-a-tablerow-within-a-tablelayout-dynamically – Madhukar Feb 12 '15 at 05:14