From the comment
When clicks the button it shows a timepicker. The user select the time and the time chosen is showed as the buttons text (alarm app).
There is no need to inflate a layout.
http://developer.android.com/guide/topics/ui/controls/pickers.html
Use a Interface as a call back to the activity.
On Button click show the DialogFragment
. Use a interface. Implement the same in Activity and set the time to button
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
public interface PickTime
{
public void returnTime(String value);
}
PickTime mCallback;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
mCallback = (PickTime) getActivity();
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
if(mCallback!=null)
{
StringBuilder sb = new StringBuilder();
sb.append(hourOfDay);
sb.append(":");
sb.append(minute);
mCallback.returnTime(sb.toString());
}
}
}
In your Activity
public class MainActivity extends ActionBarActivity implements PickTime{
Button button;
TimePickerFragment newFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1)
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
});
}
@Override
public void returnTime(String value) {
// TODO Auto-generated method stub
button.setText(value);
}
}