How to set the height and width of the Default Alert Dialog - android

I have a problem in setting the height and width of an Alert Dialog, below is my Code that i have written in OnCreateDialog() method:
AlertDialog dialog = new AlertDialog.Builder(context ,AlertDialog.THEME_HOLO_LIGHT)
.setTitle("Title")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 0:
//some Code
break;
case 1:
//some Code
break;
}
}
})
.create();
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.x = 80; //x position
WMLP.y = -100; //y position
WMLP.height = 100;
WMLP.width = 100;
dialog.getWindow().setAttributes(WMLP);
//OR
dialog.getWindow().setLayout(100, 100);
Kindly tell me what is the problem here in the Code. Thanks

You need to set the parameters for Height & Width after alertDialog.show();
So you need to call,
alertDialog.getWindow().setLayout(100, 100);
After calling
alertDialog.show();
For more check this answer.

Related

How can I set maximum height to Dialog?

I wanna set maximum height of dialog. Not custom height like that set by dp or px. I wanna set the greatest possible height to dialog relatively current device screen size.
You can not set max height directly. Just an alternate, you can reset height if its height is greater than maximum height you want to set.
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
int dialogWidth = lp.width;
int dialogHeight = lp.height;
if(dialogHeight > MAX_HEIGHT) {
d.getWindow().setLayout(dialogWidth,MAX_HEIGHT);
}
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_example);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
//lp.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 330/*height value*/, getResources().getDisplayMetrics()); for custom height value
dialog.getWindow().setAttributes(lp);
dialog.show();
I think this can solve it. I've added two way one is for set dialog height with match parent property and second one is for setting height with custom value
may be addOnLayoutChangeListener() is usefull for you.
you can add it before or after dialog.show()
this is my code :
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View view = LayoutInflater.from(this).inflate(R.layout.custom_alertdialog, null);
builder.setMessage("TestMessage xxxxxxx");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view); //在setView之前调用builder的原有设置控件方法时,能展示设置的控件,之后设置的则不展示!!
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_bk_cnoneralert));
//builder.show(); //用这个的话,背景并不会改变,依旧是默认的
dialog.show(); //必须用这个show 才能显示自定义的dialog window 的背景
//这种设置宽高的方式也是好使的!!!-- show 前调用,show 后调用都可以!!!
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
int oldRight, int oldBottom) {
int height = v.getHeight();
int contentHeight = view.getHeight();
LogUtils.e("高度", height + " / " + " / " + contentHeight);
int needHeight = 500;
if (contentHeight > needHeight) {
//注意:这里的 LayoutParams 必须是 FrameLayout的!!
//NOTICE : must be FrameLayout.LayoutParams
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
needHeight));
}
}
});
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay()
.getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels);
int height = (int) (displaymetrics.heightPixels);
d.getWindow().setLayout(width,height);
d.show();
Where d is dialog. This code sets the dialog to full screen.

Limit the width of an EditText within an AlertDialog with Layout Params?

The Question:
How can we limit the width of an EditText contained within an AlertDialog via LayoutParams?
The Assumptions:
I have an AlertDialog, whose width is set to be rather large;
Within this dialog lives an EditText, whose width should be ~1/3 of its container;
The Minor Details:
The EditText has the following types:
TYPE_CLASS_TEXT
TYPE_TEXT_FLAG_CAP_CHARACTERS
TYPE_TEXT_FLAG_NO_SUGGESTIONS
The height/width of the AlertDialog is set in pixels, scaled via density (see the code);
The Confusion:
Given the following code, why does the EditText end up spanning the entire width of the AlertDialog?
The Code:
Note that the EditText is set to {100,100} via LayoutParams, while the AlertDialog is set to {900,500}
private void show_activation_prompt()
{
// =============================================================
// ======== create EditText which filters text =================
// =============================================================
// filter for alphanumerics, insert dashes during entry, capitalize, and turn off suggestions
final TextWatcher textEditorWatcher = new SafeTextWatcher(); //custom
final LayoutParams lparams = new LayoutParams(100,100);
final EditText edittext= new EditTextPersist(mContext);
edittext.addTextChangedListener(textEditorWatcher);
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
);
edittext.setLayoutParams(lparams);
//edittext.setEms(10); //doesn't work either
// =============================================================
// ======== create alert dlg, set btn callbacks ================
// =============================================================
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setPositiveButton("Upgrade", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String activationKey = edittext.getText().toString();
on_key_entered(activationKey);
}
} );
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
post_activation_result(QlmLicense.ACTIVATION_INVALID);
}
} );
// =============================================================
// ======== configure the AlertDialog/EditText =================
// =============================================================
int nWidth, nHeight;
final float scale = mContext.getResources().getDisplayMetrics().density;
nWidth = (int) (900 * scale + 0.5f);
nHeight = (int) (500 * scale + 0.5f);
builder.setView(edittext)
.setMessage("Enter an Activation Key:")
.setTitle("Upgrade From Trial Version")
.setCancelable(false)
.show()
.getWindow()
.setLayout(nWidth,nHeight);
}
I had simple answer for your question to apply LayoutParams for Edittext in AlertDialog by change LayoutParams into FrameLayout.LayoutParams and call edittext.setLayoutParams(lparams); after calling dialog.show():
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int nWidth, nHeight;
final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
nWidth = (int) (900 * scale + 0.5f);
nHeight = (int) (500 * scale + 0.5f);
final FrameLayout.LayoutParams lparams = new FrameLayout.LayoutParams(100,100);
final EditText edittext= new EditText(this);
//edittext.addTextChangedListener(textEditorWatcher);
edittext.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
);
// =============================================================
// ======== create alert dlg, set btn callbacks ================
// =============================================================
builder.setPositiveButton("Upgrade", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String activationKey = edittext.getText().toString();
//on_key_entered(activationKey);
Toast.makeText(getApplicationContext(), activationKey, Toast.LENGTH_LONG).show();
}
} );
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//post_activation_result(QlmLicense.ACTIVATION_INVALID);
}
});
builder.setView(edittext)
.setMessage("Enter an Activation Key:")
.setTitle("Upgrade From Trial Version")
.setCancelable(false)
.show()
.getWindow()
.setLayout(nWidth, nHeight);
//Change setLayoutParams to here
edittext.setLayoutParams(lparams);

LinearLayout is slow to move, if I load one image in ImageView

I have a list, it captured moving finger, and you can delete an item by sliding your finger
if the image is not loaded in my container, with ImageView.setImage(), the container moves correctly.
However, if I loaded one image the container moves slowler.
Why is this happening?
Attach images below.
1.moves correctly
not moves correctly
always moves the container from left to rigth
Why is it?
Thanks.
UPDATE 19/01/2014
i reduced bitmap to 150 kb and continue equal.
leave my code
objelementos.lnyDatosCliente.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent event) {
final CCliente objcCCliente = lstLista.get(objelementos.posicion);
CAnimaciones objAnmanim = new CAnimaciones();
Display pantalla = afrmGstionClientes.getActivity().getWindowManager().getDefaultDisplay();
final int ancho = pantalla.getWidth();
switch ( event.getAction()) {
case MotionEvent.ACTION_DOWN:
fdYPrimeroPulsado = event.getRawY();
fdXPulsadoInicio = event.getRawX();
fdXUltimaPulsado = event.getRawX();
iTamanioLnyPulsado =0;
break;
case MotionEvent.ACTION_MOVE:
float fdXPulsado = event.getRawX();
float fdXMovimiento = fdXPulsado- fdXUltimaPulsado;
objelementos.lnyDatosCliente.setVisibility(4);
objelementos.lnyDatosClienteToMove.setVisibility(0);
objelementos.lnyDatosClienteToMove.setX(fdXMovimiento);
iTamanioLnyPulsado =1;
break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL:
float fdXPulsad = event.getRawX();
float xmov =fdXPulsad- fdXUltimaPulsado;
fdYUltimoPulsado = event.getRawY();
int difx = (int)fdXPulsad- (int)fdXPulsadoInicio;
if (difx>120){
ObjectAnimator animaciion = objAnmanim.CrearAnimacion(objelementos.lnyDatosClienteToMove, xmov, ancho+10, "x", 85);//(xFin-20, ancho+10, 200) ;
animaciion.start();
new Handler().postDelayed(new Runnable() {
public void run() {
float num = objcCCliente.getposicionXInicio();
AlertDialog.Builder builder = new AlertDialog.Builder(
getContext());
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("!AVISO!");
builder.setMessage("¿Estas seguro de eliminar el cliente?")
.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CustomAdapterListadoClientes.this.lstLista.remove(objelementos.posicion);
CustomAdapterListadoClientes.this.notifyDataSetChanged();
dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
vol(ancho-10, objelementos.lnyDatosCliente.getX(), objelementos.lnyDatosClienteToMove, 180, objelementos.lnyDatosCliente);
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}, animaciion.getDuration());
}else{
if (iTamanioLnyPulsado==0){
float dify = fdYPrimeroPulsado -fdYUltimoPulsado;
if (dify<0){
dify*=-1;
}
if (dify<10){
float fddondeesta = objelementos.lnyDatosCliente.getY();
ObjectAnimator animaciion = objAnmanim.CrearAnimacion(objelementos.lnyDatosCliente, fddondeesta-1, fddondeesta, "x",50);
animaciion.start();
Cnavegar objNavegar = new Cnavegar();
AfrmHigthModificationCliente afrmModificacion = new AfrmHigthModificationCliente(true, objcCCliente,afrmGstionClientes);
objNavegar.RemplazarFragmento(R.id.rtlAltas, afrmModificacion, "AfrmHigthModificationCliente", afrmGstionClientes.getActivity().getSupportFragmentManager());
}
}else{
if (difx>0){
vol(xmov, objelementos.lnyDatosCliente.getX(), objelementos.lnyDatosClienteToMove, 180, objelementos.lnyDatosCliente);
}else{
vol(xmov, objelementos.lnyDatosCliente.getX(), objelementos.lnyDatosClienteToMove, 180, objelementos.lnyDatosCliente);
}
}
}
break;
}
return true;
}
});
Are you using ViewHolder pattern?
Are you loading your images on another thread?
For load images I use Picasso library.
solve my problem of slow , which resided not in the size of the image
I used two linear layout changing your visibility associated with this adapter to change it to visible or invisiblo gone.
he adapter had to rewrite the code again and has this was because my problem.
now only work with linerar layout unchanged visibility, position and only my application I run to perfection
thanks for your help everyone

How Do I Position Custom Dialog at specific Coordinate?

I'm a noob to android development and I am trying to figure out how to display the NewQuickAction3D popup dialog at a specific coordinate in a view. I am integrating the popup with this tutorial. Essentially, I want to use the popup dialog to display data where the users touch instead painting on the canvas using "infoview" Currently, the popup displays at the top&center of the view that I anchor it to. How can i make it display a particular coordinate? Any help is greatly appreciated.
MY CODE
public void updateMsg(String t_info, float t_x, float t_y, int t_c){
infoView.updateInfo(t_info, t_x, t_y, t_c); //Infoview paints to on a specific coordinate
quickAction.show(infoView); //How do I use the t_x & t_y coordinates here instead of just anchoring infoview
EDIT
public void updateMsg(String t_info, float t_x, float t_y, int t_c){
infoView.updateInfo(t_info, t_x, t_y, t_c);
WindowManager.LayoutParams wmlp = quickAction.getWindow().getAttributes(); //Error here getting window attributes
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 100; //x position
wmlp.y = 100; //y position
quickAction.show(infoView);
}
Override onTouch() of your view
AlertDialog dialog;
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
showDialog(); // display dialog
break;
case MotionEvent.ACTION_MOVE:
if(dialog!=null)
dialog.dismiss();
// do something
break;
case MotionEvent.ACTION_UP:
// do somethig
break;
}
return true;
}
public void showDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(FingerPaintActivity.this);
dialog = builder.create();
dialog.setTitle("my dialog");
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 100; //x position
wmlp.y = 100; //y position
dialog.show();
}
Even to draw user touches the screen then also dialog is displayed. so dismiss dialog in on move.

How to put the alert dialog in the topmost part of the view

I am making an alert Dialog and want to show it at the topmost part of the screen(its height should start from the topmost part).How can i do that?This is the code:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
m_rb10Minutes = new RadioButton(this);
m_rb10Minutes.setText("10 minutes");
m_rb20Minutes = new RadioButton(this);
m_rb20Minutes.setText("20 minutes");
m_rb30Minutes = new RadioButton(this);
m_rb30Minutes.setText("30 minutes");
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
linearLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
linearLayout.setOrientation(1);
linearLayout.addView(m_rb10Minutes);
linearLayout.addView(m_rb20Minutes);
linearLayout.addView(m_rb30Minutes);
alertTimer = alertDialog.create();
alertTimer.setView(linearLayout);
alertTimer.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = alertTimer.getWindow().getAttributes();
WMLP.x = 0; //x position
WMLP.y = 0; //y position
alertTimer.getWindow().setAttributes(WMLP);
alertTimer.show();
use the following code
private CharSequence[] items = {"Set as Ringtone", "Set as Alarm"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item == 0) {
} else if(item == 1) {
} else if(item == 2) {
}
}
});
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.x = 100; //x position
WMLP.y = 100; //y position
dialog.getWindow().setAttributes(WMLP);
dialog.show();

Categories

Resources