How to make dialog that accesses a view without using setContentView

0

I am creating a dialog with an EditText, and I need to get this value in another activity, without using the setContentView because I do not want to redirect to the xml of the dialog ... The following code:

dialog_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 dialog's edittext:

 /*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();
                    }
                }
            });
        }
    });

When I click the button inside the dialog I need to get the value of edittext and use it in my main activty (HomeActivity). I tried to use the setContentView but it redirects to the xml of the dialog and does not happen when I click on Add. Without the error setContentView ...: (

    
asked by anonymous 12.06.2016 / 16:13

1 answer

0

From what I see of your code you are doing the bindViews (findViewById) wrong, if you inflate your view different from your main layout, the views have to be linked with them, see in 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 ...

    
12.06.2016 / 16:50