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 ...: (