Today we will learn to use Android's Toast notification. Open a new Android Project. Graphics layout of the palate Main.xml file with the following code to the button and drag to create the interface meet Linear layout gravity center of the day. Layout_width and layout_height to wrap_content the button on the other hand. The button's text in a string.xml declaredstring / id showed by the day.
ooeyasinoo Code highlight- Double-click on the code, copy the code to
ooeyasinoo Code highlight- Double-click on the code, copy the code to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" android:gravity= "center" > <Button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/alert_button" /> </ LinearLayout> |
Then Main.java file the following code,
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
| package com.example.android5; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button bt ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.button1); bt.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { Toast toast = Toast.makeText(MainActivity. this , "This Is Toast Notification" , 5000 ); toast.setGravity(Gravity.TOP, 0 , 60 ) ; toast.show(); } }); } } |
Toast in the button OnclickListener who has been active in this way.
Toast toast = Toast.makeText(MainActivity.this, "This Is Toast Notification",5000);
Here are Mainactivity.this indicate the current file. 5000 and the duration time. Gravity then sets of Toast Toast notification being displayed through the show phansanaera. Now click on the notification will show click here.
This was in today's Web using Toast notification.
No comments:
Post a Comment