What is the cause of the int variable problem? - java

I have a variable of its kind int.
I've allocated value in it through another value from within another object ,but there's a problem.
The problem is happening when i press the button 3
after i set the required time values for each object.
I'll show you the wrong message ,and the code used.
I want to change the code to work properly.
// Logcat:
java.lang.NullPointerException: Attempt to read from field 'int com.ahmedco.testcode2.TimePickerDialog.hourOfDay' on a null object reference
at com.ahmedco.testcode2.MainActivity$3.onClick(MainActivity.java:65)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
// class 1
public class MainActivity extends AppCompatActivity {
Button b1, b2, b3;
DialogFragment timePickerDialog1, timePickerDialog2;
int hourTimer1, hourTimer2, minuteTimer1, minuteTimer2;
String time1Format, time2Format, AM_PMTimer1, AM_PMTimer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
timePickerDialog1 = new TimePickerDialog(1);
timePickerDialog1.show(getSupportFragmentManager(), "timePicker");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
timePickerDialog2 = new TimePickerDialog(2);
timePickerDialog2.show(getSupportFragmentManager(), "timePicker");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hourTimer1 = ((TimePickerDialog) timePickerDialog1).hourOfDay;
hourTimer2 = ((TimePickerDialog) timePickerDialog2).hourOfDay;
minuteTimer1 = ((TimePickerDialog) timePickerDialog1).minute;
minuteTimer2 = ((TimePickerDialog) timePickerDialog2).minute;
AM_PMTimer1 = ((TimePickerDialog) timePickerDialog1).AM_PM;
AM_PMTimer2 = ((TimePickerDialog) timePickerDialog2).AM_PM;
//Log.i("trace1","test "+((TimePickerDialog) timePickerDialog1).hourOfDay);
//Log.i("trace2","test "+((TimePickerDialog) timePickerDialog2).hourOfDay);
time1Format = "" + hourTimer1 + ":" + minuteTimer1 + " " + AM_PMTimer1;
time2Format = "" + hourTimer1 + ":" + minuteTimer1 + " " + AM_PMTimer2;
Log.i("time1Format", "" + time1Format);
Log.i("time2Format", "" + time2Format);
}
});
}
}
// class 2
public class TimePickerDialog extends DialogFragment implements android.app.TimePickerDialog.OnTimeSetListener {
public String AM_PM = "";
public int hourOfDay = 0;
public int minute = 0;
public TimePickerDialog(int id) {
// currentEditText = id;
}
#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(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
#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";
}
///Log.i("hourOfDay",""+hourOfDay_+":"+hourOfDay_+" "+AM_PM);
hourOfDay = hourOfDay_;
minute = minute_;
}
}

You having problem inside b3.setOnClickListener because you are not initialized object so you have to initialize objects timePickerDialog1 and timePickerDialog2 globally.
DialogFragment timePickerDialog1=new TimePickerDialog(1), timePickerDialog2=new TimePickerDialog(2);
or
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(timePickerDialog1==null)
timePickerDialog1=new TimePickerDialog(1);
if(timePickerDialog2==null)
timePickerDialog1=new TimePickerDialog(2);
hourTimer1 = ((TimePickerDialog) timePickerDialog1).hourOfDay;
hourTimer2 = ((TimePickerDialog) timePickerDialog2).hourOfDay;
minuteTimer1 = ((TimePickerDialog) timePickerDialog1).minute;
minuteTimer2 = ((TimePickerDialog) timePickerDialog2).minute;
AM_PMTimer1 = ((TimePickerDialog) timePickerDialog1).AM_PM;
AM_PMTimer2 = ((TimePickerDialog) timePickerDialog2).AM_PM;
//Log.i("trace1","test "+((TimePickerDialog) timePickerDialog1).hourOfDay);
//Log.i("trace2","test "+((TimePickerDialog) timePickerDialog2).hourOfDay);
time1Format = "" + hourTimer1 + ":" + minuteTimer1 + " " + AM_PMTimer1;
time2Format = "" + hourTimer1 + ":" + minuteTimer1 + " " + AM_PMTimer2;
Log.i("time1Format", "" + time1Format);
Log.i("time2Format", "" + time2Format);
}
});

You declare your TimePickerDialog in the beginning (DialogFragment timePickerDialog1, timePickerDialog2;)
But only initialise it when Button 1 or 2 are pressed respectively
timePickerDialog2 = new TimePickerDialog(2);
Therefore timePickerDialog2is nulluntil Button 2 was pressed.
When trying to call .hourOfDay in Button 3 when timePickerDialog2has not been initialised yet, you get a NullPointerException. You can't call methods on an object that is null.

Related

setOnClickListener in Android for Two TextViews

I have a Simple Date Picker App in Android and it's working fine when I click on First TextView.
How Can I activate the same Calendar with a second TextView. One TextView shows the Long Date and other shows the Short Date
public class MainActivity extends AppCompatActivity {
private TextView mDisplayLongDate;
TextView mDisplayShortDate;
CheckBox checkBoxVisibility;
private DatePickerDialog.OnDateSetListener mDateSetListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDisplayLongDate = findViewById(R.id.tvLDate);
mDisplayShortDate = findViewById(R.id.tvSDate);
//View checkBox = findViewById(R.id.checkBox);
checkBoxVisibility = findViewById(R.id.checkBox_visibility);
//boolean isChecked = checkBoxVisibility.isClickable();
boolean isChecked = checkBoxVisibility.isChecked();
updateTextVisibility(isChecked);
checkBoxVisibility.setOnCheckedChangeListener((buttonView, isChecked1) -> {
//Step 05 - Updating UI according to the currently changed state
updateTextVisibility(isChecked1);
});
mDisplayLongDate.setOnClickListener(view -> {
Calendar cal = Calendar.getInstance();
With a CheckBox, the TextView Shows Long Date and Short Date.
I cannot click on the short date to edit the Calendar. How to activate the Calendar in both situation.
String dateLong = monthStr + "/" + day + "/" + year;
String dateShort = monthStr + "/" + day;
mDisplayLongDate.setText(dateLong);
mDisplayShortDate.setText(dateShort);
};
}
private void updateTextVisibility(boolean isChecked) // When checking the trigger (checkbox)
{
if (isChecked)
{
mDisplayShortDate.setVisibility(View.VISIBLE);
mDisplayLongDate.setVisibility(View.GONE);
}
else
{
mDisplayShortDate.setVisibility(View.GONE);
mDisplayLongDate.setVisibility(View.VISIBLE);
}
You can use this type of method and call it where you need .
private void getCal() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int monthStr = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dPDialog = new DatePickerDialog(MainActivity.this, android.R.style.Theme_Holo_Light_Dialog,
new DatePickerDialog.OnDateSetListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
public void onDateSet(DatePicker datepicker, int selectedY, int selectedM, int selectedD) {
String[] months = new DateFormatSymbols(Locale.ENGLISH).getMonths();
String dateLong = months[selectedM] + "/" + selectedD + "/" + selectedY;
String dateShort = months[selectedM] + "/" + selectedD;
// String dateLong = (selectedM + 1) + "/" + selectedD + "/" + selectedY;
//String dateShort = (selectedM + 1) + "/" + selectedD;
mDisplayLongDate.setText(dateLong);
mDisplayShortDate.setText(dateShort);
}
}, year, monthStr, day);
dPDialog.show();
}
Example class-
public class MainActivity extends AppCompatActivity {
private TextView mDisplayLongDate;
TextView mDisplayShortDate;
CheckBox checkBoxVisibility;
private DatePickerDialog.OnDateSetListener mDateSetListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDisplayLongDate = findViewById(R.id.tvLDate);
mDisplayShortDate = findViewById(R.id.tvSDate);
mDisplayLongDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getCal();
}
});
mDisplayShortDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getCal();
}
});
}
private void getCal() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int monthStr = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dPDialog = new DatePickerDialog(MainActivity.this, android.R.style.Theme_Holo_Light_Dialog,
new DatePickerDialog.OnDateSetListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
public void onDateSet(DatePicker datepicker, int selectedY, int selectedM, int selectedD) {
String[] months = new DateFormatSymbols(Locale.ENGLISH).getMonths();
String dateLong = months[selectedM] + "/" + selectedD + "/" + selectedY;
String dateShort = months[selectedM] + "/" + selectedD;
// String dateLong = (selectedM + 1) + "/" + selectedD + "/" + selectedY;
//String dateShort = (selectedM + 1) + "/" + selectedD;
mDisplayLongDate.setText(dateLong);
mDisplayShortDate.setText(dateShort);
}
}, year, monthStr, day);
dPDialog.show();
}
}

Unable to fetch value from parent activity to child activity Android Studio

I have two activities: MainActivity6 (parent) and tool1mode1 (Child)
from the parent activity:
if (tool == 1) {
switch (mode) {
case 1:
intentTool1mode1.putExtra("Type", typekey);
intentTool1mode1.putExtra("Key", Key);
intentTool1mode1.putExtra("Tool", "Tool 1");
intentTool1mode1.putExtra("Mode", "Home");
Toast.makeText(MainActivity6.this, "Type: " + typekey + "\n" + "Key: " + Key + "\n" + "Tool: " + "1" + "\n" + "Mode: " + "Home" + "\n", Toast.LENGTH_LONG).show();
if(key_60.isChecked())
{
String number = "480000";
long val = Long.parseLong(number);
intentTool1mode1.putExtra("Time",val);
startActivityForResult(intentTool1mode1, 1);
}
if(key_90.isChecked())
{
String number = "720000";
long val = Long.parseLong(number);
intentTool1mode1.putExtra("Time",val);
startActivityForResult(intentTool1mode1, 1);
}
break;
And this is the part from the child Activity:
private long START_TIME_IN_MILLIS;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
START_TIME_IN_MILLIS = intent.getLongExtra("Time", 0);
newbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startTimer();
}
});
The code below represents both function I used to start and update the counter in the child Activity:
private void startTimer()
{
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
progress++;
pb.setProgress((int)progress*100/(480000/1000));
}
#Override
public void onFinish()
{
progress++;
pb.setProgress(100);
//Vibration
if (Build.VERSION.SDK_INT >= 26)
{
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
Toast.makeText(com.example.ticwatch_1.tool1mode1.this,"Done",Toast.LENGTH_SHORT).show();
}
else
{
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150}, new int[]{VibrationEffect.EFFECT_CLICK},-1));
}
}
}.start();
}
private void updateCountDownText()
{
//time in minutes and seconds
int minutes = (int)(mTimeLeftInMillis/1000)/60;
int seconds = (int)(mTimeLeftInMillis/1000)%60;
//formating the above to appear as String
String timeLeftFormatted = String.format("%02d:%02d",minutes,seconds);
timer.setText(timeLeftFormatted);
}
What I want is to fetch the value from the Parent Activity and to update the value of START_TIME_IN_MILLIS with the value I fetched. So I I want the timer to run for 8 min I'll fetch 480000. What I get instead is the counter is 0 when I start it as in it already finished and didn't start from 8 min or any other value. I don't know what to do and I'm exhausted.

Applying class calendar via different method

I'm trying to create calendar in one of my fragments, yet I can't figure out how can I call the calendar class into the onCreate(); function in fragmentTwo.java.
I have tried this, but I keep getting error:
FATAL ERROR Attempt to invoke virtual method
'android.view.Window$Callback android.view.Window.getCallback()' on a
null object reference
.
fragmentTwo.java:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Calendar calendar = new Calendar();
calendar.setContentView(R.layout.calendar);
}
Calendar.java:
public class Calendar extends AppCompatActivity {
CalendarView calendarView;
TextView dateDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
calendarView = (CalendarView) findViewById(R.id.calendarView);
dateDisplay = (TextView) findViewById(R.id.date_display);
dateDisplay.setText("Date: ");
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) {
dateDisplay.setText("Date: " + i2 + " / " + i1 + " / " + i);
Toast.makeText(getApplicationContext(), "Selected Date:\n" + "Day = " + i2 + "\n" + "Month = " + i1 + "\n" + "Year = " + i, Toast.LENGTH_LONG).show();
}
});
}
}
How can I apply the Calendar class in my fragmentTwo onCreate() function?
The onCreate() method in a Fragment is called after the Activity's onAttachFragment() but before that Fragment's onCreateView().
So basically you can't do view stuff inside onCreate() .
So you need to add those two lines
Calendar calendar = new Calendar();
calendar.setContentView(R.layout.calendar);
in onCreateView()
Link to the docs

Retrieving times from two TimePickers in DialogFragments

I have an input form in an activity where I'd like to retrieve the start time and end time of an event. I am using a timepicker displayed in a dialog fragment to get the start time and return it to the main activity, but how do re use the same logic to retrieve the end time of the event? My code:
Main Activity:
public class AddEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
private int pickerHour = 0;
private int pickerMin = 0;
//code ommitted for length
public void showTimePickerDialog(View v) {
TimePickerFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String amOrPm = "am";
pickerHour = hourOfDay;
pickerMin = minute;
String minutesString = "";
if (hourOfDay > 12){ //translate to 12 hr clock. Little workaround to get both timepicker and am/pm label to show up correctly.
amOrPm = "pm";
pickerHour-=12;
}
if (pickerMin < 10) {
minutesString = "0"; //fix weird bug where only one zero is shown on times ending in :00
}
startTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
}
TimePickerFragment:
public class TimePickerFragment extends DialogFragment {
int hour,minute;
private Activity mActivity;
private TimePickerDialog.OnTimeSetListener mListener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
try {
mListener = (TimePickerDialog.OnTimeSetListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(mActivity, mListener, hour, minute,
DateFormat.is24HourFormat(mActivity));
}
}
I need to be able to differentiate between the start time and the end time, but I am not sure how to differentiate them without repeating myself. I am pretty new to Java and Android. Thank you so much for any help.
Possibly something as simple as a boolean?
public class AddEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
...
boolean isSettingStartTime = true;
//code ommitted for length
public void showTimePickerDialog(View v) {
TimePickerFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
...
if (isSettingStartTime) {
startTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
isSettingStartTime = false;
} else {
endTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
}
}
}
This would require you being able to retrieve the TimePicker from your TimePickerFragment
You either need to get the TimePicker instance when you create both TimePickerFragments and store them in two fields such as startTimePicker and finishTimePicker.
Then when the TimePicker is passed in through the onTimeSet you then check to see which TimePicker it is like so.
if ( timePicker == startTimePicker ) {
startTimeTextView.setText....
} else {
finishTimeTextView.setText...
}

timePicker with multiple buttons

I hope someone can help me. I lack in OOP knowledge and am a newbie in android.
I have 6 buttons. Each button will call the timePicker and display the time on the button.
How do i want to differentiate each buttons?
Thank you.....
Here's the code..:
public class TabTwo extends Activity implements OnClickListener {
private Button btnBrfkst;
private Button btnMtea;
private Button btnLunch;
private Button btnAftea;
private Button btnDinner;
private Button btnSupper;
private Calendar mCalen;
private int hourOfDay;
private int minute;
private int ampm;
int timePickerInput ;
private static final int Time_PICKER_ID = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_two);
btnBrfkst = (Button) findViewById(R.id.button1);
btnMtea = (Button) findViewById(R.id.button2);
btnLunch = (Button) findViewById(R.id.button3);
btnAftea = (Button) findViewById(R.id.button4);
btnDinner = (Button) findViewById(R.id.button5);
btnSupper = (Button) findViewById(R.id.button6);
mCalen = Calendar.getInstance();
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
btnBrfkst.setOnClickListener(this);
btnMtea.setOnClickListener(this);
btnLunch.setOnClickListener(this);
btnAftea.setOnClickListener(this);
btnDinner.setOnClickListener(this);
btnSupper.setOnClickListener(this);
}
#Override
#Deprecated
protected Dialog onCreateDialog(int id) {
switch (id) {
case Time_PICKER_ID:
return new TimePickerDialog(this, TimePickerListener,
hourOfDay, minute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener TimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
// while dialog box is closed, below method is called.
public void onTimeSet(TimePicker view, int hour, int minute) {
switch (timePickerInput) {
case R.id.button1:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr = (ampm == 0) ? "AM" : "PM";
// Set the Time String in Button
btnBrfkst.setText(hour12format + " : " + minute + " / " + ampmStr);
break;
case R.id.button2:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format2 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr2 = (ampm == 0) ? "AM" : "PM";
btnMtea.setText(hour12format2 + " : " + minute + " / " + ampmStr2);
break;
case R.id.button3:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format3 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr3 = (ampm == 0) ? "AM" : "PM";
btnLunch.setText(hour12format3 + " : " + minute + " / " + ampmStr3);
break;
case R.id.button4:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format4 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr4 = (ampm == 0) ? "AM" : "PM";
btnAftea.setText(hour12format4 + " : " + minute + " / " + ampmStr4);
break;
case R.id.button5:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format5 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr5 = (ampm == 0) ? "AM" : "PM";
btnDinner.setText(hour12format5 + " : " + minute + " / " + ampmStr5);
break;
case R.id.button6:
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format6 = mCalen.get(Calendar.HOUR);
hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
minute = mCalen.get(Calendar.MINUTE);
ampm = mCalen.get(Calendar.AM_PM);
String ampmStr6 = (ampm == 0) ? "AM" : "PM";
btnSupper.setText(hour12format6 + " : " + minute + " / " + ampmStr6);
break;
}
}
};
#Override
public void onClick(View v) {
showDialog(Time_PICKER_ID);
}
}
Your activity or fragment should implements View.OnClickListener.
Then each button should register each button like so:
Button buttonOne = (Button) findViewById(R.id.button_one);
Button buttonTwo = (Button) findViewById(R.id.button_two);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
Then in your onClick method:
#Override
public void onClick(View temp)
{
switch(temp.getId()) {
case R.id.button_one:
//do something
break;
case R.id.button_two:
//do something
break;
}
}
An other option is to set the method to be called in the layout file for each button using android:onClick="myMethodOne". This way you can call different methods if you want or even the same method and differentiate on ids like you do in the onClick methods.
Change this:
#Override
public void onClick(View v) {
showDialog(Time_PICKER_ID);
}
to
#Override
public void onClick(View v) {
timePickerInput = v.getId();
showDialog(Time_PICKER_ID);
}
You need to set the timerPickerInput to be the id of the button so you know which button to display the time in.

Categories

Resources