views:

1020

answers:

1

Hi,

I have an app with a View based on the SurfaceHolder (similar to the Lunar Lander tutorial). The whole GUI is drawn on a canvas, and I want to be able to prompt for user text input at a given moment using a custom layout Dialog, that is then taken care of and rendered to the canvas using standard procedure.

My problem, however, is that it seems that best practice is to open Dialogs from the Activity. This is no problem either, since i thought i might create a Handler and then pass it to the View that could in turn use it to pass Messages from the GUI thread in the View on to the Activity, that in turn could fetch the input, and send a reply back, etc.

Problem is, after I call setContentView(R.layout.main), which contains the whole app, i want to call MyAppView mMyAppView = (MyAppView) findViewById(R.id.app_view_id).

This call returns null.

What is considered to be best practice here? I can't find any good examples and the API is turning up, well, not much.

I would appreciate any help here.

Thanks!

Gus

+2  A: 

Create a dialog themed activity to display over your current activity.

public class TextEntryActivity extends Activity {
    private EditText et;

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_text_entry);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        // title
        try {
            String s = getIntent().getExtras().getString("title");
            if (s.length() > 0) {
                this.setTitle(s);
            }
        } catch (Exception e) {
        }
        // value

        try {
            et = ((EditText) findViewById(R.id.txtValue));
            et.setText(getIntent().getExtras().getString("value"));
        } catch (Exception e) {
        }
        // button
        ((Button) findViewById(R.id.btnDone)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                executeDone();
            }
        });
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
        executeDone();
        super.onBackPressed();
    }

    /**
     *
     */
    private void executeDone() {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("value", TextEntryActivity.this.et.getText().toString());
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }


}

Launch by:

Intent foo = new Intent(this, TextEntryActivity.class);
foo.putExtra("value", "old value to edit");
this.startActivityForResult(foo, EDIT_ACTION);

then catch the response on onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case EDIT_ACTION:
                try {
                    String value = data.getStringExtra("value");
                    if (value != null && value.length() > 0) {
                        //do something with value
                    }
                } catch (Exception e) {
                }
                break;
            default:
                break;
        }
    }

Manifest is defined as:

<activity
            android:name=".utils.TextEntryActivity"
            android:label="Type in the value"
            android:theme="@android:style/Theme.Dialog" />
Pentium10
Thanks!I was hoping the solution would not involve intents, but this was an elegant solution, as it were!Appreciate it, this solved my problem.
Gus