Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set margin dynamically in Android?

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); 
like image 472
user1057197 Avatar asked Jun 16 '12 09:06

user1057197


People also ask

How do I set margins to recyclerView programmatically?

margin); int marginTopPx = (int) (marginTopDp * getResources(). getDisplayMetrics(). density + 0.5f); layoutParams. setMargins(0, marginTopPx, 0, 0); recyclerView.


1 Answers

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

like image 53
Dipak Keshariya Avatar answered Sep 28 '22 03:09

Dipak Keshariya