Pages

Wednesday, December 7, 2016

Membuat Toast Menampilkan Pesan MessageBox pada Program Android

Selamat pagi dan salam sejahtera. Semoga anda disana di lindungi dan sehat serta bahagia selalu disamping orang tersayang. Pada postingan kali ini, saya akan membagikan tutorial untuk membuat toast atau messagebox yaitu pesan yang muncul beberapa saat dan menghilang di android, ini adalah cara dasar menampilkan toast di android. Okeeyy, langsung aja kita mulai, pertama kali buatlah project baru dengan nama terserah anda. Pada src ubah MainActivity seperti koding dibawah ini.
MainActivity.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.example.test;
 
import org.w3c.dom.Text;
import org.xml.sax.helpers.DefaultHandler;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
EditText nim,nama,alamat;
RadioGroup kel;
RadioButton jk;
Spinner agama;
private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        button = (Button)findViewById(R.id.button1);
        nim = (EditText)findViewById(R.id.editText1);
        nama=(EditText)findViewById(R.id.editText2);
        alamat=(EditText)findViewById(R.id.editText3);
        kel=(RadioGroup)findViewById(R.id.radioGroup1);
        agama=(Spinner)findViewById(R.id.spinner1);
        // add button listener
        button.setOnClickListener(new OnClickListener() {
                         
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            int selectedId = kel.getCheckedRadioButtonId();
            jk=(RadioButton) findViewById(selectedId);
                 Toast.makeText(getApplicationContext(),"NPM : "+ nim.getText()+"\n"+
                 "NAMA : "+nama.getText()+"\n"+"ALAMAT : "+ alamat.getText()+"\n"+
                    "JENIS KELAMIN : "+jk.getText()+
                    "\nAGAMA : "+String.valueOf(agama.getSelectedItem())+"\nData tersimpan...", Toast.LENGTH_LONG).show();
 
            }
        });
 
    }
 
}
Kemudian ubah file activity_main.xml seperti koding dibawah ini.
main_activity.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/editText2"
        android:ems="10" >
 
        <requestFocus />
    </EditText>
 
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="16dp"
        android:ems="10" />
 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignLeft="@+id/textView1"
        android:text="NAMA" />
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="20dp"
        android:text="NPM" />
 
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="22dp"
        android:text="ALAMAT" />
 
    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/radioGroup1"
        android:ems="10" />
 
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="26dp"
        android:text="JENIS KELAMIN" />
 
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignTop="@+id/spinner1"
        android:layout_marginTop="14dp"
        android:text="AGAMA" />
 
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:entries="@array/country_arrays" />
 
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView5"
        android:layout_marginLeft="32dp"
        android:layout_toRightOf="@+id/textView5" >
 
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="LAKI-LAKI" />
 
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PEREMPUAN" />
    </RadioGroup>
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textView5"
        android:layout_marginBottom="30dp"
        android:text="Simpan" />
 
</RelativeLayout

No comments:

Post a Comment