Skip to content

Instantly share code, notes, and snippets.

@JoachimR
Last active August 24, 2022 13:22
Show Gist options
  • Save JoachimR/6bfbc175d5c8116d411e to your computer and use it in GitHub Desktop.
Save JoachimR/6bfbc175d5c8116d411e to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class AppCompatMaterialAlertDialog extends DialogFragment {
private Activity activity;
private LayoutInflater inflater;
private TextView tv_my_textview;
private CheckBox cb_my_checkbox;
private String textToShow;
public static AppCompatMaterialAlertDialog getInstance(String someArgument) {
AppCompatMaterialAlertDialog appCompatMaterialAlertDialog = new AppCompatMaterialAlertDialog();
Bundle bundle = new Bundle();
bundle.putString("someArgument", someArgument);
appCompatMaterialAlertDialog.setArguments(bundle);
return appCompatMaterialAlertDialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.activity = getActivity();
this.inflater = activity.getLayoutInflater();
Bundle bundle = getArguments();
textToShow = bundle.getString("someArgument");
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.custom_layout, null);
initDialogUi(v);
final AlertDialog d = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
.setTitle(getString(R.string.some_dialog_title))
.setCancelable(true)
.setPositiveButton(activity.getString(R.string.some_dialog_title_btn_positive),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doSomething();
dismiss();
}
})
.setNegativeButton(activity.getString(R.string.some_dialog_title_btn_negative),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
})
.setView(v)
.create();
// change color of positive button
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);
b.setTextColor(getResources().getColor(R.color.colorPrimary));
}
});
return d;
}
private void doSomething() {
}
private void initDialogUi(View root) {
tv_my_textview = (TextView) root.findViewById(R.id.tv_my_textview);
tv_my_textview.setText(textToShow);
cb_my_checkbox = (CheckBox) root.findViewById(R.id.cb_my_checkbox);
cb_my_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something
}
});
}
}
<?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:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/tv_my_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="some text message\nAnd more information\neven more"/>
<CheckBox
android:id="@+id/cb_my_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_my_textview"
android:text="do not ask again or whatever"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/some_different_accent_color</item>
<item name="android:textColorPrimary">@color/some_custom_text_color</item>
</style>
</resources>
@GeoffreyHinck
Copy link

swag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment