Skip to content

Instantly share code, notes, and snippets.

@homerunsb
Last active October 29, 2020 05:54
Show Gist options
  • Save homerunsb/1e6cadea679e98c1cb91cd5bb9a31a7f to your computer and use it in GitHub Desktop.
Save homerunsb/1e6cadea679e98c1cb91cd5bb9a31a7f to your computer and use it in GitHub Desktop.
Android - Custom Dialog
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/terms_tv_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="서브타이틀 표시 영역"
android:textSize="12dp" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="horizontal"
android:textSize="12dp"
android:visibility="visible">
<RadioButton
android:id="@+id/radio_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_agree" />
<RadioButton
android:id="@+id/radio_disagree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_disagree" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/terms_tv_detail_contents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:text="약관 전문 표시 영역"
android:textSize="12dp" />
</LinearLayout>
</RelativeLayout>
public class CustomDialogFragment extends DialogFragment implements View.OnClickListener{
private static final String TAG = "CustomDialogFragment";
private static final String ARG_DIALOG_MAIN_MSG = "dialog_main_msg";
private String mMainMsg;
public static CustomDialogFragment newInstance(String mainMsg) {
Bundle bundle = new Bundle();
bundle.putString(ARG_DIALOG_MAIN_MSG, mainMsg);
CustomDialogFragment fragment = new CustomDialogFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mMainMsg = getArguments().getString(ARG_DIALOG_MAIN_MSG);
}
}
@Override
public void onResume() {
super.onResume();
int dialogWidth = getResources().getDimensionPixelSize(R.dimen.dialog_fragment_width);
int dialogHeight = ActionBar.LayoutParams.WRAP_CONTENT;
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.layout_custom_dialog, null);
((TextView)view.findViewById(R.id.dialog_confirm_msg)).setText(mMainMsg);
view.findViewById(R.id.dialog_confirm_btn).setOnClickListener(this);
builder.setView(view);
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
/*
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_custom_dialog, container, false);
((TextView)view.findViewById(R.id.dialog_confirm_msg)).setText(mMainMsg);
view.findViewById(R.id.dialog_confirm_btn).setOnClickListener(this);
Dialog dialog = getDialog();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(false);
return view;
}
*/
private void dismissDialog() {
this.dismiss();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_confirm_btn:
dismissDialog();
break;
}
}
}
<?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="match_parent"
android:background="@drawable/ninepatch_common_box_gray"
android:padding="20dp">
<TextView
android:id="@+id/dialog_confirm_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:textSize="13sp"
android:textColor="#111111"
android:textStyle="bold" />
<Button
android:id="@+id/dialog_confirm_btn"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_confirm_msg"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="@null"
android:text="확인"
android:textSize="12dp" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
String agreeOrDisagree = null;
if (checkedId == R.id.radio_group) {
agreeOrDisagree = getString(R.string.radio_agree);
} else {
agreeOrDisagree = getString(R.string.radio_disagree);
}
CustomDialogFragment dialog = CustomDialogFragment.newInstance(
getString(R.string.custom_dialog_msg, agreeOrDisagree)
);
dialog.show(getSupportFragmentManager(), "dialog");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment