OSDN Git Service

Eleven: Use appropriate String value comparison
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / utils / CustomToast.java
1 package com.cyanogenmod.eleven.utils;
2
3 import android.app.Activity;
4 import android.view.Gravity;
5 import android.view.LayoutInflater;
6 import android.view.View;
7 import android.widget.TextView;
8 import android.widget.Toast;
9 import com.cyanogenmod.eleven.R;
10
11 /**
12  * Ancillary utilities class to customize the appearance of Toast messages
13  */
14 public class CustomToast {
15
16     public static final int LENGTH_LONG = Toast.LENGTH_LONG;
17     public static final int LENGTH_SHORT = Toast.LENGTH_SHORT;
18
19     private Toast mToast;
20     private TextView mTextView;
21
22     public CustomToast(Activity activity, String message) {
23         mToast = new Toast( activity.getApplicationContext() );
24         LayoutInflater layoutInflater = activity.getLayoutInflater();
25         View toastView = layoutInflater.inflate(R.layout.custom_toast, null);
26         mToast.setView(toastView);
27
28         mTextView = (TextView) toastView.findViewById(R.id.toast_text_view);
29         if (message != null) {
30             mTextView.setText(message);
31         }
32
33         // set toast location
34         // centered with an offset in y expressed as % of display height
35         int displayHeight = activity.getWindow().getDecorView().getHeight();
36         int heightOffset = (int)(0.30 * displayHeight);
37         mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, heightOffset);
38
39     }
40
41     public static CustomToast makeText(Activity context, String text, int duration) {
42         CustomToast customToast = new CustomToast(context, text);
43         if (duration == CustomToast.LENGTH_LONG)
44             customToast.setDuration(duration);
45         else
46             customToast.setDuration(CustomToast.LENGTH_SHORT);
47
48         return customToast;
49     }
50
51     public void setDuration(int duration) {
52         mToast.setDuration(duration);
53     }
54
55     public void setMessage(String message) {
56         mTextView.setText(message);
57     }
58
59     public void show() {
60         mToast.show();
61     }
62 }