Before 2022/Android

AlertDialog 스타일을 호출한 내부 파일에서 변경하기

Eljoe 2019. 7. 11. 15:43

android custom alertdialog programmatically

private void callAlert(String pw) {
  AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
  builder.setTitle("비밀번호 확인")
    .setMessage("입력한 비밀번호로 등록하시겠습니까?")
    .setPositiveButton("확인", (dialog, which) -> {
    	Toast.makeText(getContext(), "등록이 완료되었습니다.", Toast.LENGTH_SHORT).show();
    	dialog.cancel();
    	dismiss();
    })
    .setNegativeButton("취소", (dialog, which) -> {
    	dialog.cancel();
    });

  AlertDialog dialog = builder.create();
  dialog.show();

  // 메시지 내용
  TextView mTvMessage = dialog.findViewById(android.R.id.message);
  // 폰트 변경
  mTvMessage.setTypeface(tf);

  // 확인 / 취소 버튼
  Button btnPositive = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
  Button btnNegative = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
  
  // weight 조절 
  LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
  layoutParams.weight = 10;
  
  btnPositive.setLayoutParams(layoutParams);
  btnNegative.setLayoutParams(layoutParams);

  // 취소 버튼 margin End 값 조절
  ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) btnNegative.getLayoutParams();
  lp.setMarginEnd(30);
}