Advertisement
Guest User

Untitled

a guest
May 9th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. Adding data row dynamically
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:stretchColumns="1"
  8. android:id="@+id/SensorInfoTableLayout">
  9.  
  10.  
  11. <TableRow>
  12.  
  13. <TextView
  14. android:layout_column="1"
  15. android:text="@string/sensor_name_title"
  16. android:padding="3dip" />
  17.  
  18. <TextView
  19. android:layout_column="2"
  20. android:text="@string/sensor_type_title"
  21. android:padding="3dip" />
  22. <TextView
  23. android:layout_column="3"
  24. android:text="@string/sensor_value_title"
  25. android:padding="3dip" />
  26. <TextView
  27. android:layout_column="4"
  28. android:text="@string/sensor_unit_title"
  29. android:padding="3dip" />
  30. </TableRow>
  31.  
  32. <View
  33. android:layout_height="4dip"
  34. android:background="#FF909090" />
  35.  
  36. </TableLayout>
  37.  
  38. @Override
  39. public void onCreate(Bundle savedInstanceState)
  40. {
  41. super.onCreate(savedInstanceState);
  42.  
  43. getSensorInfo();
  44. setContentView(R.layout.sensorinfo);
  45.  
  46. setInfoByView();
  47.  
  48. }
  49.  
  50. private void setInfoByView()
  51. {
  52. /* Find Tablelayout defined in xml */
  53. TableLayout myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);
  54. /* Create a new row to be added. */
  55. TableRow myTableRow = new TableRow(this);
  56. myTableRow.setLayoutParams(new LayoutParams(
  57. LayoutParams.WRAP_CONTENT,
  58. LayoutParams.WRAP_CONTENT));
  59. /* Create a Button to be the row-content. */
  60. Button b = new Button(this);
  61. b.setText("Dynamic Button");
  62. b.setLayoutParams(new LayoutParams(
  63. LayoutParams.WRAP_CONTENT,
  64. LayoutParams.WRAP_CONTENT));
  65. myTableRow.setBackgroundColor(5);
  66. /* Add Button to row. */
  67. myTableRow.addView(b);
  68. /* Add row to TableLayout. */
  69. myTableLayout.addView(myTableRow,new TableLayout.LayoutParams(
  70. LayoutParams.WRAP_CONTENT,
  71. LayoutParams.WRAP_CONTENT));
  72. }
  73.  
  74. <?xml version="1.0" encoding="utf-8"?>
  75. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  76. android:orientation="vertical"
  77. android:layout_width="fill_parent"
  78. android:layout_height="fill_parent"
  79. android:background="#BFD6E8">
  80.  
  81. <ScrollView android:id="@+id/image_scroll"
  82. android:layout_width="fill_parent"
  83. android:layout_height="wrap_content">
  84.  
  85. <TableLayout android:id="@+id/image_table"
  86. android:layout_width="fill_parent"
  87. android:layout_height="wrap_content">
  88. </TableLayout>
  89.  
  90. </ScrollView>
  91.  
  92. </LinearLayout>
  93.  
  94. TableLayout image_table=null;
  95. image_table=(TableLayout)findViewById(R.id.image_table);
  96.  
  97. for(int i=0; i<10; i++){
  98. TableRow tableRow=new TableRow(this);
  99. tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  100. tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
  101. tableRow.setPadding(5, 5, 5, 5);
  102. for(int j=0; j<1; j++){
  103. Button bttn=new Button(this);
  104. bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  105. bttn.setText(Hello);
  106. tableRow.addView(image, 200, 200);
  107. }
  108. image_table.addView(tableRow);
  109. }
  110.  
  111. private void setInfoByView2()
  112. {
  113. TableLayout myTableLayout = null;
  114. TableRow myTableRow = new TableRow(this);
  115. myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);
  116.  
  117. myTableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  118. //myTableRow.setGravity(Gravity.CENTER_HORIZONTAL);
  119. myTableRow.setPadding(5, 5, 5, 5);
  120. Button bttn = new Button(this);
  121. bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  122. bttn.setText("Hello");
  123. myTableRow.addView(bttn, 200, 200);
  124. myTableLayout.addView(myTableRow);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement