I want to give the output, which was entered by the user in EditText, in a detailed manner in a separate dialog box, by using the values of those entered values in the edittext. Any help will be appreciated.
Here is the code:
EditText editText1,editText2,editText3;
Button btn,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn2 = (Button) findViewById(R.id.btn2);
editText1 = (EditText) findViewById(R.id.editText2);
editText2 = (EditText) findViewById(R.id.editText3);
editText3 = (EditText) findViewById(R.id.editText4);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
String s = " (Result) ";
alertDialogBuilder.setTitle("Detailed Output is");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage("(The code should be like as follows)\n\n"+"sum of entered number is:"+s+"\n"+"multiply of entered number is:"+s+"\n"+"subtraction of entered number is:"+s)
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
public void calculate() {
String a = editText1.getText().toString();
String b = editText2.getText().toString();
editText3.setText(String.valueOf(Integer.parseInt(a)+ Integer.parseInt(b))+"\n"+String.valueOf(Integer.parseInt(a)* Integer.parseInt(b))+"\n"+String.valueOf(Integer.parseInt(a)-Integer.parseInt(b)));
}
}
you can user String.Format to format your string with input parameters.
first don't write msg in code because if you want translate your app to different language you don't have option, better practice is used strings.xml file to storage your string
good method is add your msg to strings.xlm file
and if you want use this in code:
and add to dialog:
Ok, try this way:
First, declare your two EditText (those you want to sum, subtract and multiply the values, etc.) as final inside the onCreate() method of your MainActivity:
Second, try this code for your
btn2.setOnClickListener
: