Learn Python


Click to Learn Python3
Showing posts with label alertdialog. Show all posts
Showing posts with label alertdialog. Show all posts

Wednesday, March 16, 2011

How to display Custom Dialog and designing a page without xml layout is given with example?

Custom Dialog appears like this



First we can see how we are going to display a button without using the xml layout,


Java

Button show = new Button(this);
show.setText("Click to view a Custom Dialog");
/** Setting FILL_PARENT for the width and height of the button */
show.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
/** Without the layout xml we are going to add the button to the screen */
setContentView(show);


Instead of using the xml layout in setContentView() we are using the new created Button's variable, likewise if we are using the linearlayout's variable means, we can display the component without using the layout xml.

In this example we used the layout params


Java

show.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


Actually this is used for setting the layout width and height for the button.

Now we can come to showing Custom Dialog, for that we need to create a class which exetends Dialog, the class will be like this


CustomizeDialog.java

package com.custom.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;

public CustomizeDialog(Context context) {
super(context);
/** It will hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v == close)
dismiss();
}

}


Then in the main class CustomDialog.java we need to call the CustomizeDialog.java class to show the custom dialog, like this


Java

CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this);
customizeDialog.show();


The full code of the CustomDialog.java will be like this


CustomDialog.java

package com.custom.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Gallery;
import android.view.WindowManager.LayoutParams;

public class CustomDialog extends Activity {
/** Called when the activity is first created. */
private Button show;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

show = new Button(this);
show.setText("Click to view a Custom Dialog");
/** Setting FILL_PARENT for the width and height of the button */
show.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
/** Without the layout xml we are going to add the button to the screen */
setContentView(show);
show.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this);
customizeDialog.show();
}
});


}
}




The full source code

Download source


For AlertDialog you can see this blog http://android-codes-examples.blogspot.com/2011/03/how-to-display-alertdialog-and.html


Have a good day.

Tuesday, March 15, 2011

How to display AlertDialog and selection in Alert Dialog and selection with radio buttons with example?

We are going to see the Alert Dialog in detail.

First we going to see how to use a normal AlertDialog

The below code is enough to call AlertDialog,


Java
AlertDialog.Builder alert = new AlertDialog.Builder(ShowDialog.this);

alert.setTitle("This is Alert Dialog");
alert.setMessage("Sample alert dialog from http://www.android-codes-examples.blogspot.com");
alert.setIcon(R.drawable.icon);
alert.setPositiveButton("Yes",
 new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
   Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
  }
 });
alert.setNegativeButton("No",
 new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
   Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
  }
 });

alert.show();


Next we are going to see how to call a AlertDialog with ListView for selection purpose


Java
final CharSequence[] items = {"India", "US", "UK", "Australia"};

AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView");
builder.setIcon(R.drawable.icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

alert.show();


Next we are going to see how to call a AlertDialog with ListView with Radio button for selection purpose


Java
final CharSequence[] items = {"India", "US", "UK", "Australia"};

AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView and Radio button");
builder.setIcon(R.drawable.icon);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});

builder.setPositiveButton("Yes",
 new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
   Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
  }
 });
builder.setNegativeButton("No",
 new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
   Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
  }
 });
AlertDialog alert = builder.create();
alert.show();


A sample with all the three examples of alertdialog is follows:


ShowDialog.java
package com.display.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ShowDialog extends Activity {
/** http://www.android-codes-examples.blogspot.com/ */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 Button alert = (Button) findViewById(R.id.alert);
 Button customList = (Button) findViewById(R.id.customList);
 Button customListRadio = (Button) findViewById(R.id.customListRadio);
 
 alert.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
  AlertDialog.Builder alert = new AlertDialog.Builder(ShowDialog.this);

  alert.setTitle("This is Alert Dialog");
  alert
    .setMessage("Sample alert dialog from http://www.android-codes-examples.blogspot.com");
  alert.setIcon(R.drawable.icon);
  alert.setPositiveButton("Yes",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {
      Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
     }
    });
  alert.setNegativeButton("No",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {
      Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
     }
    });

  alert.show();
 }
 });
 
 customList.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
  final CharSequence[] items = {"India", "US", "UK", "Australia"};

  AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
  builder.setTitle("Alert Dialog with ListView");
  builder.setIcon(R.drawable.icon);
  builder.setItems(items, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int item) {
   Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
      }
  });
  AlertDialog alert = builder.create();

  alert.show();
 }
 });
 
 customListRadio.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
  final CharSequence[] items = {"India", "US", "UK", "Australia"};

  AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
  builder.setTitle("Alert Dialog with ListView and Radio button");
  builder.setIcon(R.drawable.icon);
  builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int item) {
   Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
      }
  });
  
  builder.setPositiveButton("Yes",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {
      Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
     }
    });
  builder.setNegativeButton("No",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {
      Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
     }
    });
  AlertDialog alert = builder.create();
  alert.show();
 }
 });
}
}


Likewise for multiple choice in the alertdialog we need to use this line


Java
new DialogInterface.OnMultiChoiceClickListener() {
    public void onClick(DialogInterface dialog, int whichButton,
     boolean isChecked) {
          /** ... */
    }
});




The full source code

Download source


The different types of AlertDialog images will be like this





For Custom Dialog you can see this blog http://android-codes-examples.blogspot.com/2011/03/how-to-display-custom-dialog-and.html

Have a good day.