0

I'm writing a simple app that opens up a time picker dialog, asks for input, checks it against the system time, and tells you whether or not it is correct. However, I need to grab the TextView that displays whether it is true or false, and change it within the TimePickerFragment which is a DialogFragment. What should I do?

TimePickerFragment.java

package com.example.timechecker;
import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment
                        implements TimePickerDialog.OnTimeSetListener {
int hour;
int minutes;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minutes = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, 12, 00,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    //Grab the Text View from the Main Activity
//      MainActivity m = new MainActivity();
//      m.grabText text = new m.grabText();

    //Check if given Picker value is == to the system time, display whether or          not it is so
    if (hourOfDay == hour && minutes == minute) {
        text.setText("You are correct!");
    } else {
        text.setText("You are incorrect!");
    }
}
}

MainActivity.java

package com.example.timechecker;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
//Creates the dialog with the Time Picker
public void checkTime(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");

}

//Class to grab the Text View for the Dialog Fragment
public class grabText {
    TextView text = (TextView) findViewById(R.id.whatTime);

    //Uses a method to set the Text View text
    public void setText(String string) {
        text.setText(string);
    }
}

}
Bandit Bat
  • 25
  • 1
  • 8

2 Answers2

0

When you call findViewById in any activity then i searches in activity layout only. therefore to get the TextView from a dialog you need a reference to that dialog and then you call dialog.findViewById(id_of_field) and this will give you desired TextView.

Hope this Helps

Pramod
  • 387
  • 1
  • 7
  • 19
  • Well, I'm trying to grab the text view from the main activity, so I can change it using variables in the TimePickerFragment class – Bandit Bat Sep 25 '13 at 17:40
0

I recently did this, I was able to do this by making MainActivity.java the parent of my DialogFragment.

MainActivity.Java

Add this to MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    mActivity = this;

After, make it global here:

public class MainActivity extends Activity {
    MainActivity mActivity;

DialogFragment

Add this to DialogFragment:

public void setParent(MainActivity parent) {
    mParent = parent;
}

After, make it global here:

public class ClearDialogModern extends DialogFragment {
    MainActivity mParent;

EDIT: Here's where you setParent. Put this in your MainActivity.java onCreate:

newDialogFragment = new DialogFragment();
newDialogFragment.setParent(mActivity);

How to use:

You can now use mParent to reference the MainActivity.java.

Evan Bashir
  • 5,501
  • 2
  • 25
  • 29