Calling final Editables to use in toast - android

I am unsure as to how to approach this.
I have taken some guidelines given by another person on here and have now gotten to the point of it crashing when I push the button (the monitor)
http://i.stack.imgur.com/C8WgN.png
I want to be able to post a toast notification when the button is pushed, and then have it go away after a few seconds
Also, is there a way to do it to where it will still create a toast notification although some of the info (like line2) is empty?
basic.java:
public class Basic extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic);
Spinner spinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Button display = (Button) findViewById(R.id.Button01);
display.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText firstField = (EditText) findViewById(R.id.First);
EditText lastField = (EditText) findViewById(R.id.Last);
EditText line1Field = (EditText) findViewById(R.id.Line1);
EditText line2Field = (EditText) findViewById(R.id.Line2);
EditText cityField = (EditText) findViewById(R.id.City);
EditText stateField = (EditText) findViewById(R.id.Spinner01);
EditText zipField = (EditText) findViewById(R.id.Zip);
//EditText phoneField = (EditText) findViewById(R.id.Telephone);
final Editable firstName = firstField.getText();
final Editable lastName = lastField.getText();
final Editable lineOne = line1Field.getText();
final Editable lineTwo = line2Field.getText();
final Editable city = cityField.getText();
final Editable state = stateField.getText();
final Editable zip = zipField.getText();
//final Editable phoneNumber = phoneField.getText();
Toast toDisplay = Toast.makeText(null, firstName, 10);
toDisplay.show();
}
});
}
}
I would like the layout of the toast to be multi-lined, and looking like a postal address
First Last
Street Line 1
Line 2
City, State Zip

Toast.makeText(Change.this, "First Last \n Street Line 1 \n Line 2 \n City, State Zip", Toast.LENGTH_SHORT).show();
By using above toast we can display the output in multilines.In above statement i am using static text in that place u put required fieds like
Toast.makeText(Change.this, firstField.getText().toString()+"\n"+ lastField.getText().toString()+"\n"+ City.getText().toString(), Toast.LENGTH_SHORT).show();

Related

Handling empty EditText with toast

So this is the code i got so far:
Button calculate = (Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText sun = (EditText)findViewById(R.id.sun1);
EditText mon = (EditText)findViewById(R.id.mon1);
EditText tue = (EditText)findViewById(R.id.tue1);
EditText wed = (EditText)findViewById(R.id.wed1);
EditText thu = (EditText)findViewById(R.id.thu1);
EditText fri = (EditText)findViewById(R.id.fri1);
EditText sat = (EditText)findViewById(R.id.sat1);
EditText wage = (EditText)findViewById(R.id.wage);
double sun1 = Double.parseDouble(sun.getText().toString());
double mon1 = Double.parseDouble(mon.getText().toString());
double tue1 = Double.parseDouble(tue.getText().toString());
double wed1 = Double.parseDouble(wed.getText().toString());
double thu1 = Double.parseDouble(thu.getText().toString());
double fri1 = Double.parseDouble(fri.getText().toString());
double sat1 = Double.parseDouble(sat.getText().toString());
double wage1 = Double.parseDouble(wage.getText().toString());
TextView hours = (TextView)findViewById(R.id.hours);
TextView income = (TextView)findViewById(R.id.Income);
if(wage.getText().length()==0){
Toast.makeText(getContext(), "Enter the wage", Toast.LENGTH_SHORT).show();
}
else{
Calculator calc = new Calculator(sun1,mon1,tue1,wed1,thu1,fri1,sat1,wage1);
hours.setText(calc.hours()+"");
income.setText(calc.calc()+"");
}
As you can see, I want to display a toast whenever the user leaves any field empty. In this case, I have used only wage object to show where I am failing. I have also tried wage.getText().toString().equals("") and still my app crashes. Maybe it is the toast class that i am getting wrong. Using (this) instead of getContext() in the maketoast doesn't help either. Any suggestions?
You are getting an error, because your findViewById(...) isn't bound to the correct this. It should be outside of the onClick listener.
Typically, I would do something like the following:
public class MainActivity extends Activity {
private EditText sun;
private EditText mon;
private EditText tue;
private EditText wed;
private EditText thu;
private EditText fri;
private EditText sat;
private EditText wage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instantiateViews();
}
private void instantiateViews() {
sun = (EditText) findViewById(R.id.sun1);
mon = (EditText) findViewById(R.id.mon1);
tue = (EditText) findViewById(R.id.tue1);
wed = (EditText) findViewById(R.id.wed1);
thu = (EditText) findViewById(R.id.thu1);
fri = (EditText) findViewById(R.id.fri1);
sat = (EditText) findViewById(R.id.sat1);
wage = (EditText) findViewById(R.id.wage);
}
//Write whatever other code you would here
}
Also, I recommend you use TextUtils.isEmpty over your current method to check that wage is empty.

Hi! How do I store the fields and display the results on a different activity ? Please

I want the fields entered by the user to be displayed as a list view of the studentnoText and courseField in a new activity..
public class Student extends Activity {
private String totalStudents [];
private EditText studentnoText;
private EditText studentnameText;
private EditText courseField;
private Spinner departmentSpinner;
private EditText dobField;
private Button registerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
studentnoText = (EditText) findViewById(R.id.studentnoText);
studentnameText = (EditText) findViewById(R.id.studentnameText);
dobField = (EditText) findViewById(R.id.dobField);
courseField = (EditText) findViewById(R.id.courseField);
departmentSpinner = (Spinner) findViewById(R.id.departmentSpinner);
registerButton = (Button) findViewById(R.id.registerButton);
}
public void registerButtonPressed (View view){
Log.v("Register","Register Pressed!");
}
}
USE PUT EXTRA TO SEND DATA
mIntent.putExtra("TEXT", studentnoText.gettext().toString());
mIntent.putExtra("TEXT_1", studentName.gettext().toString());
USE GET EXTRA TO GET DATA
String strNO,strName;
strNO= extras.getString("TEXT");
strName = extras.getString("TEXT_1");

Want to display spinner selected values on textview when button is clicked

I have taken 3 edittext in my form and three spinner. I can easily get the data from edit text and display it on Textview, but the problem is how to get the data from spinner and display all the edit text and spinnner values in textview.
I am creating a form in which when the button is clicked all the data from edittext text and spinner is displayed on Textview
Try this:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v){
String item= spinner .getSelectedItem().toString();
textView .setText(item);
}
});
Try this
Button btn = (Button) findViewById(R.id.your_btn);
btn.setOnClickListner(new OnClickListener {
public void onClick(View v)
{
String spinnerStr = yourSpinner.getSelectedItem().toString();
String editTextStr = yourEditText.getText().toString();
yourTextView.setText(spinnerStr + editTextStr);
}
}
);
please try this one:
String textValue= yourSpinner.getSelectedItem().toString();

Android App crashes when I try to capture from EditText

So I am trying to make a contact form in an Alert Dialogue box which opens from a menu option. I have 3 EditText fields in my form and in my Main.java I read from those fields when the Send button in form is pressed and then I start an Email intent, or at least thats what it is supposed to do. Right now the app crashes as soon as I press the send button. Now I have troubleshooted the problem and it doesnt seem to be in the intent but it occurs when I read from the EditText fields. The code works fine when I take out the EditText reading part and just put filler information in its place, but I need this to work with the EditTexts. Thank you.
My whole code for the option in the menu:
case R.id.menu_feedback:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.menu_feedback);
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.feedback, null));
// Add the buttons
builder.setNegativeButton(R.string.send, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
final EditText nameField = (EditText) findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
final CheckBox responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);
boolean bRequiresResponse = responseCheckbox.isChecked();
/* Create the Intent */
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, R.string.send_email);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback);
/* Send it off to the Activity-Chooser */
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
dialog.dismiss();
}
});
builder.setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.show();
return true;
Ok, I assume your all EditTexts are written in feedback layout file for AlertDialog,
View dialogView = inflater.inflate(R.layout.feedback, null);
builder.setView(dialogView );
And Edittext are like,
// User clicked OK button
final EditText nameField = (EditText) dialogView.findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText) dialogView.findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText) dialogView.findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
Actually, when you are inflating any view and you have to use child views of that views then you have to use the reference of that view and findViewById() method. So in your case, dialogView.findViewById();
use builder or inflater instances for Accessing EditText or other views from AlertDialog as :
builder.setNegativeButton(R.string.send, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
final EditText nameField = (EditText)inflater. findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText)inflater.
findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText)inflater.
findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
// your code here..

Button click problem

I'm a beginner in application development. My problem is, that when I run my app and I click on the Calculate button, the program stops. The code:
public class screen1 extends Activity {
private EditText name;
private CheckBox box1;
private Spinner spinner;
private TextView text1, text2, text3, text4, text5, text6;
private Button calcbutton, closebutton;
String strength;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.military_ranks , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hubSpinner.setAdapter(adapter);
name = (EditText)findViewById(R.id.editText1);
strength = name.getText().toString();
box1 = (CheckBox)findViewById(R.id.checkBox1);
text1 = (TextView)findViewById(R.id.textView4);
text2 = (TextView)findViewById(R.id.textView6);
text3 = (TextView)findViewById(R.id.textView8);
text4 = (TextView)findViewById(R.id.textView10);
text5 = (TextView)findViewById(R.id.textView12);
text6 = (TextView)findViewById(R.id.textView14);
final Button calcbutton = (Button) findViewById(R.id.button1);
calcbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
double sebzes;
if(box1.isChecked()){
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1*(1+1/100);
text1.setText(Double.toString(sebzes));
}
else{
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1;
text1.setText(Double.toString(sebzes));
}
}
});
final Button closebutton = (Button) findViewById(R.id.button2);
closebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
In the edittext component you should be able to write numbers only. I don't know why it's not working.
The problem are these two lines:
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
The first won't fail if you use only numbers in your EditText but it would be better to ensure that or at least catch the exception that is thrown when you try to convert a character to a numberical value. Additionally you could also use Integer.valueOf(strength).intValue(); even so it is normally not really necessary.
The real problem is the second line. You declared the variable spinner but you never instantiate it. That's why you will get a NullPointerException there.
On an unrelated note: You also should start your class name with a capital letter to follow the Java naming conventions.
You're not instantiating spinner anywhere, but you're referencing it in the second line of your button click method. Probably a null reference problem.

Categories

Resources