android - Possible to alternate row colors in textview?


Keywords:android 


Question: 

What i have done is i created 1 button with 3 textview(tablelayout) and making it looks like a record display. However, i encountered a problem is that i want to alternate row colors and i found out that i can only do it with listview. Is there any way that i can do it without listview with my current code?

SortName(Button)

Date(TextView)    Name(TextView)  URL(TextView)
12/01/2015        Google          www.google.com
02/11/2015        Yahoo           www.yahoo.com

activity_record.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#b7010101">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3"
    android:id="@+id/linearLayout">
<Button
    android:id="@+id/SortName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sort By Name" />
</LinearLayout>
<ScrollView
    android:id="@+id/layout"
    android:layout_height="match_parent"
    android:scrollbars="horizontal|vertical"
    android:layout_width="match_parent"
    android:layout_marginTop="3dip"
    android:scrollbarStyle="outsideInset"
    android:fillViewport="true">
<HorizontalScrollView
    android:id="@+id/horizontalView"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal|vertical"
    android:layout_width="wrap_content"
    android:layout_marginTop="3dip">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tlGridTable" >
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <TextView
                android:id="@+id/textView4" android:text="Date"
                android:layout_weight="1" android:background="#cac9c9"
                android:textColor="#000000"
                android:padding="10dip" android:gravity="center"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/textView2" android:text="Name"
                android:layout_weight="1" android:background="#d3d3d3"
                android:textColor="#000000"
                android:padding="10dip" android:gravity="center"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/textView3" android:text="URL"
                android:layout_weight="1" android:background="#cac9c9"
                android:textColor="#000000"
                android:padding="10dip" android:gravity="center"
                android:textStyle="bold" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
    <TextView
                android:id="@+id/textView5"
                android:layout_weight="1" android:background="@drawable/back"
                android:textColor="#000000"
                android:padding="6dip" android:gravity="left"/>

            <TextView
                android:id="@+id/textView6"
                android:layout_weight="1"       android:background="@drawable/back"
                android:textColor="#000000"
                android:padding="6dip" android:gravity="left"/>

            <TextView
                android:id="@+id/textView7"
                android:layout_weight="1" android:background="@drawable/back"
                android:textColor="#000000"
                android:padding="6dip" android:gravity="left"/>

        </TableRow>
    </TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>

Record.java

public class Record extends AppCompatActivity {
Button SortName;
TextView view,view2,view3;
ArrayList<HistoryDetails> userList1 = new ArrayList<>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    view = (TextView) findViewById(R.id.textView5);
    view2 = (TextView) findViewById(R.id.textView6);
    view3 = (TextView) findViewById(R.id.textView7);

    SortName = (Button)findViewById(R.id.SortName);

    SortName.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            view.setText("");
            view2.setText("");
            view3.setText("");

            Collections.sort(userList1, new Comparator<HistoryDetails>() {
                @Override
                public int compare(HistoryDetails first, HistoryDetails second) {
                    return first.name.compareToIgnoreCase(second.name);
                }
            });

            for (HistoryDetails obj : userList1) {
                view.append(getDate(obj.date, "dd/MM/yyyy   HH:mm:ss") + "\n");

                if (obj.name.length() > 30)
                    view2.append(obj.name.substring(0, 30) + "\n");
                else
                    view2.append(obj.name + "\n");

                if (obj.url.length() > 30)
                    view3.append(obj.url.substring(0, 30) + "\n");
                else
                    view3.append(obj.url + "\n");
            }
        }
    });
}

HistoryDetails.java

public class HistoryDetails {
    public HistoryDetails(Long date, String name, String url) 
      {
        this.date = date;
        this.name = name;
        this.url = url;
      }
    public long date = 0;
    public String name = "";
    public String url = "";
}

2 Answers: 

Is there any way that i can do it without listview with my current code?

Use TableLayout and use getChildAt to set alternate row colors for TableRow :

TableLayout tableLayout=(TableLayout) findViewById(R.id. tlGridTable);
for (int count = 0; count < tableLayout.getChildCount(); count++) {
  View view=tableLayout.getChildAt(count);
  // access TextViews from Row
  View viewText1 = view.getChildAt(0);
  View viewText2 = view.getChildAt(1);
  // access others in same way...
  if (count % 2 == 1) {
     view.setBackgroundColor(Color.BLUE);  
     // set color for TextView's
     viewText1.setBackgroundColor(Color.BLACK); 

   }else{
     view.setBackgroundColor(Color.GREEN);  

    // set color for TextView's
     viewText1.setBackgroundColor(Color.GRAY); 
   }
}

and we can also access textView5, textView6, textView7 using view to set different colors for TextView's in each row.

 

It is easy having an array of TextView

int anternate=0;
for(int i=0; i<lenght; i++)
{
if(alternate==1) textview[i].setBackgroundColor(R.color.GRAY);//choose your color
else textview[i].setBackgroundColor(0x00000000);//transparent
alternate++;
alternate=alternate & 1;
}