package com.example.namratha.tableview; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.widget.HorizontalScrollView; import android.widget.ScrollView; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; public class MainActivity extends AppCompatActivity { int startTime , endTime; int columnName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); String[] row = {"9", "10", "11", "12", "13", "14", "15" ,"16" , "17" , "18", "19" ,"20","21","22","23","24"}; String[] column = {"COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6" , "COLUMN7" , "COLUMN8" ,"COLUMN9" , "COLUMN10" , "COLUMN11"}; int rl = row.length; int cl = column.length; ScrollView sv = new ScrollView(this); TableLayout tableLayout = createTableLayout(row, column, rl, cl); HorizontalScrollView hsv = new HorizontalScrollView(this); hsv.addView(tableLayout); sv.addView(hsv); setContentView(sv); //Meeting time for colun 3 is from 9 to 11 /*startTime = 9; endTime = 11; int rowId = findRowID(startTime ,endTime);*/ //meeing time is at 12 for column 3; int rowIndex = findRowID(15); makeCellEmpty(tableLayout , rowIndex , 3); //meeting time is 9 to 11 startTime = 9; endTime =11; columnName =3; calculateTimeDiff(tableLayout, columnName ); startTime = 12; endTime =18; columnName = 5; calculateTimeDiff(tableLayout,columnName); } private void calculateTimeDiff(TableLayout tableLayout, int name) { for(int i = startTime;i<=endTime;i++) { int rowIndexSample = findRowID(i); makeCellEmpty(tableLayout, rowIndexSample,name); } } private int findRowID(int startTime) { int rowId = 0; if(startTime == 9) rowId = 1; else if(startTime == 10) rowId =2; else if(startTime == 11) rowId =3; else if(startTime == 12) rowId =4; else if(startTime == 13) rowId =5; else if(startTime == 14) rowId =6; else if(startTime == 15) rowId =7; else if(startTime == 16) rowId =8; else if(startTime == 17) rowId =9; else if(startTime == 18) rowId =10; else if(startTime == 19) rowId =11; else if(startTime == 20) rowId =12; else if(startTime == 21) rowId =13; else if(startTime == 22) rowId =14; else if(startTime == 23) rowId =15; else if(startTime == 24) rowId =16; return rowId; } public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) { // get row from table with rowIndex TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex); /* tableRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1.0f));*/ // get cell from row with columnIndex TextView textView = (TextView) tableRow.getChildAt(columnIndex); // make it black textView.setBackgroundColor(Color.BLACK); } public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex) { // get row from table with rowIndex TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex); // get cell from row with columnIndex TextView textView = (TextView) tableRow.getChildAt(columnIndex); textView.setText("Hello"); } private TableLayout createTableLayout(String[] rv, String[] cv, int rowCount, int columnCount) { // 1) Create a tableLayout and its params TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(); TableLayout tableLayout = new TableLayout(this); tableLayout.setBackgroundColor(Color.BLACK); // 2) create tableRow params TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(); tableRowParams.setMargins(1, 1, 1, 1); tableRowParams.weight = 1; int leftMargin=10; int topMargin=50; int rightMargin=10; int bottomMargin=50; // tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); for (int i = 0; i < rowCount; i++) { // 3) create tableRow TableRow tableRow = new TableRow(this); tableRow.setBackgroundColor(Color.GRAY); //tableRow.setBackground(getResources().getDrawable(R.drawable.border)); /* tableRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1.0f));*/ for (int j = 0; j < columnCount; j++) { // 4) create textView TextView textView = new TextView(this); // textView.setText(String.valueOf(j)); textView.setBackgroundColor(Color.WHITE); textView.setGravity(Gravity.CENTER); textView.setWidth(200); textView.setHeight(300); String s1 = Integer.toString(i); String s2 = Integer.toString(j); String s3 = s1 + s2; int id = Integer.parseInt(s3); Log.d("TAG", "-___>" + id); if (i == 0 && j == 0) { textView.setText("0==0"); } else if (i == 0) { Log.d("TAAG", "set Column Headers"); textView.setText(cv[j - 1]); } else if (j == 0) { Log.d("TAAG", "Set Row Headers"); textView.setText(rv[i - 1]); } else { // textView.setText("" + id); // check id=23 if (id == 23) { // textView.setText("ID=23"); } } // 5) add textView to tableRow tableRow.addView(textView, tableRowParams); } // 6) add tableRow to tableLayout tableLayout.addView(tableRow, tableLayoutParams); } return tableLayout; } }
Wednesday, 17 May 2017
Android TableLayout Dynamic Creation and filling cells
Subscribe to:
Post Comments (Atom)
Pass a HashMap from Angular Client to Spring boot API
This example is for the case where fileData is very huge and in json format let map = new Map<string, string>() map.set(this.ge...
-
package com.example.spinnerhint; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.app.Activi...
-
I faced this issue while implementing custom image button , here in xml following attribute is not supported android:background="?...
-
best link for android timers http://www.fampennings.nl/maarten/android/04timers/index.htm orientation best link Orientation Change h...
No comments:
Post a Comment