Transparent background in custom dialog

I'm trying to get transparent backgroud on my custon Dialog, but I can't seem to get it done on Android 4.4.4. It only works on Android 5.1.

As a result, I'm looking for: http://www.americocarelli.com.br/android_5.1.png

But I get this: http://www.americocarelli.com.br/android_4.4.4.png

This is my custom layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialogo_layout_root"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparente"
    android:windowBackground="@color/transparente">

    <TextView
        android:id="@+id/titulo_dialogo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center_horizontal"
        android:paddingStart="@dimen/paddingStartEnd"
        android:paddingEnd="@dimen/paddingStartEnd"
        android:paddingTop="@dimen/paddingTopBottom"
        android:paddingBottom="@dimen/paddingTopBottom"
        android:background="@color/fundoEscuro"
        android:textSize="@dimen/abc_text_size_title_material_toolbar"/>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/drawer_shadow_down"
        android:layout_below="@id/titulo_dialogo"
        android:contentDescription="@string/..."/>

    <TextView
        android:id="@+id/mensagem_dialogo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titulo_dialogo"
        android:paddingStart="@dimen/paddingStartEnd"
        android:paddingEnd="@dimen/paddingStartEnd"
        android:paddingTop="8dp"
        android:paddingBottom="@dimen/paddingTopBottom"
        android:background="@color/transparente"/>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/mensagem_dialogo"
        android:layout_alignParentStart="true"
        android:shrinkColumns="*"
        android:stretchColumns="*">

        <TableRow>
            <Button
                android:id="@+id/BotaoNegativo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_shape"
                android:textAllCaps="false"/>

            <Button
                android:id="@+id/BotaoPositivo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_shape"
                android:textAllCaps="false"/>
        </TableRow>
    </TableLayout>
</RelativeLayout>

      

In my .xml style, I got this definition:

<color name="transparente">#00FFFFFF</color>

      

And this is the code:

public class Dialogos extends DialogFragment {
    private static final String PARAMETRO_TITULO = "titulo";
    private static final String PARAMETRO_MENSAGEM = "mensagem";
    private static final String PARAMETRO_TXT_BTN_POSITIVO = "txtBtnPositivo";
    private static final String PARAMETRO_TXT_BTN_NEGATIVO = "txtBtnNegativo";

    public Dialogos(){
    }

    public static Dialogos newInstance(int titulo, int mensagem, int txtBtnPositivo, int txtBtnNegativo) {
        Dialogos fragment = new Dialogos();
        Bundle args = new Bundle();

        args.putInt(PARAMETRO_TITULO, titulo);
        args.putInt(PARAMETRO_MENSAGEM, mensagem);
        args.putInt(PARAMETRO_TXT_BTN_POSITIVO, txtBtnPositivo);
        args.putInt(PARAMETRO_TXT_BTN_NEGATIVO, txtBtnNegativo);

        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int titulo = getArguments().getInt(PARAMETRO_TITULO);
        int mensagem = getArguments().getInt(PARAMETRO_MENSAGEM);
        int txtBtnPositivo = getArguments().getInt(PARAMETRO_TXT_BTN_POSITIVO);
        int txtBtnNegativo = getArguments().getInt(PARAMETRO_TXT_BTN_NEGATIVO);

        Typeface fonte = Typeface.createFromAsset(getActivity().getAssets(), "Fonts/RegencieLightAlt.ttf");

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.mensagem_dialogo, null);
        TextView tvTitulo = (TextView) view.findViewById(R.id.titulo_dialogo);
        TextView tvMensagem = (TextView) view.findViewById(R.id.mensagem_dialogo);

        tvTitulo.setTypeface(fonte);
        tvMensagem.setTypeface(fonte);

        if (titulo != 0)
            tvTitulo.setText(titulo);

        Button btnPositivo = (Button) view.findViewById(R.id.BotaoPositivo);
        Button btnNegativo = (Button) view.findViewById(R.id.BotaoNegativo);

        tvMensagem.setText(mensagem);

        btnPositivo.setTypeface(fonte);
        btnNegativo.setTypeface(fonte);

        btnPositivo.setText(txtBtnPositivo);
        btnNegativo.setText(txtBtnNegativo);

        btnPositivo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent());
            }
        });

        btnNegativo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
            }
        });

        builder.setView(view);

        return builder.create();
    }
}

      

+3


source to share


1 answer


The problem is that the AlertDialog constructor is not really suitable for creating a transparent dialog and will and will always have this black background which is actually the theme for it, use the dialog to create a transparent theme instead.

Example:



    Dialog alertDialog = new Dialog(this);
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.setContentView(R.layout.tabs);
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    alertDialog.show();

      

Using Dialog doesn't require any sort of theme manipulation for transparent background, so it's mostly easy.

+11


source







All Articles