1

I want to display an error message on an Android app screen after some error happens. So I put a string in my strings.xml file and in the code I'd simply like to tell the system to display that code.

But I am not entirely sure how to do that. I have something like this that I scribbled:

TextView errorMessage = (TextView) findViewById(R.id.add_problem_validation_error);
    errorMessage.setText(R.string.add_problem_validation_error);

But its not even compiling because I am not sure how to properly refer to the string.xml items and display them.

Any idea how to do this the right way?

Thanks!

1
  • Also if there is an error you are getting post the error from the logcat Commented Mar 7, 2012 at 21:14

2 Answers 2

1

Why not just use a Toast?

Toast.makeText(getApplicationContext(),
           R.string.add_problem_validation_error, Toast.LENGTH_LONG).show();

Or you could use a alert dialog..

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.add_problem_validation_error)
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I was thinking to use toast, but what if the person doesn't notice the message and the message disappears lol...it might confuse the user....but ye I would prefer toast.
Im sure they will see the nice toast on the screen. lol Alot of applications use this and im sure 99.9% of all there users see it. Or you could try to display a alert dialog. Try either our and see if it works for you. This is what i would do.
1

Try using this code instead:

TextView errorMessage = (TextView) findViewById(R.id.add_problem_validation_error);
errorMessage.setText(getResources().getString(R.string.add_problem_validation_error));

The variable R.string.add_problem_validation_error is really an integer id used by the Android OS to point to resources. You need to use the resources class to get the corresponding value of that id.

Your TextView will need an id line in the xml file:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/add_problem_validation_error"
    />

Your strings.xml file will need a string line with the message:

<string name="add_problem_validation_error">Your message here</string>

2 Comments

@onit this line TextView errorMessage = (TextView) findViewById(R.id.add_problem_validation_error); does not work because of the id. Does the id have to be present in both the xml file for that activity and strings.xml?
@GeekedOut Edited my post. Check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.