dialog not setting layout params - android

using a dialog as a window I tried to set its layout as
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.x = 120;
lp.y = 120;
lp.width = 800;
lp.height = 450;
Dialog dialog = new Dialog(cxt);
dialog.getWindow().setAttributes(lp);
dialog.setContentView(vFirst);
dialog.show();
where
View vFirst = inflater.inflate(R.layout.popup, container, false);
which is inside a button click,but it is not setting it.

I think you should use this
dialog.getWindow().setLayout(800, 450);
This will set the dialog window size.

public class Myclass extends Dialog
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dialogWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
// contents here
</LinearLayout>

According to documentation: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html,
X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.
Y position for this window. With the default gravity it is ignored. When using TOP or BOTTOM it provides an offset from the given edge.
So to make x and y work you should set Gravity attribute before. For example:
lp.gravity = Gravity.TOP;
lp.gravity = Gravity.LEFT;
lp.y = 120;
lp.x = 120;

//dialog width and height are set according to screen, you can take you want
Dialog myDialog = new Dialog(context, R.style.CustomTheme);
myDialog.setContentView(R.layout.share_post_dialog);
int[] widthHeight = getDeviceWidthAndHeight(context);
int dialogHeight = widthHeight[1] - widthHeight[1] / 4;
int dialogWidth = widthHeight[0] - widthHeight[0] / 5;
WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
params.width= dialogWidth;
params.height = dialogHeight;
myDialog.getWindow().setAttributes((WindowManager.LayoutParams) params);
myDialog.show();
//This is used to get height width of screen
#SuppressWarnings("deprecation")
public static int[] getDeviceWidthAndHeight(Context context) {
int widthHeght[] = new int[2];
int measuredWidth = 0;
int measuredHeight = 0;
WindowManager w = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
} else {
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
}
return widthHeght;
}

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.

How to programmatically add multiple views at the bottom of a layout file without them overlapping

I have successfully added a new view to the bottom of my layout using the resources on Stack Exchange and other. However, if I try to add a second view programmatically, the two views appear to overlap.
This code block successfully adds a new view to my layout file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
// First view
View playControlsPanelMinimized = new View(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
final float scale = activityMain.getContext().getResources().getDisplayMetrics().density;
float dps = 10;
int pixels = (int) (dps * scale + 0.5f);
params.height = pixels;
playControlsPanelMinimized.setLayoutParams(params);
playControlsPanelMinimized.setBackgroundColor(Color.parseColor("#00FFFF"));
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
activityMain.addView(playControlsPanelMinimized);
However, when I add a second view by simply repeating the code with new variables, I see there is an overlap behavior, to the effect that I only see the first view.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout activityMain = (RelativeLayout)findViewById(R.id.activity_main);
// First view
View playControlsPanelMinimized = new View(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
final float scale = activityMain.getContext().getResources().getDisplayMetrics().density;
float dps = 10;
int pixels = (int) (dps * scale + 0.5f);
params.height = pixels;
playControlsPanelMinimized.setLayoutParams(params);
playControlsPanelMinimized.setBackgroundColor(Color.parseColor("#00FFFF"));
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
activityMain.addView(playControlsPanelMinimized);
// Second View
View playControlsPanelMinimized2 = new View(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
final float scale2 = activityMain.getContext().getResources().getDisplayMetrics().density;
float dps2 = 10;
int pixels2 = (int) (dps2 * scale2 + 0.5f);
params2.height = pixels2;
playControlsPanelMinimized2.setLayoutParams(params2);
playControlsPanelMinimized2.setBackgroundColor(Color.parseColor("#00FFFF"));
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
activityMain.addView(playControlsPanelMinimized2);
edit:
I changed activity_main.xml to LinearLaout, and adjusted my variables accordingly, but now I no longer see the views.
LinearLayout activityMain = (LinearLayout)findViewById(R.id.activity_main);
// First View
View playControlsPanelMinimized = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
final float scale = activityMain.getContext().getResources().getDisplayMetrics().density;
float dps = 10;
int pixels = (int) (dps * scale + 0.5f);
params.height = pixels;
playControlsPanelMinimized.setLayoutParams(params);
playControlsPanelMinimized.setBackgroundColor(Color.parseColor("#00FFFF"));
//params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
activityMain.addView(playControlsPanelMinimized);
edit
I am trying a new technique with the relative layout method to no avail as of yet
View playControlsPanelMinimized = new LinearLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
final float scale = activityMain.getContext().getResources().getDisplayMetrics().density;
float dps = 10;
int pixels = (int) (dps * scale + 0.5f);
params.height = pixels;
playControlsPanelMinimized.setLayoutParams(params);
playControlsPanelMinimized.setBackgroundColor(Color.parseColor("#00FFFF"));
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
activityMain.addView(playControlsPanelMinimized);
View A = new View(this);
View B = new View(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
A.setLayoutParams(params2);
B.setLayoutParams(params2);
playControlsPanelMinimized.addView(A);
playControlsPanelMinimized.addView(B);
Replace your RelativeLayout activity_main with LinearLayout which have orientation horizontal or vertical
Use lnearLayout instead of Relative layout, may be you need to use weightsum
set orientation to vertical / horizontal
put view right of other view and set it in xml .

Match layout width to height

I have a LinearLayout, I set the width to match_parent. How can I match the height to the width to get a square layout?
<LinearLayout
android:id="#+id/linearLayout"
android:orientation="horizontal"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="2dp"
android:gravity="center"
android:background="#drawable/card_layout">
</LinearLayout>
To achive this you need to get width of your #+id/linearLayout programatically
LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout);
int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
int screenHeight = mContext.getResources().getDisplayMetrics().widthPixels;
mLinearLayout.measure(screenWidth, screenHeight);
int layoutWidth = mLinearLayout.getMeasuredWidth();
And then set width values as height
ViewGroup.LayoutParams params = mLinearLayout.getLayoutParams();
params.height = layoutWidth;
mLinearLayout.setLayoutParams(params);
You can also do this like #balaji koduri showed in OnGlobalLayoutListener
try this:
final LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LayoutParams params = (LayoutParams) layout
.getLayoutParams();
params.height = params.width;
layout.setLayoutParams(params);
layout.getViewTreeObserver().addOnGlobalLayoutListener(
null);
}
});
Do it programmatically -
LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout);
ViewGroup.LayoutParams params = mDialogLayout.getLayoutParams();
params.width = mContext.getResources().getDisplayMetrics().widthPixels;
params.height = mContext.getResources().getDisplayMetrics().widthPixels;
if you want other size -
params.width = 200;
params.height = 200;
Make your custom layout with overrided onMeasure
#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}

view.getLocationOnScreen(locations) returns wrong Y coordinate

int[] locations = new int[2];
view.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
otherView.setLocation(x, y);
This results in otherView being verticaly ~somewhere in the middle of first view and not exactly a top of it.
This is basically the logic of setLocation method:
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = mX;
layoutParams.y = mY;
dialog.getWindow().setAttributes(layoutParams);
return dialog;
Just a guess, but I notice you didn't ensure the margins of the default-generated dialog are zero. Perhaps try this:
WindowManager.LayoutParams lp...
lp.horizontalMargin = 0;
lp.verticalMargin = 0;

How to set EditText box height and width programmatically in Android

I want to manage the height and width of an EditText box programmatically in Android. I tried edittext.setWidth(32); and edittext.setEms(50);, but both are not working. See my below code, as I am using it to create dynamic EditTexts in Android.
private EditText createEditText()
{
final LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
final EditText edittext = new EditText(this);
edittext.setLayoutParams(lparams);
edittext.setWidth(32);
edittext.setEms(50);
return edittext;
}
private EditText createEditText()
{
final LayoutParams lparams = new LayoutParams(50,30); // Width , height
final EditText edittext = new EditText(this);
edittext.setLayoutParams(lparams);
return edittext;
}
Try this.
edittext.getLayoutParams().width=32;
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
final float height = metrics.heightPixels;
EditText edittext = new EditText(this);
edittext.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,(int) (height/2)));
RelativeLayout.LayoutParams.width = 32;
RelativeLayout.LayoutParams.height = 50;
works for me. for example
lparams.width = 32;
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams();
params.width = mScreenWidth / 5;
params.height = mScreenWidth / 5;
edittext.setLayoutParams(params);
Try the following code:-
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
profile_pic.getLayoutParams().height=(screenHeight/6);
profile_pic.getLayoutParams().width=(screenHeight/6);

Categories

Resources