android - Possible to alternate row colors in textview? -


what have done created 1 button 3 textview(tablelayout) , making looks record display. however, encountered problem want alternate row colors , found out can listview. there way can without listview 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 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);                 }             });              (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 = ""; } 

is there way can without listview current code?

use tablelayout , use getchildat set alternate row colors tablerow :

tablelayout tablelayout=(tablelayout) findviewbyid(r.id. tlgridtable); (int count = 0; count < tablelayout.getchildcount(); count++) {   view view=tablelayout.getchildat(count);   // access textviews 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 textview's      viewtext1.setbackgroundcolor(color.black);      }else{      view.setbackgroundcolor(color.green);        // set color textview's      viewtext1.setbackgroundcolor(color.gray);     } } 

and can access textview5, textview6, textview7 using view set different colors textview's in each row.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -