Take Edittext value from a layout passed as a parameter to a Alertdialog.Builder at the click of the button?

Asked

Viewed 7,034 times

5

I use Layoutinflater in my view - View view = li.inflate(R.layout.alertdialog, null); I created a Alertdialog.Builder Builder setting for it the view, and the buttons positive and negative. My Layout has an Edittext and I want to get the value of Edittext after the click of the positive button. I use an Edittext type variable to rescue the Edittext Layout reference with R.id.edtText, even so, using the log its value is null. How do I get the value of Edittext in this case?

LayoutInflater li = getLayoutInflater();

    View view = li.inflate(R.layout.alertdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Criando novo arquivo de apresentação");
    builder.setView(view);

    builder.setPositiveButton("Criar",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    nomeArquivo = (EditText) findViewById(R.id.nomeArquivo);
                    String temp = diretorio + nomeArquivo.getText().toString() + ".cron";

This part of the code gives an error and ends my application. - nomeArquivo.getText().toString()

xml

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

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/arquivoicon" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="38dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Nome do arquivo:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/nomeArquivo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:gravity="center"
    android:text="teste" >

    <requestFocus />
</EditText>

1 answer

7


You are using the findViewById of Activity.

You have to use the findViewById of the Alertdialog: dialog.findViewById(R.id.nomeArquivo);

LayoutInflater li = getLayoutInflater();

    View view = li.inflate(R.layout.alertdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Criando novo arquivo de apresentação");
    builder.setView(view);

    builder.setPositiveButton("Criar",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    nomeArquivo = (EditText) ((Dialog) dialog).findViewById(R.id.nomeArquivo);
                    String temp = diretorio + nomeArquivo.getText().toString() + ".cron";
  • 1

    ramaral worked perfectly, only had to make a change. - filename = (Edittext) ((Dialog) dialog). findViewById(R.id.filename); - has a dot(.) between parentheses and findViewBy... Thank you!

Browser other questions tagged

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