I am currently doing an android application that contains customize alert dialog. It contains a button , but i can't set the margin for the button . the code is given below. setmargin method is not working
AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this); Button button = new Button(Login.this); button.setText("Send"); LayoutParams buttonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); button.setLayoutParams(buttonLayoutParams); resetPassword=editText.getText().toString(); LinearLayout layout = new LinearLayout(Login.this); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(textView); layout.addView(editText); layout.addView(button); myDialog.setView(layout);
margin); int marginTopPx = (int) (marginTopDp * getResources(). getDisplayMetrics(). density + 0.5f); layoutParams. setMargins(0, marginTopPx, 0, 0); recyclerView.
Write below code to set margin, it may help you.
AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this); Button button = new Button(Login.this); EditText editText = new EditText(Login.this); TextView textView = new TextView(Login.this); button.setText("Send"); LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); buttonLayoutParams.setMargins(50, 10, 0, 0); button.setLayoutParams(buttonLayoutParams); String resetPassword = editText.getText().toString(); LinearLayout layout = new LinearLayout(Login.this); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(textView); layout.addView(editText); layout.addView(button); myDialog.setView(layout); myDialog.show();
Use LinearLayout.LayoutParams
or RelativeLayout.LayoutParams
according to parent layout of the child view
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With