Displaying a dialog using an activity in android tutorial.
There are times where you need to display a dialog window to get a confirmation from the user. In
this case, you can override the onCreateDialog()protected method defined in the base Activity
class to display a dialog window. The following Try It Out shows you how.
Now, create a new android project.
Edit interface in activity_main.xml file:
<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.demoshowdialog.MainActivity"
>
<Button
android:id="@+id/btnShow_Dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Dialog" />
</RelativeLayout>
Next, the proccess code in MainActivity.java file:
import android.app.Activity;
import
android.app.AlertDialog;
import android.app.Dialog;
import
android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class MainActivity extends Activity {
private Button btnShow_Dialog;
CharSequence[] items = { "Asus", "Acer", "Dell", "Vaio" };
boolean[] bool = new boolean[items.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShow_Dialog = (Button)
findViewById(R.id.btnShow_Dialog);
btnShow_Dialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(0);
}
});
}
@Override
protected Dialog
onCreateDialog(int id) {
// TODO Auto-generated
method stub
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Show Dialog
Demo")
.setPositiveButton("Yes",new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog,int which) {
// TODO Auto-generated
method stub
}
})
.setNegativeButton("No",new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO Auto-generated
method stub
}
})
.setMultiChoiceItems(items, bool,new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void
onClick(DialogInterface dialog,int which, boolean isChecked) {
// TODO Auto-generated
method stub
}
}).create();
}
return null;
}
}
Explain:To display a dialog, you first override the onCreateDialog() method in the Activity class
@Override
protected
Dialog onCreateDialog(int id) {
...
}
This method is called when you call the showDialog() method
This method is called when you call the showDialog() method
btnShow_Dialog = (Button)
findViewById(R.id.btnShow_Dialog);
btnShow_Dialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(0);
}
});
Download source code: updating...
How to earn money with a casino - JTM Hub
Casino is the place 제주도 출장안마 to get free 보령 출장안마 games to play, and you could spend 안산 출장안마 some money to make some 원주 출장마사지 money. Here are some useful information and tips 구미 출장마사지 for getting started.