Manny265
Manny265

Reputation: 1709

How to set text in a textview from a class that implements DialogFragment

Im experimenting on dialogs in android and I have come across some problem.
I have two classes,one is tha MainActivity that extends Activity and the other is MyDialog that extends DialogFragment. It is from a new MyDialog object in the MainActivity that I call a show method on the object to show the dialog.


Now after selecting whatever option I have tried a number of ways to setText to a TextView but I cant make use of the findViewById method because thats from Activity and I'm not extending Activity in MyDialog. Tried to create a new Activity object (just trying) and to use the findViewById method from there but to no avail.
Also I tried creating a public TextView in the OnCreate method of the MainActivity before the MyDialog object is created and tried to access and setText from the other class it also didnt work. Please help,how can I set text after selecting an option on the dialog from the setPositive,setNegative and setNeutral methods that come with the DialogFragment.

Here is some extract of the MyDialog class,just the part where I set the title etc for the dialog options:

@Override public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.dialogMessage).setPositiveButton(R.string.dialogacceptmsg, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            //setting text and color to text in a result textview
            message.setText("You are going to MARS with Sir Richard Branson");

            /*v= new Activity();//creating a new activity to find the textview
            message=(TextView)v.findViewById(R.id.textView1);
            message.setText("You are going to MARS with Sir Richard Branson");
            message.setTextColor(Color.MAGENTA);*/

        }
    }).setNegativeButton(R.string.dialogcancelmsg,new DialogInterface.OnClickListener() {


        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            //setting text and color to text in a result textview
            message.setText("Your ticket will be sold to Justin Bieber");

            /* v= new Activity();//creating a new activity to find the textview
            message = (TextView) v.findViewById(R.id.textView1);
            message.setText("Your ticket will be sold to Justin Bieber");
            message.setTextColor(Color.RED);*/


        }
    }).setNeutralButton("Not sure",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // setting text and color to text in a result textview
            message.setText("Keeping it on standby");
            /*v=new Activity();
            message =(TextView) v.findViewById(R.id.textView1);
            message.setText("Keeping it on standby");
            message.setTextColor(Color.BLUE);*/

        }
    }).setTitle(R.string.dialogTitle) ;


    return builder.create();
}

Upvotes: 0

Views: 2240

Answers (2)

Kasra Rahjerdi
Kasra Rahjerdi

Reputation: 2503

Option 1:

Use getActivity() or getParentFragment() depending on what the file you're creating your DialogFragment is (Activity/Fragment) and set up a public method like setTextForMyView(String text) which takes an attribute and puts it into the text view.

This will only work if you're creating the DialogFragment using the activity's context, meaning you're doing something like new MyDialogFragment(this...) rather than new MyDialogFragment(getApplicationContext()...)

Option 2:

Set up an interface within your DialogFragment, have the parent activity implement it, pass parent activity in as the listener for your interface either in the constructor or some method before displaying the dialog, then call your interface's method after the click.

Sidenote: I'd strongly recommend reading through the documentation for Activities and the Activity Lifecycle so you can know why referencing TextViews in other activities or attempting to create new ones doesn't work.

Upvotes: 1

ssantos
ssantos

Reputation: 16526

Have you tried

TextView yourTextView = (TextView) getActivity().findViewById(R.id.yourTextView);
yourTextView.setText("Some text");

?

Upvotes: 2

Related Questions