How to make dialog that accesses a view without using setContentView

Asked

Viewed 164 times

0

I am creating a dialog with an Edittext, and I need to take this value in another Edittext, without using setContentView pq do not want to redirect to the dialog xml... Follows the code:

dialogo_edittext.xml:

<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/valorml"
            android:textColor="@color/primary_text"
            android:textSize="15dp"
            android:id="@+id/digite"/>
        <EditText
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:id="@+id/mlvalor"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ml"
            android:textSize="15dp"
            android:textColor="@color/primary_text"
            android:id="@+id/ml"/>
    </LinearLayout>
</HorizontalScrollView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="@string/btn_add"
    android:background="@drawable/designbotao"
    android:textColor="@color/icons"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:id="@+id/btn_addcopo"/>

when I try to get the value typed in the edittext dialog:

 /*Está dentro do onCreate de HomeActivity.java*/
    mloutro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String titulo2 = "ADICIONAR COPO";
            LayoutInflater inflater2 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v2 = inflater2.inflate(R.layout.dialogo_edittext, null);
            dialog2 = new Dialog(HomeActivity.this);
            dialog2.setTitle(titulo2);
            dialog2.setContentView(v2);
            dialog2.show();
            /*Botão do dialog*/
            Button add = (Button) findViewById(R.id.btn_addcopo);

            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /*valor digitado no EditText*/
                    EditText valorml = (EditText) findViewById(R.id.mlvalor);
                    int valml = Integer.parseInt(valorml.getText().toString());
                    barraprogresso.setProgress(barraprogresso.getProgress() + valml);
                    falta_agua -= valml;
                    Log.i("outro", "falta: " + falta_agua + "qtd agua:" + qtd_agua);
                    dialog2.hide();
                    if (falta_agua <= 0) {
                        nivelU++;
                        String texto = getString(R.string.lvl);
                        Log.i("texto", "texto: " + texto);
                        nivel.setText(getString(R.string.lvl) + " " + nivelU);
                        barraprogresso.setProgress(0);
                        falta_agua = qtd_agua;
                        dialog.show();
                    }
                }
            });
        }
    });

By clicking the button inside the dialog I need to take the edittext value and use it in my main activty (Homeactivity). I tried to use setContentView but redirects pro xml from dialog and it does not happen nd when I click Add. Without setContentView error... :(

1 answer

0


From what I see of your code you are making the bindViews(findViewById) wrong, if you inflate your view different from your main layout, the views have to be bound with them, see in this my example:

final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.dialogo_edittext); dialog.setTitle("ADICIONAR COPO"); final EditText valorml = (EditText) dialog.findViewById(R.id.mlvalor); Button add = (Button) dialog.findViewById(R.id.btn_addcopo); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //dialog.dismiss(); int valml = Integer.parseInt(valorml.getText().toString()); } }); dialog.show();

dialog.findViewById...

  • It worked out! Thank you!

Browser other questions tagged

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