My previous question get the value from a method in another class is understandably closed because its duplicate, but I still can't find a way to solve the problem. I will be grateful if someone kindly help. I have progressed a little, but thats not enough. First, my current code:
MainActivity.java
public class MainActivity extends AppCompatActivity
implements CalFragment.OnDateSelected{
private TextView mTextMessage;
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.menu_home);
return true;
case R.id.navigation_dashboard:
//mTextMessage.setText(date);
showDateDialog();
mTextMessage.setText(date);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.menu_location);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public void showDateDialog() {
DialogFragment CalFragment = new CalFragment();
CalFragment.show(getSupportFragmentManager(), "timePicker");
//date = "Hello";
}
}
and
CalFragment.java
public class CalFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
OnDateSelected mcallback;
public interface OnDateSelected {
public void NewStrDate(String strDate);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
Date d = new Date(year, month, day);
String strDate = dateFormatter.format(d);
}
}
I want to supply the date from CalFragment to ShowDate dialogue, but its not working. At the present state, it shows implements
declaration in MainActivity
is faulty. I am reading some java tutorial, but its just my 2nd day. So kindly help a bit.