How to change Alertdialog buttons?

Asked

Viewed 352 times

0

I wanted to change the buttons of my Alertdialog for example: I would like to exchange the positivebutton for a textview and so put the onClick event in it.

My java:

Mainactivity.java

public void onBackPressed() {
    View view = this.getLayoutInflater().inflate(R.layout.dialogo_multiuso, null);
    ((TextView)view.findViewById(R.id.botao_negativo)).setOnClickListener(new View.OnClickListener(){

        public void onClick(View view) {
        }
    });
    ((TextView)view.findViewById(R.id.botao_positivo)).setOnClickListener(new View.OnClickListener(){

        public void onClick(View view) {
            MenuActivity.this.finish();
        }
    });
    if (this.conf.getString("tema", "").equals((Object)"claro")) {
        ((LinearLayout)view.findViewById(R.id.fundo)).setBackgroundColor(-4342339);
        ((TextView)view.findViewById(R.id.titulo)).setTextColor(-16777216);
        ((TextView)view.findViewById(R.id.titulo)).setBackgroundColor(-6381922);
        ((TextView)view.findViewById(R.id.texto)).setTextColor(-16777216);
        ((TextView)view.findViewById(R.id.texto)).setBackgroundColor(-6381922);
        ((TextView)view.findViewById(R.id.botao_positivo)).setTextColor(-16777216);
        ((TextView)view.findViewById(R.id.botao_negativo)).setTextColor(-16777216);
        ((LinearLayout)view.findViewById(R.id.fundo_opcoes)).setBackgroundColor(-6381922);
    }
    if (this.conf.getString("idioma", "").equals((Object)"pt")) {
        AlertDialog.Builder builder = new AlertDialog.Builder((Context)this);
        ((TextView)view.findViewById(R.id.titulo)).setText((CharSequence)"Quer realmemte sair?");
        ((TextView)view.findViewById(R.id.botao_positivo)).setText((CharSequence)"Sim");
        ((TextView)view.findViewById(R.id.botao_negativo)).setText((CharSequence)"N\u00e3o");
        ((TextView)view.findViewById(R.id.texto)).setVisibility(8);
        builder.setView(view);
        builder.create().show();
        return;
    }
    AlertDialog.Builder builder = new AlertDialog.Builder((Context)this);
    ((TextView)view.findViewById(R.id.titulo)).setText((CharSequence)"Do you really want to leave?");
    ((TextView)view.findViewById(R.id.botao_positivo)).setText((CharSequence)"Yes");
    ((TextView)view.findViewById(R.id.botao_negativo)).setText((CharSequence)"No");
    ((TextView)view.findViewById(R.id.texto)).setVisibility(8);
    builder.setView(view);
    builder.create().show();
}

My XML is: xml dialog.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="1" android:layout_width="-1" android:layout_height="-2">
    <LinearLayout android:orientation="1" android:id="false" android:background="424242" android:padding="10dp" android:layout_width="-1" android:layout_height="-2">
        <TextView android:textSize="16sp" android:textStyle="1" android:textColor="ffffff" android:gravity="16" android:id="false" android:background="000" android:padding="10dp" android:focusable="false" android:layout_width="-1" android:layout_height="50dp" android:text="Titulo" />
        <TextView android:textSize="14sp" android:textStyle="1" android:textColor="ffffff" android:gravity="51" android:id="false" android:background="000" android:padding="10dp" android:focusable="false" android:layout_width="-1" android:layout_height="-2" android:layout_marginTop="5dp" android:text="Texto do diálogo." />
        <LinearLayout android:orientation="0" android:id="false" android:background="000" android:layout_width="-1" android:layout_height="40dp" android:layout_marginTop="5dp">
            <TextView android:textSize="16sp" android:textStyle="1" android:textColor="ffffff" android:gravity="17" android:id="false" android:padding="10dp" android:focusable="false" android:layout_width="-1" android:layout_height="-1" android:text="não" android:layout_weight="{4:1065353216}" />
            <TextView android:textSize="16sp" android:textStyle="1" android:textColor="ffffff" android:gravity="17" android:id="false" android:padding="10dp" android:focusable="false" android:layout_width="-1" android:layout_height="-1" android:text="sim" android:layout_weight="{4:1065353216}" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

This is all I need for my Alertdialog to be complete. If anyone wants more information, please do not hesitate to ask for my thanks.

  • What’s the problem with the code? Where it doesn’t work as expected?

  • You can make an xml with textview and inflate inside alertdialog

  • @ramaral my doubt is how I can use the Inflater so that when I click on a text view it has the same effect as a negativeButton for example.

  • The click on textView R.id.botao_positive is working?

  • Yes the positive and to end the application only the negative that no, in case I’m trying to close the dialog box when it is clicked but I’m not getting.

1 answer

1


To close Alertdialog use the method Dismiss().

This requires a reference to Alertdialog at the time you declare the OnClickListener textview R.id.botao_negativo.

Thus, you must create Alertdialog before declaring Listener.

public void onBackPressed() {
    View view = this.getLayoutInflater().inflate(R.layout.dialogo_multiuso, null);

    TextView titulo = (TextView)view.findViewById(R.id.titulo);
    TextView botao_positivo = (TextView)view.findViewById(R.id.botao_positivo);
    TextView botao_negativo = (TextView)view.findViewById(R.id.botao_negativo);
    TextView texto = (TextView)view.findViewById(R.id.texto);

    if (this.conf.getString("tema", "").equals((Object)"claro")) {
        ((LinearLayout)view.findViewById(R.id.fundo)).setBackgroundColor(-4342339);
        titulo.setTextColor(-16777216);
        titulo.setBackgroundColor(-6381922);
        texto.setTextColor(-16777216);
        texto.setBackgroundColor(-6381922);
        botao_positivo.setTextColor(-16777216);
        botao_negativo.setTextColor(-16777216);
        ((LinearLayout)view.findViewById(R.id.fundo_opcoes)).setBackgroundColor(-6381922);
    }
    if (this.conf.getString("idioma", "").equals((Object)"pt")) {
        titulo.setText((CharSequence)"Quer realmemte sair?");
        botao_positivo.setText((CharSequence)"Sim");
        botao_negativo.setText((CharSequence)"N\u00e3o");
    }else {
        titulo.setText((CharSequence) "Do you really want to leave?");
        botao_positivo.setText((CharSequence) "Yes");
        botao_negativo.setText((CharSequence) "No");
    }
    texto.setVisibility(8);

    AlertDialog.Builder builder = new AlertDialog.Builder((Context)this);
    builder.setView(view);
    final AlertDialog alertDialog = builder.create();
    botao_negativo.setOnClickListener(new View.OnClickListener(){

        public void onClick(View view) {
            alertDialog.dismiss();;
        }
    });
    botao_positivo.setOnClickListener(new View.OnClickListener(){

        public void onClick(View view) {
            MenuActivity.this.finish();
        }
    });
    alertDialog.show();
}

I simplified the code to avoid repetitive use of findViewById().

It can be further simplified if you use String Resources to have the texts in more than one language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.