user3679701
user3679701

Reputation: 45

Android trouble in setting text from Edit TextView from alert dialog to EditText View

I have an Text View on which when user click will open a alert dialog. The dialog consists of Edit Text and a button. In Edit text when user enters it's height and clicks on "OK" button,the entered height will gets displayed to the Edit Text.

MainActivity.java

et_weightAndHeight = (EditText) findViewById(R.id.et_weightAndHeight);

        et_weightAndHeight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 LayoutInflater li = LayoutInflater.from(AccountActivity.this);
                 View promptView = li.inflate(R.layout.prompts, null);


                 AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountActivity.this);
                 alertDialog.setView(promptView);



             final EditText input =  (EditText)findViewById(R.id.editTextDialogUserInput);

                alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub


                        et_weightAndHeight.setText(input.getText().toString());



                    }
                });

                AlertDialog aD = alertDialog.create();
                aD.show();
            }
        });

    }

}

prompts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Weight in lbs : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>

</LinearLayout>

Upvotes: 0

Views: 434

Answers (1)

Spring Breaker
Spring Breaker

Reputation: 8251

Without proper explanation and logcat, I am assuming you are getting NullPointerException because you haven't initialized the TextView inside the dialog properly.

Change,

final TextView input =(TextView)findViewById(R.id.editTextDialogUserInput);

to

final TextView input =(TextView)promptView.findViewById(R.id.editTextDialogUserInput);

Upvotes: 1

Related Questions