1.
Membuat aplikasi Android pertama dengan
menggunakan eclipse. Ketika membuka aplikasi tampil kalimat dan ada radio
button yang harus diisi, setelah di klik oke maka akan menampilkan Window
Dialog untuk mendapat konfirmasi dari user.
2.
Source Code
activity_hello_world.xml
<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="com.example.helloworld.HelloWorldActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<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="17dp"
android:text="Rozaq Akbar, How Are You?"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="92dp"
android:text="OK" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/textView1"
android:text="Fine, 125150207111101" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
android:text="Not good, -" />
</RelativeLayout>

HelloWorldActivity.java
package
com.example.helloworld;
import
android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import
android.view.MenuItem;
import
android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
public class HelloWorldActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
Button
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new
View.OnClickListener() {
public void onClick(View v){
}
});
}
protected Dialog
onCreateDialog(int id){
switch (id){
case 0:
return new AlertDialog.Builder(this)
.setTitle("Welcome, Rozaq Akbar.. Your
NIM is 125150100111016")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void
onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
"OK!",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new
DialogInterface.OnClickListener()
{
public void
onClick(DialogInterface dialog, int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel!",
Toast.LENGTH_SHORT).show();
}
})
.create();
}
return null;
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world, menu);
return true;
}
@Override
public boolean
onOptionsItemSelected(MenuItem item) {
// Handle action
bar item clicks here. The action bar will
// automatically
handle clicks on the Home/Up button, so long
// as you specify a
parent activity in AndroidManifest.xml.
int id =
item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
3.
Screenshot hasil

Tampilan awal setelah aplikasi helloworld
dibuka

Tampilan setelah klik ok, menampilkan
window dialog sebagai konfirmasi ok atau cancel.

Apabila window dialog di klik ok maka akan
tampil toast ok! seperti gambar di bawah ini.

Apabila window dialog di klik Cancel maka
akan tampil toast Cancel! seperti gambar di bawah ini.

Tidak ada komentar:
Posting Komentar