Skip to content

Instantly share code, notes, and snippets.

@gvr23
Created December 28, 2017 15:55
Show Gist options
  • Save gvr23/fb95c9d50891a41c3ca556748f8487be to your computer and use it in GitHub Desktop.
Save gvr23/fb95c9d50891a41c3ca556748f8487be to your computer and use it in GitHub Desktop.
Dynamic DialogFragment
ANDROID
THIS IS THE ACTIVITY CLASS
=============================================================================================================================
public class MensajeErrorDialog extends DialogFragment implements View.OnClickListener {
public final static int TIPO_NORMAL = 1;
public final static int TIPO_PREGUNTA = 2;
private String titulo, mensaje;
private int tipoDialogo;
public static MensajeErrorDialog newInstance(String titulo, String mensaje, int tipoDialogo){
MensajeErrorDialog mensajeErrorDialog = new MensajeErrorDialog();
mensajeErrorDialog.titulo = titulo;
mensajeErrorDialog.mensaje = mensaje;
mensajeErrorDialog.tipoDialogo = tipoDialogo;
return mensajeErrorDialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_mensaje_error, container, false);
Button btnOk = view.findViewById(R.id.btnOK);
if(!TextUtils.isEmpty(titulo)){
TextView tvTitulo = view.findViewById(R.id.tvTitulo);
tvTitulo.setText(titulo);
}
if(!TextUtils.isEmpty(mensaje)){
TextView tvMensaje = view.findViewById(R.id.tvMensaje);
tvMensaje.setText(mensaje);
}
if(tipoDialogo == TIPO_PREGUNTA){
Button btnNo = view.findViewById(R.id.btnNo);
TextView tvMensaje = view.findViewById(R.id.tvMensaje);
btnOk.setText("Si");
btnNo.setVisibility(View.VISIBLE);
tvMensaje.setVisibility(View.GONE);
btnOk.setOnClickListener(this);
btnNo.setOnClickListener(this);
}else{
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
return view;
}
@Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(getDialog().getWindow().getAttributes());
layoutParams.width = (int)(size.x * 0.6);
getDialog().getWindow().setAttributes(layoutParams);
}
@Override
public void onClick(View v){
dismiss();
BaseActivity baseActivity = (BaseActivity)getActivity();
baseActivity.ocultoDialogoConCodigo(Integer.parseInt((String)v.getTag()));
}
}
THIS IS THE LAYOUT
=============================================================================================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/boton_promociones"
android:scaleType="centerCrop"/>
<TextView
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fontFamily="@font/dinnextltpro_regular"
android:text="Ups"
android:textColor="@color/colorWhite"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="51dp"
android:layout_height="163dp"
android:src="@drawable/corchete_izq_naranja" />
<LinearLayout
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:layout_width="55dp"
android:layout_height="52dp"
android:src="@drawable/admiracion_naranja" />
<TextView
android:id="@+id/tvTitulo"
android:layout_marginTop="15dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/colorBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/dinnextltpro_regular"
android:text="Ocurrio un error inesperado" />
<TextView
android:id="@+id/tvMensaje"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="@color/colorBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/dinnextltpro_regular"
android:text="Lo sentimos, tenemos un problema de mantenimiento, ingrese nuevamente en unos segundos" />
<LinearLayout
android:layout_marginTop="20dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:background="@drawable/button_border"
android:tag="1"
android:fontFamily="@font/dinnextltpro_regular"
android:text="OK" />
<Button
android:id="@+id/btnNo"
android:visibility="gone"
android:layout_marginStart="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/colorWhite"
android:textStyle="bold"
android:background="@drawable/button_border"
android:tag="2"
android:fontFamily="@font/dinnextltpro_regular"
android:text="No" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="51dp"
android:layout_height="163dp"
android:src="@drawable/corchete_der_naranja" />
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment