Learn Python


Click to Learn Python3

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.

1 comment:

  1. You can also visit the http://android-codes-examples.blogspot.com/2011/04/animated-customized-popup-transparent.html for animated dialog

    ReplyDelete