Saturday, 29 May 2021

Displaying the Table View In Android

 The Following Activity defines the Table View of the Patient BP Data.





package com.atozfit.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.atozfit.R;
import com.atozfit.Service.AtoZBPService;
import com.atozfit.main.AtoZBPAttributes;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class DisplayBP_Results extends AppCompatActivity {

private static Context instance;

ProgressDialog mProgressBar;

private TableLayout mTableLayout;

TextView systolicTxt =null;

TextView diastolicTxt=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the table
setContentView(R.layout.activity_display_b_p__results);
mProgressBar = new ProgressDialog(this);
mTableLayout = (TableLayout) findViewById(R.id.tableInvoices);
mTableLayout.setStretchAllColumns(true);
startLoadData();
diastolicTxt = (TextView)findViewById(R.id.Diastolic_txt);
getSupportActionBar().setDisplayShowTitleEnabled(false);

}

public void startLoadData() {
mProgressBar.setCancelable(false);
mProgressBar.setMessage("Fetching Readings..");
mProgressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressBar.show();
new LoadDataTask().execute(0);

}




//////////////////////////////////////////////////////////////////////////////

//
// The params are dummy and not used
//
class LoadDataTask extends AsyncTask<Integer, Integer, String> {
@Override
protected String doInBackground(Integer... params) {

try {
Thread.sleep(2000);

} catch (InterruptedException e) {
e.printStackTrace();
}

return "Task Completed.";
}
@Override
protected void onPostExecute(String result) {
mProgressBar.hide();
loadData();
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {

}
}



public void loadData() {
int systolicMean=0;
int diastolicMean=0;
int leftRowMargin=0;
int topRowMargin=0;
int rightRowMargin=0;
int bottomRowMargin = 0;
int textSize = 0, smallTextSize =0, mediumTextSize = 0;

textSize = (int) getResources().getDimension(R.dimen.font_size_verysmall);
smallTextSize = (int) getResources().getDimension(R.dimen.font_size_small);
mediumTextSize = (int) getResources().getDimension(R.dimen.font_size_medium);

AtoZBPService bpService = new AtoZBPService();
List<AtoZBPAttributes> data = bpService.retrieveBPData();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM HH:mm, yyyy",Locale.ENGLISH);


int rows = data.size();
getSupportActionBar().setTitle("Readings (" + String.valueOf(rows) + ")");
TextView textSpacer = null;

mTableLayout.removeAllViews();

// -1 means heading row
for(int i = -1; i < rows; i ++) {
AtoZBPAttributes row = null;
if (i > -1)
row = data.get(i);
else {
textSpacer = new TextView(this);
textSpacer.setText("");

}
// data columns
final TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));

tv.setGravity(Gravity.LEFT);

tv.setPadding(5, 15, 0, 15);
if (i == -1) {
tv.setText("Sno");
tv.setBackgroundColor(Color.parseColor("#f0f0f0"));
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv.setText(String.valueOf(i+1));
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

final TextView tv2 = new TextView(this);
if (i == -1) {
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.MATCH_PARENT));
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

tv2.setGravity(Gravity.LEFT);

tv2.setPadding(5, 15, 0, 15);
if (i == -1) {
tv2.setText("Date");
tv2.setBackgroundColor(Color.parseColor("#f7f7f7"));
}else {
tv2.setBackgroundColor(Color.parseColor("#ffffff"));
tv2.setTextColor(Color.parseColor("#000000"));
tv2.setText(row.getDate());
}


final LinearLayout layCustomer = new LinearLayout(this);
layCustomer.setOrientation(LinearLayout.VERTICAL);
layCustomer.setPadding(0, 10, 0, 10);
layCustomer.setBackgroundColor(Color.parseColor("#f8f8f8"));

final TextView tv3 = new TextView(this);
if (i == -1) {
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv3.setPadding(5, 5, 0, 5);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv3.setPadding(5, 0, 0, 5);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

tv3.setGravity(Gravity.TOP);

final LinearLayout layAmounts = new LinearLayout(this);
layAmounts.setOrientation(LinearLayout.VERTICAL);
layAmounts.setGravity(Gravity.RIGHT);
layAmounts.setPadding(0, 10, 0, 10);
layAmounts.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));



final TextView tv4 = new TextView(this);
if (i == -1) {
tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv4.setPadding(5, 5, 1, 5);
layAmounts.setBackgroundColor(Color.parseColor("#f7f7f7"));
} else {
tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setPadding(5, 0, 1, 5);
layAmounts.setBackgroundColor(Color.parseColor("#ffffff"));
}

tv4.setGravity(Gravity.RIGHT);

if (i == -1) {
tv4.setText("Systolic");
tv4.setBackgroundColor(Color.parseColor("#f7f7f7"));
tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv4.setBackgroundColor(Color.parseColor("#ffffff"));
tv4.setTextColor(Color.parseColor("#000000"));
tv4.setText(row.getSystolic());
tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
systolicMean+=Integer.parseInt(row.getSystolic());
}

layAmounts.addView(tv4);


final LinearLayout layDiastolic = new LinearLayout(this);
layDiastolic.setOrientation(LinearLayout.VERTICAL);
layDiastolic.setGravity(Gravity.RIGHT);
layDiastolic.setPadding(0, 10, 0, 10);
layDiastolic.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));



final TextView tv5 = new TextView(this);
if (i == -1) {
tv5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv5.setPadding(5, 5, 1, 5);
layAmounts.setBackgroundColor(Color.parseColor("#f7f7f7"));
} else {
tv5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv5.setPadding(5, 0, 1, 5);
layDiastolic.setBackgroundColor(Color.parseColor("#ffffff"));
}

tv5.setGravity(Gravity.RIGHT);

if (i == -1) {
tv5.setText("Diastolic");
tv5.setBackgroundColor(Color.parseColor("#f7f7f7"));
tv5.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv5.setBackgroundColor(Color.parseColor("#ffffff"));
tv5.setTextColor(Color.parseColor("#000000"));
tv5.setText(row.getDiastolic());
tv5.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
diastolicMean+=Integer.parseInt(row.getDiastolic());
}

layDiastolic.addView(tv5);
LinearLayout layMean=null;
if(i+1==rows){
layMean = new LinearLayout(this);
layMean.setOrientation(LinearLayout.VERTICAL);
layMean.setGravity(Gravity.CENTER_VERTICAL);
layMean.setPadding(0, 0, 0, 0);
layMean.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.MATCH_PARENT));

final TextView tv6 = new TextView(this);

tv6.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv6.setPadding(0, 0, 1, 5);
layMean.setBackgroundColor(Color.parseColor("#ffffff"));
int sysMean=systolicMean/rows;
int diasMean=diastolicMean/rows;
tv6.setText("BP:"+sysMean+"/"+diasMean+"mmHg");
tv6.setGravity(Gravity.RIGHT);
layMean.addView(tv6);
}





// add table row
final TableRow tr = new TableRow(this);
tr.setId(i + 1);
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
tr.setPadding(0,0,0,0);
tr.setLayoutParams(trParams);






tr.addView(tv);
tr.addView(tv2);
tr.addView(layCustomer);
tr.addView(layAmounts);
tr.addView(layDiastolic);
TableRow tr1=null;
TableLayout.LayoutParams tr1Params=null;
if(i+1==rows){
tr1 = new TableRow(this);
tr.setId(rows + 1);
tr1Params = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tr1Params.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
tr1.setPadding(0,0,0,0);
tr1.setLayoutParams(trParams);
tr1.addView(layMean);
}



if (i > -1) {

tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TableRow tr = (TableRow) v;
//do whatever action is needed

}
});


}
mTableLayout.addView(tr, trParams);
if(i+1==rows){
mTableLayout.addView(tr1, tr1Params);
}
if (i > -1) {

// add separator row
final TableRow trSep = new TableRow(this);
TableLayout.LayoutParams trParamsSep = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParamsSep.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);

trSep.setLayoutParams(trParamsSep);
TextView tvSep = new TextView(this);
TableRow.LayoutParams tvSepLay = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
tvSepLay.span = 4;
tvSep.setLayoutParams(tvSepLay);
tvSep.setBackgroundColor(Color.parseColor("#d9d9d9"));
tvSep.setHeight(1);

trSep.addView(tvSep);
mTableLayout.addView(trSep, trParamsSep);
}


}
}



}

No comments:
Write comments