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);
Related
How to add a switch button in the title bar of alert dialog? As an example see the Wi-Fi options screenshot. As you can see there is a switch toggle button inside the title in Wi-Fi options. Is it possible to create a custom Alert Dialog to add a switch toggle button inside the title bar of the Alert Dialog? The code given below is for the screen shot number 2 and I have marked in it where I want it to be added. Please help me and put me in the right direction.
alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Recording Timer");
LinearLayout LL = new LinearLayout(context);
LL.setFocusable(true);
LL.setFocusableInTouchMode(true);
LL.setOrientation(LinearLayout.HORIZONTAL);
final TextView secondsTextView = new TextView(context);
final TextView minutesTextView = new TextView(context);
final TextView hoursTextView = new TextView(context);
secondsTextView.setText("seconds");
minutesTextView.setText("minutes");
hoursTextView.setText("hours");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
lp.setMargins(0, 15, 0, 0);
LL.setLayoutParams(lp);
LinearLayout.LayoutParams lp11 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp11.setMargins(79, 0, 0, 0);
LL.addView(hoursTextView, lp11);
LinearLayout.LayoutParams lp22 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp22.setMargins(107, 0, 0, 0);
LL.addView(minutesTextView, lp22);
LinearLayout.LayoutParams lp33 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp33.setMargins(98, 0, 0, 0);
LL.addView(secondsTextView, lp33);
LinearLayout LL1 = new LinearLayout(context);
LL1.setOrientation(LinearLayout.HORIZONTAL);
secondsPicker = new NumberPicker(context);
minutesPicker = new NumberPicker(context);
hoursPicker = new NumberPicker(context);
secondsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal==0 && minutesPicker.getValue()==0 && hoursPicker.getValue()==0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
minutesPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal==0 && secondsPicker.getValue()==0 && hoursPicker.getValue()==0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
hoursPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal==0 && minutesPicker.getValue()==0 && secondsPicker.getValue()==0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
secondsPicker.setMaxValue(59);
secondsPicker.setMinValue(0);
minutesPicker.setMaxValue(59);
minutesPicker.setMinValue(0);
hoursPicker.setMaxValue(23);
hoursPicker.setMinValue(0);
if (timedRecordingIsOn || timedRecordingDialogOpened) {
secondsPicker.setValue(secondsNumberPickerInt);
minutesPicker.setValue(minutesNumberPickerInt);
hoursPicker.setValue(hoursNumberPickerInt);
}
timedRecordingDialogOpened = true;
LinearLayout.LayoutParams lp110 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp110.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
lp110.setMargins(0, 40, 0, 0);
LL1.setLayoutParams(lp110);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp1.setMargins(52, 0, 0, 0);
LL1.addView(hoursPicker, lp1);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp2.setMargins(62, 0, 0, 0);
LL1.addView(minutesPicker, lp2);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp3.setMargins(72, 0, 0, 0);
LL1.addView(secondsPicker, lp3);
RelativeLayout relativeLayout1 = new RelativeLayout(context);
relativeLayout1.addView(LL);
relativeLayout1.addView(LL1);
alertDialogBuilder.setView(relativeLayout1);
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
secondsNumberPickerInt = secondsPicker.getValue();
minutesNumberPickerInt = minutesPicker.getValue();
hoursNumberPickerInt = hoursPicker.getValue();
timeRecordingInMilliSeconds = (hoursPicker.getValue() * 3600000) + (minutesPicker.getValue() * 60000) + (secondsPicker.getValue() * 1000);
timedRecordingIsOn = true;
timedRecordingDialogOpened = false;
timedRecordingTextView.setText("Timer set for: " + (hoursPicker.getValue() == 0 ? "00" : hoursPicker.getValue()) + ":" + (minutesPicker.getValue() == 0 ? "00" : minutesPicker.getValue()) + ":" + (secondsPicker.getValue() == 0 ? "00" : secondsPicker.getValue()));
timedRecordingTextView.setVisibility(View.VISIBLE);
// button.performClick();
break;
case DialogInterface.BUTTON_NEGATIVE:
if (timedRecordingIsOn) {
timedRecordingIsOn = false;
timedRecordingTextView.setVisibility(View.INVISIBLE);
}
timeRecordingInMilliSeconds = 0;
secondsNumberPickerInt = 0;
minutesNumberPickerInt = 0;
hoursNumberPickerInt = 0;
timedRecordingDialogOpened = false;
break;
}
}
};
alertDialogBuilder.setPositiveButton("SET TIMER", dialogClickListener);
if (timedRecordingIsOn) {
alertDialogBuilder.setNegativeButton("CANCEL TIMER", dialogClickListener);
} else {
alertDialogBuilder.setNegativeButton("CANCEL", dialogClickListener);
}
alertDialog1 = alertDialogBuilder.create();
alertDialog1.setOnCancelListener(
new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) { //When you touch outside of dialog bounds, the dialog gets canceled and this method executes.
timeRecordingInMilliSeconds = 0;
secondsNumberPickerInt = 0;
minutesNumberPickerInt = 0;
hoursNumberPickerInt = 0;
timedRecordingDialogOpened = false;
}
}
);
alertDialog1.show();
LL.requestFocus();
secondsPicker.clearFocus();
if(!timedRecordingIsOn && savedInstanceState!=null) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
if(savedInstanceState!=null) {
if(secondsNumberPickerInt==0 && minutesNumberPickerInt ==0 && hoursNumberPickerInt == 0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
Note: Follow any one of the method & MainActivity.this replace with your context
If U need java code follow this
final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
final TextView titleTextView = new TextView(MainActivity.this);
titleTextView.setText("Title");
titleTextView.setTextColor(Color.RED);
Switch switchval=new Switch(MainActivity.this);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
switchval.setLayoutParams(params);
RelativeLayout relative=new RelativeLayout(MainActivity.this);
relative.addView(titleTextView);
relative.addView(switchval);
dialog.setCustomTitle(relative);
/*Your Numberpickerview example is following*/
//dialog.setView(yourcustomview);
dialog.create().show();
--------------------------------------------------------------------------------
Type XML: if u want xml code follow this
final AlertDialog.Builder dialog1 = new AlertDialog.Builder(MainActivity.this);
dialog1.setCancelable(false);
dialog1.setCustomTitle(getLayoutInflater().inflate(R.layout.btn_share,null));
//dialog1.setView(/*Your number picker view*/);
dialog1.create().show();
btn_share.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:textColor="#color/colorAccent"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"/>
</RelativeLayout>
Add switch button in Alert Dialog title bar in Android
You can use a custom layout which includes both title and body and then hide the default implementation.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
You can hide the title of a dialog using:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
My suggestion for this is to create layout file for Custom dialog and do like this it will be helpful.
final Dialog dialog = new Dialog(getContext());
dialog.setCancelable(false);
dialog.setContentView(R.layout.your_dialog_layout);
dialog.setTitle("Dialog Title");
//views inside your layout declare and cast here
//e.g. final Button btn = (Button)dialog.findViewById(R.id.btn);
//after all view casting and onClickListeners show your dialog
dialog.show();
try to implement one layout file and inflate when alert dialog is open and give toggle button in this layout
final Dialog dialog = new Dialog(getContext());
dialog.setCancelable(false);
dialog.setContentView(R.layout.your_dialog_layout);
dialog.setTitle("Dialog Title");
//views inside your layout declare and cast here
//e.g. final Button btn = (Button)dialog.findViewById(R.id.btn);
//after all view casting and onClickListeners show your dialog
dialog.show();
I am using a .java which implements surfaceView to draw in the screen with canvas instead of using a .xml layout file, but I would like to know if somehow at some point (when what i do in the view is completed) its possible to associate this view with the layout file or to call a button or alerdialog.
To be clearer, something like when you win or fail in a game to show up an alerDialog like "you lose" or similar.
Main_Activity looks like:
public class Main extends Activity {
activity_layout_animation animation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
animation = new activity_layout_animation(this);
setContentView(animation);
}
#Override
protected void onPause(){
super.onPause();
animation.pause();
}
#Override
protected void onResume(){
super.onResume();
animation.resume();
}
Some piece of code of view file:
public class activity_layout_animation extends SurfaceView implements Runnable {
boolean CanDraw = false
public activity_layout_animation(Context context){
super(context);
surfaceHolder = getHolder();
}
#Override
public void run(){
while(CanDraw){
if ( !surfaceHolder.getSurface().isValid()){
continue;
}
}
}
edit: I can add somethig like this:
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
but it just works in public activity_layout_animation(Context context){ and i want to add it in run()
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = activity.getLayoutInflater();
final View dialogCoordinate = inflater.inflate(R.layout.show_coordinates, null);
dialogBuilder.setCancelable(false);
dialogBuilder.setView(dialogCoordinate);
ImageView imgShowCoordinates = (ImageView) dialogCoordinate.findViewById(R.id.imgShowCoordinates);
imgShowCoordinates.setImageResource(R.drawable.whitetrans);
// int width = dmsUtility.getScreenSize(activity)[0];
// int height = dmsUtility.getScreenSize(activity)[1];
Log.d("Coordinate", "showCoordinates: "+arrayList.get(pos).getComment());
if (arrayList.get(pos).getComment() != null && !arrayList.get(pos).getComment().isEmpty()
&& !arrayList.get(pos).getComment().equals("null")) {
String[] parts1 = arrayList.get(pos).getComment().split("[\\s\\,]+");
Log.d(TAG, "getBody: " + arrayList.get(pos).getComment());
float[] numbers = new float[parts1.length];
for (int p = 0; p < numbers.length; p++) {
numbers[p] = Math.round(Float.parseFloat(parts1[p]) * 1000) / 1000f;
}
bmp = ((BitmapDrawable) imgShowCoordinates.getDrawable()).getBitmap();
if (bmp != null && !bmp.isRecycled()) {
Bitmap tempBitmap = Bitmap.createScaledBitmap(bmp, 300, 300, true);
tempCanvas = new Canvas(tempBitmap);
canW = tempBitmap.getWidth();
canH = tempBitmap.getHeight();
for (int q = 0; q < numbers.length; q = q + 4) {
float x1 = (canW / 300) * numbers[0 + q];
if (numbers.length > q + 2) {
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLUE); // Text Color
paint.setStrokeWidth(1f); // Text Size
paint.setAntiAlias(true);
float y1 = (canH / 300) * numbers[1 + q];
float x2 = (canW / 300) * numbers[2 + q];
float y2 = (canH / 300) * numbers[3 + q];
tempCanvas.drawLine(x1, y1, x2, y2, paint);
}
}
}
}
dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
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.
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();
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.