custom_toast_layout.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| android:id="@+id/root" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/picture_frame" android:orientation="horizontal" > android:id="@+id/imageView1" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="5dp" android:src="@drawable/inbox" /> android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:text="Toast text here" android:textColor="#000000" android:textAppearance="?android:attr/textAppearanceSmall" /> |
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
| android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#454545" android:orientation="vertical" > android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click here to see custom Toast" /> |
In java File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| Button mButton1 = (Button) findViewById(R.id.button1); mButton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Message Sent Succesfully !!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.BOTTOM, 10, 80); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); } }); |
Multiple Toast
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| Toast m_currentToast; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showToast("5"); showToast("4"); showToast("3"); showToast("2"); showToast("1"); showToast("Go go go..."); } }); } void showToast(String text) { if (m_currentToast != null) { m_currentToast.cancel(); } LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.root)); TextView text1 = (TextView) layout.findViewById(R.id.text); text1.setText(text); m_currentToast = new Toast(getApplicationContext()); m_currentToast.setDuration(Toast.LENGTH_SHORT); m_currentToast.setView(layout); m_currentToast.setGravity(Gravity.TOP, 10, 180); m_currentToast.show(); } |
No comments:
Post a Comment