How do I change the AlertDialog buttons?

0

I wanted to change the buttons of my AlertDialog for example: I would like to change the positivebutton for a textview and thus put the onClick event on 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: dialogo.xml

<?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>

For my AlertDialog to be complete, this is missing. If anyone wants more information, do not hesitate to ask for my thanks.

    
asked by anonymous 20.10.2017 / 07:45

1 answer

1

To close AlertDialog use the method dismiss () .

For this you need to have the reference to AlertDialog at the time you declare OnClickListener of TextView R.id.botao_negativo .

So you should 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've simplified the code to avoid repetitive use of findViewById() .

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

    
21.10.2017 / 15:35