0

I have two class ,I want to apply a function inside one of them. At the same time, apply the function to another class. In a sense: call class ClassTimePickerDialogto from inside class MainActivity to show an hour. And after pressing the button inside the clock. I'm doing override for function onTimeSet. Then it runs out of work inside class Mainactivity. Then it executes the function finsh.

class mainactivity

public class MainActivity extends AppCompatActivity implements android.app.TimePickerDialog.OnTimeSetListener{

    Button b1;
    DialogFragment timePickerDialog1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timePickerDialog1 = new TimePickerDialog();
                timePickerDialog1.show(getSupportFragmentManager(), "timePicker");
            }
        });
    }


    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        Log.i("Dofunction", "IsDon"+i);
        finish();
    }

    // End ;
}

class TimePickerDialog

public  class TimePickerDialog extends DialogFragment implements android.app.TimePickerDialog.OnTimeSetListener{

    public String AM_PM = "";
    public int hourOfDay =0;
    public int minute=0;


    Context context;

    public TimePickerDialog(){
    }

    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new android.app.TimePickerDialog(context,this , hour , minute, DateFormat.is24HourFormat(context));
    }


    @Override
    public void onTimeSet(TimePicker view, int hourOfDay_, int minute_){
          // "11:06 PM"
         if(hourOfDay_>12){
             AM_PM = "PM";
             hourOfDay_= hourOfDay_-12;
           }else{
             AM_PM = "AM";
        }
         hourOfDay=hourOfDay_;
         minute=minute_;
    }


}
RamPrakash
  • 1,687
  • 3
  • 20
  • 25
ibrahem ahmed
  • 25
  • 1
  • 4
  • Please edit your question with meaningful names of classes and methods and what you want to achieve. If your code already does something right, please you have to ask about something that's not working. – P5music Jan 09 '20 at 17:57
  • Don't understand your question. I think you could ask it in a better way with less code. The answer is probably yes. – Christopher Schneider Jan 09 '20 at 17:57
  • I guess this answer works for you https://stackoverflow.com/questions/15915318/timepicker-ontimeset-not-being-called/15916863#15916863 – Volkan Albayrak Jan 09 '20 at 19:18

1 Answers1

0

You can override a method that you inherit from a parent class. Not a method from some other "random" java class.

  • Overriding is not relevant to the question. However, you want to write this kind of remarks in a comment, not an answer. – P5music Jan 09 '20 at 18:15