how to make Multiple-calc operation in one click button -android- - android

i try to make Multiple-calc operation in one click but when i click on button done its show error so how can i modification this code to be able to calc all of this operations in one click with out any errors . here is the code
public class calcit extends Activity {
EditText onehighx,twowightx,threefinallresultx,four100x,fiveresultof100x,sixresultofresultx;
Button donee;
BigDecimal onehigh,twowight,threefinallresult,four100,fiveresultof100,sixresultofresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calcit);
donee = (Button) findViewById(R.id.button1);
onehighx = (EditText) findViewById(R.id.high);// high
twowightx = (EditText) findViewById(R.id.Weight);//Weight
threefinallresultx = (EditText) findViewById(R.id.finall_result);//result =Weight * result of result 100
four100x = (EditText) findViewById(R.id.e100);//100
fiveresultof100x = (EditText) findViewById(R.id.re_of_100);// result of 100 = high * 100
sixresultofresultx = (EditText) findViewById(R.id.result_of_100_100);// result of result 100 /ex : 1.75*1.75
donee.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onehigh = new BigDecimal(onehighx.getText().toString());
twowight= new BigDecimal(twowightx.getText().toString());
threefinallresult = new BigDecimal(threefinallresultx.getText().toString());
four100 = new BigDecimal(four100x.getText().toString());
fiveresultof100 = new BigDecimal(fiveresultof100x.getText().toString());
sixresultofresult = new BigDecimal(sixresultofresultx.getText().toString());
//---here i want to fix this code to be able to show all of this Multiple-calc operations in
//(threefinallresultx)------
fiveresultof100x.setText((onehigh).divide(four100).toString());
sixresultofresultx.setText((fiveresultof100).multiply(fiveresultof100).toString());
threefinallresultx.setText((twowight).divide(sixresultofresult).toString());
}
});
}

If I am not wrong you want to do calculations on values fetched from Edittexts but you are instead trying to divide or multiply setText boxes and not the values contained in them. So just fetch values in appropriate format and calculate them normally such as
onehigh=onehigh/four100;
fiveresultof100=fiveresultof100*fiveresultof100;
twowight=twowight/sixresultofresult;
and then set them in textboxes
fiveresultof100x.setText(onehigh);
ixresultofresultx.setText(fiveresultof100);
threefinallresultx.setText(twowight);

Related

saving/printing text Android

I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.
Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}
Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.

Android - How to calculate one edit text and divide another value?

I am a total beginner, I am wondering if anyone could help me out with the code. I am trying to make ideal daily water intake apps.
There will be only one edit text for user to input their weight and I want it to divide for example 0.024. Have button to calculate and then display the answer on screen.
public class WaterCalculate extends Activity {
//Declare textviews as fields, so they can be accessed throughout the activity.
EditText weightuser;
TextView tv4;
ImageButton calculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.water_calculate);
//Bind the EditText views
weightuser = (EditText)findViewById(R.id.weight);
tv4 = (TextView)findViewById(R.id.res);
calculate = (ImageButton)findViewById(R.id.calc);
calculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
calculate();
}
});
}
private void calculate() {
//get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(weightuser.getText().toString());
//do the calculation
Double calculatedValue = (value1/0.024);
//set the value to the textview, to display on screen.
tv4.setText(String.valueOf("You need " + calculatedValue + "\nliters of water per day" ));
}
}
When i run the apps the button to calculate its not working and it show the app has stopped. Appreciate for the help.
I thought the problem is in this line.
tv4.setText(String.valueOf("You need " + calculatedValue + "\nliters of water per day" ));
Give a try like this...
tv4.setText("You need "+Double.toString(calculatedValue)+"\nliters of water per day");
and also ensure that you catched the exceptions at necessary places like converting editText value into double.
Try the below code, am sure it will work, when you use click listener for button you should use View.Onclick
class WaterCalculate extends Activity {
// Declare textviews as fields, so they can be accessed throughout the
// activity.
EditText weightuser;
TextView tv4;
ImageButton calculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.water_calculate);
// Bind the EditText views
weightuser = (EditText) findViewById(R.id.weight);
tv4 = (TextView) findViewById(R.id.res);
calculate = (ImageButton) findViewById(R.id.calc);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calculate();
}
});
}
private void calculate() {
// get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(weightuser.getText().toString());
// do the calculation
Double calculatedValue = (value1 / 0.024);
// set the value to the textview, to display on screen.
tv4.setText(String.valueOf("You need " + calculatedValue
+ "\nliters of water per day"));
}
}

Android:Got NumberFormatExxception while getting data from EditText field

java.lang.RuntimeException: Unable to start activity ComponentInfo { com.project/com.project.simple} : java.lang.NumberFormatException
EditText et1,et2,et3;
Button b1, b2;
Float two,three,four;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.can);
et1 = (EditText) findViewById(R.id.editText1);
two = Float.valueOf(et1.getText().toString());
et2 = (EditText) findViewById(R.id.editText2);
three = Float.valueOf(et2.getText().toString());
et3 = (EditText) findViewById(R.id.editText3);
four = Float.valueOf(et3.getText().toString());
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
you are in onCreate() where the fields are specified. the user could not have time to enter any valid data. you need to move the fetching of the data somewhere else...like your onClick() perhaps.
move your code from onCerate to an other method
(for example the onClick method of the Button )
and you should
try this:
try{
two = Float.parseFloat(et1.getText().toString());
}catch(NumberFormatException e){
two = 0;
Toast toast = Toast.makeText(this, 'Invalid number format on `two` field', 500);
toast.show();
}
For each text fields what you want to read
Comment:
float format is 2.3 and don't use 2,3
It seems that the input is not valid. Please check twice that you don't try to parse a letters to a number. If you have a float please check that you use the right locale for parsing. E.g. in germany is pi 3,1415... and not 3.1415...
If you cannot preparse the values you could put the parsing trys in try catch blocks like this:
float value;
try {
value=Float.parseFloat(someString);
} catch(NumberFormatException e) {
// input was no valid float
}

add text boxes to a view in android

I have a View in Android which is basically a large image, with many small images in it that can be dragged. I want to add an option to create text boxes inside this view also, i.e. for the user to be able to create a text bix, write in it, move it around, etc. Does anyone know of a way to do this? Thanks!!!
Try to put a text box on small images as disable to set, and for example setOnLongClickListener and enable it so you can provide user to write textbox... Then setOnTouchListener on every small images to move around on the big one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ViewGroup vg = (ViewGroup) findViewById(R.id.lout);
final ImageView img = (ImageView) findViewById(R.id.imageView1);
final TouchListener listen1 = new TouchListener();
img.setOnTouchListener(listen1);
DragListener listen2 = new DragListener();
vg.setOnTouchListener(listen2);
final EditText et = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView lbl = new TextView(vg.getContext());
lbl.setText(et.getText());
lbl.setLayoutParams(new LayoutParams(0,0,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lbl.setOnTouchListener(listen1);
vg.addView(lbl);
}
});
}
Something like this...

I have an int value; now how do I present it in a TextView to the user? Android

Beginning I should say that I am very new to programming.
I am building an android application which includes a calculator function. What I want to do is on button click to get the user input from two EditTexts, add them together and then display the result in a TextView. Similar questions have been covered by others, but what they don't cover is how do you actually display the result in a TextView (or at least I didn't find one myself). So I tried the following which was suggested in many posts:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.team2);
//finds the references for the view in the layouts
b_t2p_ok = (Button) findViewById(R.id.bOK2);
EditText1 = (EditText) findViewById(R.id.etPaixnidi2);
EditText2 = (EditText) findViewById(R.id.etPontoi2);
b_t2p_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//First I am trying to do this for one EditText
String myEditValue = etPaixnidi20.getText().toString();
int myEditNum = Integer.parseInt(myEditValue);
myEditNum.setText(textOut);
}
});
}
However when I try to display the int myEditNum in a TextView(textOut1) using the setText() method I get an error for the setText method (saying: ''Cannot invoke setText(TextView) on the primitive type int'').
I also tried (below) to convert int myEditNum to a String and then display it to a TextView but still doesn't work as I get the folloing error for the setText method: ''The method setText(TextView) is undefined for the type String''.
EditText myEdit = (EditText) findViewById(R.id.edittext1);
String myEditValue = myEdit.getText().toString();
int myEditNum = Integer.parseInt(myEditValue);
String texting = Integer.toString(myEditNum);
texting.setText(textOut1);
Why does this happen and how do I fix this?
You need to call setText(string) on a Textview. So you can do something like:
// Replace R.id.textview1 with the id of your textview
TextView tv = (TextView) findViewById(R.id.textview1);
tv.setText(myEditValue);
You seem to be using the wrong variable to settext here. the View class can do setText()
Try:
...
public void onClick(View v)
{
//First I am trying to do this for one EditText
String myEditValue = etPaixnidi20.getText().toString();
EditText1.setText( myEditValue );
// or
EditText2.setText( myEditValue );
}
...
you should use
textOut1.setText(texting)
You have the TextView and the String the wrong way round.
textOut1.setText(texting);

Categories

Resources