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);
}


}
}



}

Activity Navigation in Android

 In the following posts, we can see how to navigate to the other activity in android. In the Below, Example is the addUserBtn is the Button created, on clicking this button this has to navigate to the Activity Called the Registration Activity. 

Button addUserBtn;
addUserBtn = (Button) findViewById(R.id.add_user_btn);
addUserBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openNewActivity(Registration.class);
}
});

Passing Data Between Activity in Android

 Use of the Intent We can Pass the Data between the Activity in the android application below the sample code to set the data in the Intent.


Consider the Below Activity Named Main Activity where the value is set and in another activity called DisplayMessageActivity we are displaying it. Blocked is the code that needs to be set.


1. MainActivity 


package com.example.myfirstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class MainActivity extends AppCompatActivity {

    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

   /** Called when the user taps the Send button */

    public void sendMessage(View view) {

        Intent intent = new Intent(this, DisplayMessageActivity.class);

        EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);

        String message = editText.getText().toString();

        intent.putExtra(EXTRA_MESSAGE, message);

        startActivity(intent);

    }

}


2. DisplayMessageActivity  


package com.example.myfirstapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_display_message);

        // Get the Intent that started this activity and extract the string

        Intent intent = getIntent();

        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text

        TextView textView = findViewById(R.id.textView);

        textView.setText(message);

    }

}

Creating My First Android Application

In order to start with your first android app, download the android studio IDE and install the required features. It’s a long Process and downloads a lot of google library and once it downloads all the library then it ready for the development environment.  


  1. 1. Install the Latest version of Android Studio. 

  1. 2. In the Welcome to Android Studio window, click Create New Project. 

  1. 3. In the Select a Project Template window, select Empty Activity and click Next. 

  1. 4. In the Configure your project window, complete the following: 

  • Enter "My First App" in the Name field. 

  • Enter "com.example.myfirstapp" in the Package name field. 

  • If you'd like to place the project in a different folder, change its Save location. 

  • Select either Java or Kotlin from the Language drop-down menu. 

  • Select the lowest version of Android you want your app to support in the Minimum SDK field. Based on this the application is compatible with the versions.  

  • If your app will require legacy library support, mark the Use legacy android.support libraries checkbox. 

  • Leave the other options as they are. 

  1. 5. Click Finish. 


You can Alternatively check out the following project from Github and get started with it.