I have the following code and i need to change a little bit the procedure. As it is it works and when i press b1 button returns me on text field et3 a calculation of 3 other text fields. I need to return the calculation on text field et3 without pressing the button. I think i need to add a watcher on text field et4 and when this is not empty then to return me the calculation on et3. The code is this
package com.example.b15_calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText et1, et2, et3, et4;
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);
et4 = (EditText) findViewById(R.id.editText4);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
//step3 : write add functionality.
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
}
});
//step3 : ΜΕΤΑΒΛΗΤΕΣ
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String f = et1.getText().toString();
double i = Double.parseDouble(f);
String s = et2.getText().toString();
double j = Double.parseDouble(s);
String w = et4.getText().toString();
double q = Double.parseDouble(w);
double price_gold = 10;
double fpa = 1.23;
double fpol = 0.10;
double fpolam = 999;
double isot = 100;
double sint_ker = 10;
double result1 = (i * price_gold) + (j * 1000) + (q * isot);
double result2 = result1 / 340.75;
int gap;
if (result2 >= fpolam){
double result = (result2 * fpol);
double result3 = ((result2 * sint_ker) * fpa) + result;
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
} else{
double result3 = ((result2 * sint_ker) * fpa);
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You can use TextWatcher
et4.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// make the calculation here and set the result to et3
String f = et1.getText().toString();
double i = Double.parseDouble(f);
String s = et2.getText().toString();
double j = Double.parseDouble(s);
String w = s.toString();
double q = Double.parseDouble(w);
double price_gold = 10;
double fpa = 1.23;
double fpol = 0.10;
double fpolam = 999;
double isot = 100;
double sint_ker = 10;
double result1 = (i * price_gold) + (j * 1000) + (q * isot);
double result2 = result1 / 340.75;
int gap;
if (result2 >= fpolam){
double result = (result2 * fpol);
double result3 = ((result2 * sint_ker) * fpa) + result;
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
} else{
double result3 = ((result2 * sint_ker) * fpa);
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
}
}
});
Related
num1 and num2 are input values that need to have long decimal points but when it parses the string the decimal gets stripped and it crashes. Any assistance would be appreciated.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBuyInButton = (Button) findViewById(R.id.addBuyInButton);
addBuyInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText feeNumEditText = findViewById(R.id.feeNumEditText);
EditText buyInNumEditText =
findViewById(R.id.buyInNumEditText);
TextView targetTextView = findViewById(R.id.targetTextView);
Blockquote right around here is where I run into problems...
int num1 =
Integer.parseInt(feeNumEditText.getText().toString());
int num2 =
Integer.parseInt(buyInNumEditText.getText().toString());
int num3 = 3;
int num4 = 100;
int num5 = num1 + num3;
int num6 = num5 / num4;
int num7 = num6 * num2;
int num8 = num7 + num2;
targetTextView.setText(num8 + "");
}
});
}
}
Had to use float and DecimalFormat to get desired effect.
String str2 = buyInNumEditText.getText().toString();
float num1 = Float.parseFloat(str1);
float num2 = Float.parseFloat(str2);
float num3 = 3;
float num4 = 100;
float num5 = num1 + num3;
float num6 = num5 / num4;
float num7 = num6 * num2;
float num8 = num7 + num2;
DecimalFormat formatter = new DecimalFormat("##.########");
String num9 = formatter.format(num8);
It works when I convert from Fahrenheit to Celsius but it doesn't work when I try to convert it back from Celsius into Fahrenheit. I've tried for so many times but it still doesn't work for me.
Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter_second);
rb_celcius = (RadioButton) findViewById(R.id.radioButton_celcius);
rb_fahrenheit = (RadioButton) findViewById(R.id.radioButton2_fahrenheit);
btn_convert = (Button) findViewById(R.id.button3_convert);
btn_clear = (Button) findViewById(R.id.button5_clear);
edTxt_amount = (EditText) findViewById(R.id.editText_amount);
txtVw_answer = (TextView) findViewById(R.id.textView7_answer);
btn_convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
if (rb_celcius.isChecked()) {
String strFah = edTxt_amount.getText().toString();
if (!strFah.isEmpty()) {
{
dblFahrenheit = Double.parseDouble(strFah);
}
if (dblFahrenheit <= 212) {
dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
txtVw_answer.setText("The answer is " + dblConvertedTemp);
} if (rb_fahrenheit.isChecked()) {
String strCel = edTxt_amount.getText().toString();
if (!strCel.isEmpty()) {
{
dblCelcius = Double.parseDouble(strCel);
}
if (dblCelcius <= 100) {
dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
txtVw_answer.setText("The answer is" + dblFahConversion);
}
}
else if (v == btn_clear){
rg_group.clearCheck();
txtVw_answer.setText("");
}
}
}
}
}
}
});
}
}
I can't say if the logic is correct, but from what I see, the braces are all wrong. This is also not the best way of writing this solution, I will leave that to you.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter_second);
rb_celcius = (RadioButton) findViewById(R.id.radioButton_celcius);
rb_fahrenheit = (RadioButton) findViewById(R.id.radioButton2_fahrenheit);
btn_convert = (Button) findViewById(R.id.button3_convert);
btn_clear = (Button) findViewById(R.id.button5_clear);
edTxt_amount = (EditText) findViewById(R.id.editText_amount);
txtVw_answer = (TextView) findViewById(R.id.textView7_answer);
btn_convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
if (rb_celcius.isChecked()) {
String strFah = edTxt_amount.getText().toString();
if (!strFah.isEmpty()) {
dblFahrenheit = Double.parseDouble(strFah);
}
if (dblFahrenheit <= 212) {
dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
txtVw_answer.setText("The answer is " + dblConvertedTemp);
}
}
if (rb_fahrenheit.isChecked()) {
String strCel = edTxt_amount.getText().toString();
if (!strCel.isEmpty()) {
dblCelcius = Double.parseDouble(strCel);
}
if (dblCelcius <= 100) {
dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
txtVw_answer.setText("The answer is" + dblFahConversion);
} else if (v == btn_clear){
rg_group.clearCheck();
txtVw_answer.setText("");
}
}
}
}
}
});
}
Your indentation and use of braces makes it quite hard to see the problem. If I fix the indentation, I get the following code:
public void onClick(View v) {
double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
if (rb_celcius.isChecked()) {
String strFah = edTxt_amount.getText().toString();
if (!strFah.isEmpty()) {
{
dblFahrenheit = Double.parseDouble(strFah);
}
if (dblFahrenheit <= 212) {
dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
txtVw_answer.setText("The answer is " + dblConvertedTemp);
}
if (rb_fahrenheit.isChecked()) {
String strCel = edTxt_amount.getText().toString();
if (!strCel.isEmpty()) {
{
dblCelcius = Double.parseDouble(strCel);
}
if (dblCelcius <= 100) {
dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
txtVw_answer.setText("The answer is" + dblFahConversion);
}
}
else if (v == btn_clear){
rg_group.clearCheck();
txtVw_answer.setText("");
}
}
}
}
}
}
I think it does not work because you check if rb_fahrenheit is checked within the if block that is executed when rb_celcius is checked. Given the fact that these views are radiobuttons, I assume that they reside in the same RadioGroup in which case both radiobuttons have to be checked. This is impossible for a RadioGroup. This explains why the other way around does not work. From the looks of it, the code itself should work.
I want to code a simple calculator with 3 inputs (NUM1,NUM2,NUM3), 3 buttons(SUM,SUB,MUL) and 1 RESULT text plane. In my app if I give all inputs, the app works correctly. However if I don't give any input for 1 field, I encounter app stopped error. I want to make my app work with any number of inputs.
Here is my code.
public void onButtonClickSum(View v){
EditText e1 = (EditText)findViewById(R.id.editText);
EditText e2 = (EditText)findViewById(R.id.editText2);
EditText e3 = (EditText)findViewById(R.id.editText3);
TextView t1 = (TextView)findViewById(R.id.textView2);
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int num3 = Integer.parseInt(e3.getText().toString());
int sum = num1 + num2 + num3;
t1.setText(Integer.toString(sum));
}
public void onButtonClickSub(View v){
EditText e1 = (EditText)findViewById(R.id.editText);
EditText e2 = (EditText)findViewById(R.id.editText2);
EditText e3 = (EditText)findViewById(R.id.editText3);
TextView t1 = (TextView)findViewById(R.id.textView2);
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int num3 = Integer.parseInt(e3.getText().toString());
int sub = num1 - num2 - num3;
t1.setText(Integer.toString(sub));
}
public void onButtonClickMul(View v){
EditText e1 = (EditText)findViewById(R.id.editText);
EditText e2 = (EditText)findViewById(R.id.editText2);
EditText e3 = (EditText)findViewById(R.id.editText3);
TextView t1 = (TextView)findViewById(R.id.textView2);
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int num3 = Integer.parseInt(e3.getText().toString());
int mul = num1 * num2 * num3;
t1.setText(Integer.toString(mul));
}
You need to put null or empty checks before you parse the input as integers
Try making the EditText objects fields, since you are reusing them in each method
Example:
// Fields
private EditText mEditText1 = (EditText)findViewById(R.id.editText);
private EditText mEditText2 = (EditText)findViewById(R.id.editText2);
private EditText mEditText3 = (EditText)findViewById(R.id.editText3);
private TextView mTextView = (TextView)findViewById(R.id.textView2);
public void onButtonClick(View v){
int num1 = 0;
int num2 = 0;
int num3 = 0;
String e1 = mEditText1.getText();
String e2 = mEditText2.getText();
String e3 = mEditText3.getText();
if(!e1.isEmpty()) {
num1 = Integer.parseInt(e1);
}
if(!e2.isEmpty()){
num2 = Integer.parseInt(e2);
}
if(!e3.isEmpty()){
num3 = Integer.parseInt(e3);
}
int sub = num1 - num2 - num3;
mTextView.setText(Integer.toString(sub));
}
Change from
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int num3 = Integer.parseInt(e3.getText().toString());
to
int num1 = 0, num2 = 0, num3 = 0;
int num1_error = 0, num2_error = 0, num3_error = 0;
try{
num1 = Integer.parseInt(e1.getText().toString());
}Catch(Exception e){
num1_error = 1;
}
try{
num2 = Integer.parseInt(e2.getText().toString());
}Catch(Exception e){
num2_error = 1;
}
try{
num3 = Integer.parseInt(e3.getText().toString());
}Catch(Exception e){
num3_error = 1;
}
// for addition
int sum = 0;
if(num1_error == 0){
sum += num1;
}
if(num2_error == 0){
sum += num2;
}
if(num1_error == 0){
sum += num3;
}
t1.setText(Integer.toString(sum));
// for subtraction
int sub = 0;
if(num1_error == 0){
sub = num1 - sub;
}
if(num2_error == 0){
sub = sub - num2;
}
if(num3_error == 0){
sub = sub - num3;
}
t1.setText(Integer.toString(sub));
// for multiplication
int mul = 1;
if(num1_error == 0){
mul = mul*num1;
}
if(num2_error == 0){
mul = mul*num2;
}
if(num3_error == 0){
mul = mul*num3;
}
t1.setText(Integer.toString(mul));
Try doing this and same for other methods:
EditText e1 ,e2, e3;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
e3 = (EditText)findViewById(R.id.editText3);
t1 = (TextView)findViewById(R.id.textView2);
}
public void onButtonClickSum(View v){
if(e1.getText().toString().length()==0){
e1.setError("Required");
}
else if(e2.getText().toString().length()==0){
e2.setError("Required");
}
else if(e3.getText().toString().length()==0){
e3.setError("Required");
}
else{
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int num3 = Integer.parseInt(e3.getText().toString());
int sum = num1 + num2 + num3;
t1.setText(Integer.toString(sum));
}
}
I have 2 Edittext which only accepts numbers.
I want to make sure that the fields are not empty before the submit button is pressed to stop the application from crashing.
These are the 2 edittext fields i have.
EditText weightText = (EditText)findViewById(R.id.WeightText);
EditText heightText = (EditText)findViewById(R.id.Heighttext);
Code:
public class BMI extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
}
public void calculateHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.calculate) {
// get the references to the widgets
EditText weightText = (EditText)findViewById(R.id.WeightText);
EditText heightText = (EditText)findViewById(R.id.Heighttext);
TextView resultText = (TextView)findViewById(R.id.resultLabel);
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
}
}
private float calculateBMI (float weight, float height) {
return (float)Math.round((weight / (height * height)) * 100) / 100 ;
}
// interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}
You can check that your EditTexts are not empty like below:
weightText.getText().length() != 0 && heightText.getText().length() != 0
And you can use this condition inside the onClick(View) method, which is called when your button is clicked like below:
yourButton.setOnClickListener( new OnClickListener(){
public void onClick(){
if(weightText.getText().length() != 0 && heightText.getText().length() != 0){
//do sth
}
});
The second way, you can create TextWatcher which set to both EditTexts and in onTextChanged() you can check that both EditTexts are not empty like below:
private class YourTextWatcher implements TextWatcher{
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
yourButton.setEnabled(weightText.getText().length() != 0 && heightText.getText().length() != 0)
}
#Override
public void afterTextChanged(Editable editable) {
}
}
you can set this TextWatcher for your EditText like below:
weightText.addTextChangedListener(new YourTextWatcher());
heightText.addTextChangedListener(new YourTextWatcher());
Here is code that your Activity should look like:
public class BMI extends Activity {
EditText weightText;
EditText heightText;
TextView resultText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
weightText = (EditText) findViewById(R.id.WeightText);
heightText = (EditText) findViewById(R.id.Heighttext);
resultText = (TextView) findViewById(R.id.resultLabel);
Button button = (Button) findViewById(R.id.calulate);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculateHandler();
}
});
}
public void calculateHandler() {
// make sure we handle the click of the calculator button
if (weightText.getText().length() == 0 || heightText.getText().length() == 0) {
return;
}
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
// display toast additionally to example
Toast.makeText(this, bmiValue + " = " + bmiInterpretation, Toast.LENGTH_LONG).show();
}
private float calculateBMI(float weight, float height) {
return (float) Math.round((weight / (height * height)) * 100) / 100;
}
// interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}
Calculate method
public void calculateHandler() {
// make sure we handle the click of the calculator button
if (weightText.getText().toString().trim().length() == 0 || heightText.getText().toString().trim().length() == 0) {
Toast.makeText(this, "Please fill all field by numbers", Toast.LENGTH_LONG).show();
return;
}
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
// display toast additionally to example
}
weightText.getText().toString().isEmpty();
try like this..
String weight = weightText.getText().toString();
String height = heightText.getText().toString();
if((Integer.parseInt(weight)!=null) && (Integer.parseInt(height)!=null)){
//Not an empty
}
else{
//empty
}
I'm working on an image processing application that analyzes ECG graph. To do so, I need to detect certain peaks of the graph.
How can I print the "coordinates", "xcoor" and "ycoor" in the user interface? I tried toasting it but it doesn't work. I tried textView but the application force closes.
package com.thesis.results;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import android.widget.Toast;
import com.example.edgedetection.R;
public class MainActivity extends Activity {
// initialize variables
static int i = 0;
static int bl_ = 0; // number of black; pixels in the image
static int op_ = 0;
static int Al = 0;
static int Re = 0;
static int Gr = 0;
static int Bl = 0;
static int Alp = 0;
static int Red = 0;
static int Gre = 0;
static int Blu = 0;
static int stop = 0;
static int stopx = 0;
static int stopy = 1000;
static int xcoor[];
static int ycoor[];
static int width;
static int height;
static int RRdistance;
static double voltage;
static int peakcoordinates;
ImageView imageSource, imageAfter;
Bitmap bitmap_Source;
ProgressBar progressBar;
Button process;
TextView counter;
TextView coordinates;
private Handler handler;
Bitmap afterProcess;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
process = (Button) findViewById(R.id.btn_process);
imageSource = (ImageView) findViewById(R.id.imageSource);
imageAfter = (ImageView) findViewById(R.id.imageAfter);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
counter = (TextView) findViewById(R.id.counter);
coordinates = (TextView) findViewById(R.id.coordinates);
process.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
bitmap_Source = BitmapFactory.decodeResource(getResources(),
R.drawable.test_ideal_graph);
handler = new Handler();
StratBackgroundProcess();
}
});
}
private void StratBackgroundProcess() {
Runnable runnable = new Runnable() {
public void run() {
afterProcess = processingBitmap(bitmap_Source, 0, 0, 0);
handler.post(new Runnable() {
public void run() {
progressBar.setVisibility(View.GONE);
imageAfter.setImageBitmap(afterProcess);
calculatevoltage();
// calculateRRdistance();
counter.setText("" + bl_ + "# (" + stopx + "," + stopy
+ " " + "and" +
")" + " {" + width + "," + height + " } = "
+ voltage + "mV" + " " + "R-R distance:" + " "
+ RRdistance);
coordinates.setText(" " + xcoor + "," + ycoor + " " );
}
private void calculatevoltage() {
// TODO Auto-generated method stub
voltage = ((0.1 * height) * (height - stopy)) / height;
// 1.5 mV is the total voltage of the graph, 1 box =
// 0.1mV
}
//private void calculateRRdistance() {
// TODO Auto-generated method stub
// RRdistance = stopx1 - stopx;
// 1.5 mV is the total voltage of the graph, 1 box =
// 0.1mV
// }
});
}
};
new Thread(runnable).start();
}
public static Bitmap processingBitmap(Bitmap src, double red, double green,
double blue) {
// image size
width = src.getWidth();
height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
int flag = 0;
//array
int[] trial = new int[width];
// scan through all pixels
for (int x = 0; x < width; ++x) {
flag = 0;
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
// apply filtering on each channel R, G, B
Al = Color.alpha(pixel);
Re = (int) (Color.red(pixel));
Gr = (int) (Color.green(pixel));
Bl = (int) (Color.blue(pixel));
// set new color pixel to output bitmap
if ((Re == 0) && (Gr == 0) && (Bl == 0) && (flag == 0)) {
bmOut.setPixel(x, y, Color.argb(255, 0, 0, 0));
flag = 1;
trial[x] = y;
} else
bmOut.setPixel(x, y, Color.argb(255, 255, 255, 255));
}
}
//detect all possible peaks
for (int x = 1; x < width; x++) {
if (trial[x] < trial[x - 1] && trial[x] < trial[x + 1]) {
peakcoordinates = src.getPixel(x, trial[x]); //get pixels, how to display? (textview, toast?)
//Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
}
//detect all R peaks
for (int y = 1; y > (trial[1]-50); y++ ){
xcoor[i] = x;
ycoor[i] = y;
}
return bmOut;
}
return bmOut;
}
}
Your application crashes because you're trying to access your Views from a worker thread, which is prohibited in Android. I'd recommend using the AsyncTask class, which runs a certain task on a worker thread and provides methods that run on UI thread, where you can update your Views. Hope this helps.
You can modify your view (something like TextView, ImageView) only in UI thread. Try AsyncTask:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
process = (Button) findViewById(R.id.btn_process);
imageSource = (ImageView) findViewById(R.id.imageSource);
imageAfter = (ImageView) findViewById(R.id.imageAfter);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
counter = (TextView) findViewById(R.id.counter);
coordinates = (TextView) findViewById(R.id.coordinates);
process.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
bitmap_Source = BitmapFactory.decodeResource(getResources(),
R.drawable.test_ideal_graph);
handler = new Handler();
new BackgroundTask().execute();
}
});
}
private class BackgroundTask extends AsyncTask<String, Long, String> {
#Override
protected String doInBackground(String... s) {
afterProcess = processingBitmap(bitmap_Source, 0, 0, 0);
return null;
}
#Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
imageAfter.setImageBitmap(afterProcess);
calculatevoltage();
// calculateRRdistance();
counter.setText("...");
coordinates.setText(" " + xcoor + "," + ycoor + " " );
}
}