One of the Important things in android that one uses are the Alert Dialog, and these are also very easy to use and make.
Some very basic codes for building an Alert Dialog is.
AlertDialog.Builder builder = new AlertDialog.builder(this);
builder.setMessage(R.string.Tittle) // title for the alert dialog
.setPositiveButton(R.string.Button1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set a action or Intent
}
.setNegetiveButton(R.string.Button2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set a cancal button
}
.setNeturalButton(R.string.Button3, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set another action or Intent
});
builder.show();
and this code can be used with any button which may have onClick, or by simply using the onClick with a button or any thing that you may feel apropriate in the onCreate method in java class.
also any one button can also be skipped while placing the alert dialog. but not two buttons of same functions can be placed i.e two positive or two negative buttons simultaneously.
Apart from this the custom Dialog are also very useful which can be used as custom button and text view as one may wish. For this first the main thing that one has to build is a custom layout xml file which can be used as alert dialog, like as below.
custom_dialog.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_height="wrap_content"
android:layout_weight="wrap_content"
android:id="@+id/button1"
android:text="First Button"/>
<Button
android:layout_height="wrap_content"
android:layout_weight="wrap_content"
android:id="@+id/button2"
android:text="Second Button"/>
<Button
android:layout_height="wrap_content"
android:layout_weight="wrap_content"
android:id="@+id/cancel"
android:text="Cancel"/>
</LinearLayout>
After the custom dialog is build the next step is to build the dialog in java.
MainActivity.java
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Tittle")
Button bCancel = (Button) dialog.findViewById(R.id.cancel);
bCancel.setOnClickListener(new View,OnClickListener(){
@Override
public void onClick(View v) {
dialog,dismiss();
}
});
dialog.findViewById(R.id.button1)
.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Do anything
});
dialog.findViewById(R.id.button2)
.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Do some other thing or same thing depends on you
});
dialog.show();
And this code can be putted into any button that may be clicked or any other thing which has onClick property. And this one is custom build.
Some very basic codes for building an Alert Dialog is.
AlertDialog.Builder builder = new AlertDialog.builder(this);
builder.setMessage(R.string.Tittle) // title for the alert dialog
.setPositiveButton(R.string.Button1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set a action or Intent
}
.setNegetiveButton(R.string.Button2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set a cancal button
}
.setNeturalButton(R.string.Button3, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// set another action or Intent
});
builder.show();
and this code can be used with any button which may have onClick, or by simply using the onClick with a button or any thing that you may feel apropriate in the onCreate method in java class.
also any one button can also be skipped while placing the alert dialog. but not two buttons of same functions can be placed i.e two positive or two negative buttons simultaneously.
Apart from this the custom Dialog are also very useful which can be used as custom button and text view as one may wish. For this first the main thing that one has to build is a custom layout xml file which can be used as alert dialog, like as below.
custom_dialog.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_height="wrap_content"
android:layout_weight="wrap_content"
android:id="@+id/button1"
android:text="First Button"/>
<Button
android:layout_height="wrap_content"
android:layout_weight="wrap_content"
android:id="@+id/button2"
android:text="Second Button"/>
<Button
android:layout_height="wrap_content"
android:layout_weight="wrap_content"
android:id="@+id/cancel"
android:text="Cancel"/>
</LinearLayout>
After the custom dialog is build the next step is to build the dialog in java.
MainActivity.java
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Tittle")
Button bCancel = (Button) dialog.findViewById(R.id.cancel);
bCancel.setOnClickListener(new View,OnClickListener(){
@Override
public void onClick(View v) {
dialog,dismiss();
}
});
dialog.findViewById(R.id.button1)
.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Do anything
});
dialog.findViewById(R.id.button2)
.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Do some other thing or same thing depends on you
});
dialog.show();
And this code can be putted into any button that may be clicked or any other thing which has onClick property. And this one is custom build.
No comments:
Post a Comment