user182944
user182944

Reputation: 8067

Setting a global error message for the entire application

How can i set a global error message for the entire application?

As of now, i am adding an TextView with text set to "" in the layout of every activity and when some error occurs, i am adding that error message to that TextView and displaying it to the user. I don't think it is a good approach to add an empty TextView in the layout of every activity, hence i am thinking of adding only one TextView somewhere which can be displayed in the layout of all the activity for the error messages.

Or, even may be some other approach other than adding an empty Textview.

Please suggest.

Regards,

Upvotes: 0

Views: 248

Answers (3)

Thanos Karpouzis
Thanos Karpouzis

Reputation: 315

By far the easier, and better applied for the appearance of your app is an AlertDialog.

It is super easy to implement using the alertdialog builder.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(myErrorMessage)
   .setCancelable(true)
   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });  

 AlertDialog alert = builder.create();
  alert.show();

It created a pop-up alert with one button to dismiss it. You don't have to add anything on the layouts and you can execute it anywhere on your code.

You can also set more attributes and make it even more beautiful and informative for the user like title or icons etc.

    String theErrorMessage = "The error Message";

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(theErrorMessage)
    .setTitle("ERROR!")
    .setIcon(R.drawable.ic_launcher)
       .setCancelable(true)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });  

     AlertDialog alert = builder.create();
     alert.show();

I will be much better to try this feature than a textview. This is the result of the code above.

screen shot 1

Upvotes: 1

bughi
bughi

Reputation: 1858

You could use a Dialog to show your error message.

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34775

you can define an xml file and you can include the xml file either by code or through xml.

Through xml you can include by below code::

<include layout="@layout/name"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" />

and through code you can inflate like below:::

View view; 
LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.mylayout, null);

Upvotes: 1

Related Questions