I am trying to get the value of inputEditText and multiply it by the value of convertNum.
The error I'm getting is: Operator '*' cannot be applied to 'android.widget.EditText','double'
I believe I need to convert the value of inputEditText into a number, but I'm not sure. Any help would be appreciated. Here is my code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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);
// Declare variables
final Button calculateButton = (Button) findViewById(R.id.button);
final TextView resultTextView = (TextView) findViewById(R.id.textView2);
final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
final EditText inputEditText = (EditText) findViewById(R.id.editText);
final double convertNum = 0.125;
// Set resultTextView to be invisible on app start
resultTextView.setVisibility(View.INVISIBLE);
// When calculate button is pressed: Set resultTextView to visible and show result
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resultTextView.setVisibility(View.VISIBLE);
resultnumTextView.setText(inputEditText * convertNum);
}
});
}
You get the error as you are trying to multiply EditText (which is a View instance) and double (which is a number).
Easiest fix is to replace
resultnumTextView.setText(inputEditText * convertNum);
with
resultnumTextView.setText(String.valueOf(
Float.valueOf(inputEditText.getText().toString()) * convertNum
));
Instead of this:
resultnumTextView.setText(inputEditText * convertNum);
Do this:
resultnumTextView.setText(Double.parseDouble(inputEditText.getText().toString()) * convertNum);
inputEditText is an EditText, a widget to write text. You need to extract the typed text and convert into a float.
inputEditText.getText().toString()
Will give you a String with the typed text. Then use the wrapper class Float to convert into a number
String s = inputEditText.getText().toString();
Float f= Float.parseFloat(s);
As you can see here
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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);
// Declare variables
final Button calculateButton = (Button) findViewById(R.id.button);
final TextView resultTextView = (TextView) findViewById(R.id.textView2);
final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
final EditText inputEditText = (EditText) findViewById(R.id.editText);
final double convertNum = 0.125;
// Set resultTextView to be invisible on app start
resultTextView.setVisibility(View.INVISIBLE);
// When calculate button is pressed: Set resultTextView to visible and show result
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String valueFromInput = inputEditText.getText().toString();
try{
double valueToDouble = Double.parseDouble(valueFromInput);
double result = convertNum * valueToDouble;
resultnumTextView.setText(result+"");
resultTextView.setVisibility(View.VISIBLE);
}catch(Exception e){
// print the exception.
}
}
});
}
Related
I am new to Android. I am trying to select the spinner choice and multiply it by the user input. (it's an app that multiples the number of pizzas by the cost of each pizza) Does that make sense? So, I am able to process one spinner option and get the correct answer, but i can't figure out how do code more than one spinner option.
Heres the Java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
//below is where I have placed the values associated with the Pizza Choices//
//each one is under the txtPizza string array and is referenced//
double costCheese = 4.50;
double costPepperoni = 5.75;
double costVeggie = 4.75;
double costGluten = 5.30;
double costPersonal = 3.15;
int txtnumber;
double txtresult;
String txtGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText amount = (EditText)findViewById(R.id.txtnumber);
final Spinner type =(Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.OrderBtn);
cost.setOnClickListener(new View.OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));
#Override
public void onClick(View v) {
txtnumber = Integer.parseInt(amount.getText().toString());
txtresult = costCheese * txtnumber;
DecimalFormat currency = new DecimalFormat("$###,###.##");
result.setText("Cost for this order is " + currency.format(txtresult)); }
});
}}
Well you need an adapter to illustrate the options that you desire.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array_spinner=new String[5];
array_spinner[0]="option1";
array_spinner[1]="option2";
array_spinner[2]="option3";
array_spinner[3]="option4";
array_spinner[4]="option5";
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
}
Check this out for more about spinners.
Line 33, all debug logs are fine.
As u can see I even comented the method that is causing problems and it still crashesh.
MainActivity.java
package com.example.hillsmatrixinverser;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Math matrix;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText Text1 = (EditText) findViewById(R.id.a) ;
final EditText Text2 = (EditText) findViewById(R.id.b) ;
final EditText Text3 = (EditText) findViewById(R.id.c) ;
final EditText Text4 = (EditText) findViewById(R.id.d) ;
// final EditText TextFinal = (EditText) findViewById(R.id.editText1) ;
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//
Log.d("TEXT1", Text1.getText().toString());
Log.d("TEXT2", Text2.getText().toString());
Log.d("TEXT3", Text3.getText().toString());
Log.d("TEXT4", Text4.getText().toString());
matrix.GetValues(Text1.getText().toString(), Text2.getText().toString(),
Text3.getText().toString(), Text4.getText().toString());
// TextFinal.setText(matrix.Calculate());
}
});
}
}
class:
package com.example.hillsmatrixinverser;
import android.app.Application;
public class Math {
public int a;
public int b;
public int c;
public int d;
public void GetValues(String a1, String b1, String c1, String d1){
// Integer.parseInt(a1);
// Integer.parseInt(b1);
// Integer.parseInt(c1);
// Integer.parseInt(d1);
}
public String Calculate(){
return Integer.toString(a+b+c+d);
}
}
Line causing problems:
matrix.GetValues(Text1.getText().toString(), Text2.getText().toString(),
Text3.getText().toString(), Text4.getText().toString());
You never initialize Math matrix;
So when you're trying to call your GetValues method on your matrix object, it throws the NPE.
in the program I have 3 fields to enter when I enter data and pressed for starting the button below creates three dynamic input fields, the program calculates the average score of only 3 top fields. I want that when the user changed any data in dynamically created fields it counted the data from these fields, regardless of their number
package com.example.averegemark;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
public class Table extends Activity {
LinearLayout LL;
EditText et1,et2,et3;
double coef=0,points=0,sum=0,coefsum=0;
TextView total;
Button bNext;
String name="",mark="",coeff="";
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
bNext = (Button) findViewById(R.id.bNext);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
total = (TextView) findViewById(R.id.tvTotal);
total.setText("GPA:");
LL = (LinearLayout) findViewById(R.id.LL);
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name = et1.getText().toString();
mark = et2.getText().toString();
coeff = et3.getText().toString();
if(mark.length()==0 || coeff.length()==0)
Toast.makeText(getApplicationContext(), "Enter mark `and coefficient", Toast.LENGTH_LONG).show();
else
NewView(name,mark,coeff);
}
});
}
private void NewView(String name, String mark, String coeff) {
// TODO Auto-generated method stub
EditText info1 = new EditText(this);
EditText info2 = new EditText(this);
EditText info3 = new EditText(this);
info1.setId(i);i++;
info1.setId(i);i++;
info1.setId(i);i++;
info1.setText(""+name);
info2.setText(""+mark);
info3.setText(""+coeff);
TableLayout table = (TableLayout) findViewById(R.id.tbl);
table.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(info1);
row.addView(info2);
row.addView(info3);
table.addView(row);
String rez="";
rez= info2.getText().toString();
points = Double.parseDouble(rez);
rez= info3.getText().toString();
coef = Double.parseDouble(rez);
sum +=coef*points;
coefsum+=coef;
sum/=coefsum;
total.setText("GPA : "+sum+"\nCoefficient sum. "+coefsum);
et3.setText("");
et2.setText("");
et1.setText("");
et1.requestFocus();
}
}
so when I will change any data in row , i want to recalculate this, but here it use only top EditText not dynamically created :
String rez="";
rez= info2.getText().toString();
points = Double.parseDouble(rez);
rez= info3.getText().toString();
coef = Double.parseDouble(rez);
sum +=coef*points;
coefsum+=coef;
sum/=coefsum;
total.setText("GPA : "+sum+"\nCoefficient sum. "+coefsum);
When you press the button, you get the information from the original edittexts. You have to find the last row in the table and get the edittexts from there.
CharSequence data = ((TextView) v).getText();
String result = data.toString();
Android - I am using intent to communicate between two activities. I used putExtra() in the calling activity and i am using getExtras in the called activity. But the problem is iam not able to set the edit text with the number i retrieved in the intent. below is my program`
// Calling activity
package com.example.androidtutorial2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.text.InputType;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//EDIT TEXT
final EditText sharedata = (EditText) findViewById ( R.id.editText1 ) ;
sharedata.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
//BUTTON
Button sendbutton = (Button) findViewById (R.id.button2) ;
sendbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int edittext_data = Integer.valueOf(sharedata.getText().toString());
Intent sendintent = new Intent(MainActivity.this,second_activity.class);
sendintent.putExtra("somedata", edittext_data);
startActivity(sendintent);
}
});
// Button button_to_call_activity = (Button) findViewById(R.id.button1);
// button_to_call_activity.setOnClickListener(new OnClickListener() {
//
// #Override
// public void onClick(View v) {
//
//
// }
// });
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
//CALLED ACTIVITY
package com.example.androidtutorial2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
public class second_activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
EditText displaymessage = (EditText) findViewById ( R.id.editText1 );
Intent intent1 = getIntent();
Bundle bundle1 = intent1.getExtras();
int integer1 = bundle1.getInt("somedata");
Integer string1 = integer1;
displaymessage.setText(integer1); // HERE IS A RUNTIME ERROR
}
}
use
displaymessage.setText("" +integer1);
Or
displaymessage.setText(String.valueOf(integer1));
1. First of all if you are using Java 1.5 and above dont useInteger.valueOf() but instead use Integer.parseInt().
eg:
int edittext_data = Integer.parseInt(sharedata.getText().toString());
2. Your problem with the output is here.
Dont do this ... Integer string1 = integer1;
Use it like this...
String string1 = getIntent().getExtras().getString("somedata");
displaymessage.setText(string1);
Since you don't seem to be doing anything with the variable as an integer, you could also simply keep it as a string.
sendintent.putExtra("somedata", sharedata.getText().toString());
String integer1 = bundle1.getString("somedata");
ok so i what to develop an application that will take the text from edittext to textview with the click of button this is what i have so far kind of a noob to java
package one.two;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Button;
import java.util.Scanner;
public class one extends Activity {
public static void main(String args[]){
Scanner what = new Scanner(System.in);
what.toString();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Button btn = findViewById(yourButtonId);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText txt = findViewById(yourEditTextId);
TextView txt1 = findViewById(yourTextViewId);
txt.setText( txt.getText().toString() );
}
});