Question

Is is possible to update a picture in a custom AlertDialog, from a result obtained through an AsyncTask? A user is prompted to enter another username, which an AsyncTask checks if said entered username is valid. When the boolean result is returned, the AsyncTask updates an image beside the username showing a check (for valid) or an X (for invalid).

My custom AlertDialog that gets inflated via the following code:

Fragment1 extends Fragment {

    public boolean onOptionsItemSelected(MenuItem item){ 
        switch (item.getItemId()){
        ...
        case 1:
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View usernamePrompt = inflater.inflate(R.layout.alertdialog_username, ViewGroup)getActivity().getCurrentFocus());

            AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
            alert.setTitle("Send New Friend Request");
            alert.setView(usernamePrompt);
            alert.show();
            return true; 
        ...

The layout simply contains an edittext beside an imageview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/ad_et"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textVisiblePassword|textNoSuggestions" />

    <ImageView
        android:id="@+id/welcomescreen_iv_username_okay"
        style="@style/spacing_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/x" />

</LinearLayout>

the AsyncTask literally just returns a boolean value, but I have no idea how to update a view contained in an AlertDialog from this info. Is this even possible?

Edit: here's the AsyncTask: (I did not add it originally because I have it calling methods that simply update a TextView that is part of the Fragment (i.e. updateUsernameView(boolean isValid)). I could find how to access the Views in the AlertDialog from the Fragment which it is hosted. Could I add a TextWatcher to the EditText view when it is inflated and trigger a new AsyncTask onTextChanged? I'm going to try that and see if it works and let you all know.

private class CheckNewUsernameTask extends AsyncTask<String, Void, Boolean> {
    private static final String TAG = "CheckNewUsernameTask";   

        @Override
        protected Boolean doInBackground(String... params){
            String response = ClearNetworking.checkUsername(params[0]);
            Boolean result = null;

            try {
                JSONObject jsonResponse = new JSONObject(response);
                if (jsonResponse.has(ClearJSONParser.KEY_AVAILABLE)){
                    result = jsonResponse.getBoolean(ClearJSONParser.KEY_AVAILABLE);
                }
            } catch (JSONException e) {
                Log.e("TAG", "JSONException", e);
            }
            return result;
        }

        @Override
        protected void onPostExecute(Boolean result){
            if (result != null){
                updateUsernameView(result);
            } else {
                updateUsernameView(false);
            }
                Log.i(TAG, "... available = " + result);
            }
        }
    }
Was it helpful?

Solution

Following snippet will work for you.Update imageview as soon as you get the result.

View usernamePrompt = inflater.inflate(R.layout.alertdialog_username, ViewGroup)getActivity().getCurrentFocus());

   ImageView view=(ImageView)usernamePrompt.findViewById(R.id.welcomescreen_iv_username_okay);
   view.setImageBitmap(....);

OTHER TIPS

Declare the members inflater, usernameprompt, alert globally. Use following code in onPostExecute method of your async task.

inflater = getActivity().getLayoutInflater();
usernamePrompt = inflater.inflate(R.layout.alertdialog_username, ViewGroup)getActivity().getCurrentFocus());
usernamePrompt.findviewbyid("your image views id").setImageResource(new image resource);
alert.setView(usernamePrompt);
alert.show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top