Tuesday, 16 September 2014

Andriod: Creating a Toast with data from the parent Activity of an onClickListener

We'll often register an inner class as an onClickListener. Inside this if there's a need to access the parent Activity use this:
getBaseContext()

Making a Toast message:
Toast.makeText(Context, text, Toast.LENGTH_LONG).show();


Code sample:

        addTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = ((EditText)findViewById(R.id.task_text)).getText().toString();
                Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG).show();
            }
        });

No comments:

Post a Comment