Sum timestamps from two textviews and display in new textview - java

I have two textviews and two buttons. When you click on the first button, a window with a choice of time opens. You select the time and it is displayed in textview1. Similarly with the second button and textview2. Now the main task is to sum this time and when you click on the Sum button, display the sum of the time in TextView3 (textview1 + textview2). any ideas how to implement this? preferably with code
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is First Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity"
android:id="#+id/btnActTwo"
android:onClick="onClick">
</Button>
<TextView
android:id="#+id/viewTime1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time1" />
<Button
android:id="#+id/time1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose Time 1"
android:onClick="setTime"/>
<TextView
android:id="#+id/viewTime2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time2" />
<Button
android:id="#+id/time2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose Time 2"
android:onClick="setTime2"
/>
<TextView
android:id="#+id/sum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sum" />
<Button
android:id="#+id/btnSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SUM"
android:onClick="onClick2"/>
</LinearLayout>
And Main.java code:
package com.example.myapplication_lab4;
import androidx.appcompat.app.AppCompatActivity;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView viewTime1;
TextView viewTime2;
TextView sum;
Button time1;
Button time2;
Button btnSum;
Calendar dateAndTime=Calendar.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewTime1 = (TextView) findViewById(R.id.viewTime1);
viewTime2 = (TextView) findViewById(R.id.viewTime2);
sum = (TextView) findViewById(R.id.sum);
time1 = (Button) findViewById(R.id.time1);
time2 = (Button) findViewById(R.id.time2);
btnSum = (Button) findViewById(R.id.btnSum);
}
// display a dialog box for selecting the time for 1
public void setTime(View v) {
new TimePickerDialog(MainActivity.this, t,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE), true)
.show();
}
// display a dialog box for selecting the time for 2
public void setTime2(View v) {
new TimePickerDialog(MainActivity.this, t2,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE), true)
.show();
}
// setting start time for 1
private void setInitialDateTime() {
viewTime1.setText(DateUtils.formatDateTime(this,
dateAndTime.getTimeInMillis(),
DateUtils.FORMAT_SHOW_TIME));
}
// setting start time for 2
private void setInitialDateTime2() {
viewTime2.setText(DateUtils.formatDateTime(this,
dateAndTime.getTimeInMillis(),
DateUtils.FORMAT_SHOW_TIME));
}
// setting the time picker for 1
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);
setInitialDateTime();
}
};
// setting the time picker for 2
TimePickerDialog.OnTimeSetListener t2 = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);
setInitialDateTime2();
}
};
//irrelevant to the issue
//Click to go to Activity 2
public void onClick(View view) {
Button btnActTwo;
btnActTwo = (Button) findViewById(R.id.btnActTwo);
if (view.getId() == R.id.btnActTwo) {//call sec act
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
public void onClick2(View view) {
}
}

Here is sample code of sum two date.
String time1="0:30:32";
String time2="0:35:20";
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date1 = timeFormat.parse(time1);
Date date2 = timeFormat.parse(time2);
long sum = date1.getTime() + date2.getTime();
String date3 = timeFormat.format(new Date(sum));
Log.e("TAG", "Date sum is => " + date3);
**Output: ** Date sum is => 01:05:52

Related

Making app to count time since first time used

How to make an app to count the time since the first time opened? I have no idea which class to use. Is it possible to achieve this with a stopwatch or something similar? Can someone pls share some code? I don't know what to search on the internet just have an idea of what I want to build. Or just tell me an idea with what I can achieve this and how?
statisticLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (plusiliminus==0){
Toast.makeText(getActivity(),R.string.toaststatistic,Toast.LENGTH_SHORT).show();
} else {
Fragment fragmentstatistic=new Statistic();
FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frameLayout,fragmentstatistic).commit();
}
}
});
As far as I could understand, you need persistent storage for the time when it was first used. I think SharedPreferences is a good way of storing this in your case.
In the onCreate function of your launcher activity, you might get the System.currentTimeInMillis() and store it in the SharedPreferences as stated here.
Then when you need to reset the time, you just have to clear the value in the SharedPreferences and can set a new value again.
I hope this gives you an idea of your implementation.
You have basically to do two things.
Get the current time when you start the app, or press a button using something like
Date currentTime = Calendar.getInstance().getTime();
This is a good beginning, you get the time, then you have to parse this time in a format you want to show to the user then you manage a stop and reset button as showed for instance in this tutorial
So you build a TextView and you show in real time the seconds, parsed in the way you need
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView timer ;
Button start, pause, reset;
long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;
Handler handler;
int Seconds, Minutes, MilliSeconds ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = (TextView)findViewById(R.id.tvTimer);
start = (Button)findViewById(R.id.btStart);
pause = (Button)findViewById(R.id.btPause);
reset = (Button)findViewById(R.id.btReset);
handler = new Handler() ;
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
StartTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
reset.setEnabled(false);
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
reset.setEnabled(true);
}
});
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MillisecondTime = 0L ;
StartTime = 0L ;
TimeBuff = 0L ;
UpdateTime = 0L ;
Seconds = 0 ;
Minutes = 0 ;
MilliSeconds = 0 ;
timer.setText("00:00:00");
}
});
}
public Runnable runnable = new Runnable() {
public void run() {
MillisecondTime = SystemClock.uptimeMillis() - StartTime;
UpdateTime = TimeBuff + MillisecondTime;
Seconds = (int) (UpdateTime / 1000);
Minutes = Seconds / 60;
Seconds = Seconds % 60;
MilliSeconds = (int) (UpdateTime % 1000);
timer.setText("" + Minutes + ":"
+ String.format("%02d", Seconds) + ":"
+ String.format("%03d", MilliSeconds));
handler.postDelayed(this, 0);
}
};
}
and the view of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context="in.amitsin6h.stopwatch.MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingBottom="90dp">
<TextView
android:text="00:00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvTimer"
android:textSize="50dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_marginTop="120dp"
android:paddingBottom="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Start"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvTimer"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:id="#+id/btStart" />
<Button
android:text="Pause"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btPause"
android:layout_alignBaseline="#+id/btStart"
android:layout_alignBottom="#+id/btStart"
android:layout_centerHorizontal="true" />
<Button
android:text="Reset"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btPause"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/btReset" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

Select First and Last Dates from Range selection of Dates

I've been Trying to create an Android application which able to select multiple days from the calendar, and display first and last Dates of the selection in different TextViews and Update that event in a Firestore Database.
this is the Calendar Activity class
CalendarActivity.Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.squareup.timessquare.CalendarPickerView;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CalendarActivity extends AppCompatActivity {
String eventStartDate ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CalendarPickerView calendar_view = (CalendarPickerView)
findViewById(R.id.calendar_view);
//getting currentS
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
Date today = new Date();
//add one year to calendar from todays date
calendar_view.init(today, nextYear.getTime())
.inMode(CalendarPickerView.SelectionMode.RANGE);
//action while clicking on a date
calendar_view.setOnDateSelectedListener(new
CalendarPickerView.OnDateSelectedListener() {
#Override
public void onDateSelected(Date date) {
Toast.makeText(getApplicationContext(),"Selected Date is : " +date.toString(),Toast.LENGTH_SHORT).show();
eventStartDate=date.toString();
}
#Override
public void onDateUnselected(Date date) {
//...
}
});
//fetch dates
final List<Date> dates = calendar_view.getSelectedDates();
//final int eventEndDateIndex = dates.lastIndexOf(dates);
//eventStartDate = dates.get(0);
//eventEndDate= dates.get(eventEndDateIndex);
//Displaying all selected dates while clicking on a button
Button btn_show_dates = (Button) findViewById(R.id.btn_show_dates);
btn_show_dates.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int j=0;
for (int i = 0; i< calendar_view.getSelectedDates().size();i++){
//here you can fetch all dates
Toast.makeText(getApplicationContext(),calendar_view.getSelectedDates().get(i).toString(),Toast.LENGTH_SHORT).show();
}
}
});
}
}
And this is the Layout File
activity_calendar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CalendarActivity">
<com.squareup.timessquare.CalendarPickerView
android:id="#+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="467dp"
android:layout_above="#+id/btn_show_dates"
android:layout_alignParentTop="true"
android:background="#FFFFFF"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay" />
<Button
android:id="#+id/btn_show_dates"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Display Dates"
android:background="#color/colorPrimary"
android:textColor="#FFFFFF"/>
<TextView
android:id="#+id/eventStartDate"
android:layout_width="107dp"
android:layout_height="23dp"
android:text="Event Start Date"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="472dp" />
<TextView
android:id="#+id/eventEndDate"
android:layout_width="116dp"
android:layout_height="23dp"
android:text="Event End Date"
tools:layout_editor_absoluteX="252dp"
tools:layout_editor_absoluteY="472dp" />
</android.support.constraint.ConstraintLayout>
You have a list of selected days as Date objects:
calendar_view.getSelectedDates();
Using a compare function such as:
Collections.sort(your_dates, new Comparator<Date>() {
#Override
public int compare(Date d1, Date d2) {
if(d1.getTime() > d2.getTime())
return 1;
else if(d1.getTime() < d2.getTime())
return -1;
return 0;
}
});
Then you can just access the first and last element of the list.

Android app crashes after launching intent and using setText() in TextView

I'm trying to launch an activity using intents and setting values in TextViews in the launched activity, but when I try to do that my app crashes. Heres the activity I'm trying to launch. It crashes when i try to set the text in the last two code lines
public class GameOver extends Activity {
TextView correctTxt;
TextView incorrectTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int correct = intent.getIntExtra("correct", 0);
int incorrect = intent.getIntExtra("incorrect", 0);
correctTxt = (TextView)findViewById(R.id.correct_txt);
incorrectTxt = (TextView)findViewById(R.id.incorrect_txt);
correctTxt.setText("" + correct);
incorrectTxt.setText("" + incorrect);
}
}
And the activity that's launching. I make use of the intent in the trueButton onClickListener method:
package com.example.beithia.geotest;
import android.app.Activity;
import java.util.*;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
public class TestActivity extends Activity {
private Button trueButton;
private Button falseButton;
private TextView questionTextView;
private TextView correctNum;
private TextView incorrectNum;
private TextView avgScore;
int numOfCorrect = 0;
int numOfIncorrect = 0;
double num = 1;
private TrueFalse[] questionBank = new TrueFalse[] {
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia,true),
new TrueFalse(R.string.question_channel_islands,true),
new TrueFalse(R.string.question_china,false),
new TrueFalse(R.string.question_mexico,true),
new TrueFalse(R.string.question_turkey,false),
new TrueFalse(R.string.question_everest,false),
new TrueFalse(R.string.question_colombia,false),
new TrueFalse(R.string.question_vatican,true),
new TrueFalse(R.string.question_nile,true),
};
private int currentIndex = 0;
private void updateQuestion() {
int question = questionBank[currentIndex].getQuestion();
questionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = questionBank[currentIndex].isTrueQuestion();
int msgId = 0;
if (userPressedTrue == answerIsTrue) {
msgId = R.string.correct_toast;
numOfCorrect++;
}
else {
msgId = R.string.incorrect_toast;
numOfIncorrect++;
}
Toast.makeText(this,msgId,Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questionTextView = (TextView)findViewById(R.id.question_text_view);
correctNum = (TextView)findViewById(R.id.correct_num);
incorrectNum = (TextView)findViewById(R.id.incorrect_num);
avgScore = (TextView)findViewById(R.id.average_score);
trueButton = (Button) findViewById(R.id.true_button);
trueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(true);
double score = (numOfCorrect / num) * 100;
currentIndex = (currentIndex + 1) % questionBank.length;
correctNum.setText("Correct: " + numOfCorrect);
incorrectNum.setText("Incorrect: " + numOfIncorrect);
avgScore.setText("Score: " + String.format("%.2f", score) + "%");
updateQuestion();
num++;
if(num > 10) {
Intent intent = new Intent(TestActivity.this, GameOver.class);
intent.putExtra("correct", numOfCorrect);
intent.putExtra("incorrect", numOfIncorrect);
startActivity(intent);
}
}
});
falseButton = (Button) findViewById(R.id.false_button);
falseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(false);
double score = (numOfCorrect / num) * 100;
currentIndex = (currentIndex + 1) % questionBank.length;
correctNum.setText("Correct: " + numOfCorrect);
incorrectNum.setText("Incorrect: " + numOfIncorrect);
avgScore.setText("Score: " + String.format("%.2f", score) + "%");
updateQuestion();
num++;
}
});
updateQuestion();
}
}
Here's the layout for activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/correct_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff00ff12" />
<TextView
android:id="#+id/incorrect_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff5440" />
<TextView
android:id="#+id/average_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff8c1a" />
<TextView
android:id="#+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
</LinearLayout>
And the layout for the activity_game_over.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/correct_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/incorrect_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I can get it to work if I use the setContentView(R.layout.activity_game_over);
however when I try to launch the main activity again it starts the GameOver Activity, but it should start the GeoQuiz activity instead.
In the GameOver activity you don't set any content view setContentView(), hence you textViews cannot be found, findViewById returns null, hence you get NullPointerException when invoking setText().
I can get it to work if I use the setContentView(R.layout.activity_game_over); however when I try to launch the main activity again it starts the GameOver Activity, but it should start the GeoQuiz activity instead. Please help!
The activity that runs at start up is defined in the android manifest xml file of your project.
look for
action android:name="android.intent.action.MAIN"

How could I make a notification off of information from a DatePicker/TimePicker

I am making a time management application. What I am trying to do is have some information:
DatePicker=to select the date of an event
TimePicker=to select the time of an event
Couple EditTexts=to put in information about the event(length in minutes, title)
So I have all of this in an activity, and now I need to find a way that I can take all of this information and make it into a push notification which will notify me at the date selected and at the time selected and for the amount of time inputted. Please tell if this is possible. If it is not, then please tell me if I can find some other way to do something similar.
Here is my .java:
package net.mastersofdisasters2.timagement;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import org.w3c.dom.Text;
import java.text.DateFormat;
import java.util.Calendar;
public class assignment extends Activity implements DatePicker.OnDateChangedListener
{
private TimePicker timePicker1;
private int hour;
private int minute;
static final int TIME_DIALOG_ID = 999;
// display current time
public void setCurrentTimeOnView() {
timePicker1 = (TimePicker) findViewById(R.id.timeofassignment);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this,
timePickerListener, hour, minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
DatePicker dp=null;
Calendar cal=null;
TextView mcdate=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assignment);
Button button = (Button)findViewById(R.id.create_assignment);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(assignment.this,PushNotification.class));
}
});
enter code here
setCurrentTimeOnView();`enter code here`
dp=(DatePicker)findViewById(R.id.dateofassignment);
mcdate=(TextView)findViewById(R.id.enter_assignment_datetime);
cal=Calendar.getInstance();
dp.init(dp.getYear(),dp.getMonth(),dp.getDayOfMonth(),this);
}
public void onDateChanged(DatePicker dpview, int year, int monthOfYear, int dayOfMonth) {
cal.set(year, monthOfYear, dayOfMonth);
java.util.Date d = cal.getTime();
mcdate.setText(String.valueOf(d.getMonth() +1) + "/" + String.valueOf(d.getDate()) + "/" + String.valueOf((d.getYear() + 1900)));
}
}
Now that was just to add the DatePicker and TimePickers. Now what do I do to create a push notification from the inputted information?
Heres my activity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#color/light_blue"
>
<TextView android:id="#+id/enter_assigment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="#bool/abc_action_bar_embed_tabs"
android:text="#string/assignmentadd"
android:textSize="20sp"
android:textColor="#color/light_black"
/>
<EditText android:id="#+id/enter_assignment_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/assignmentadd"
android:layout_below="#id/enter_assigment_text"
android:paddingTop="10dp"
/>
<TextView
android:id="#+id/enter_assignment_datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/assignment_datetime"
android:layout_below="#+id/enter_assignment_field"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textColor="#color/light_black"/>
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/enter_assignment_field"
android:id="#+id/dateofassignment"
android:layout_marginTop="30dp">
</DatePicker>
<TextView android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dateofassignment"
android:layout_marginTop="10dp"
android:text="#string/thechosendate"
/>
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timeofassignment"
android:layout_below="#+id/date"
android:layout_marginTop="10dp">
</TimePicker>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/choose_assignment_length"
android:layout_below="#+id/timeofassignment"
android:layout_marginTop="10dp"
android:text="#string/length_of_assignment">
</TextView>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/enter_assignment_length"
android:hint="#string/length_of_assignment"
android:layout_below="#+id/choose_assignment_length"
android:layout_marginTop="10dp"/>
<Button android:id="#+id/create_assignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/enter_assignment_length"
android:layout_centerHorizontal="true"
android:text="#string/submit"
android:background="#color/cornflower_blue"
/>
</RelativeLayout>
Please Help quickly I need to finish this very soon

Selecting Arrival and exit time using time picker in android

Here am selecting arrival and exit time using imageview with help of TimePickerFragment, DialogFragment.
My question is how to select the exit time..When am selecting exit time it will show in the arrival time text box only. How to solve this problem help me.
Here code for arrival and exit time
Am using two text box and two image view button.
import java.util.Calendar;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
#SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
EditText arrtime,exittime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectArrivalTime(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void selectExitTime(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void populateSetArrTime(int hourOfDay, int minute) {
arrtime = (EditText)findViewById(R.id.editText1);
arrtime.setText(+hourOfDay+":"+minute);
}
public void populateSetExitTime(int hourOfDay, int minute) {
exittime= (EditText)findViewById(R.id.editText2);
exittime.setText(+hourOfDay+":"+minute);
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
populateSetArrTime(hourOfDay, minute);
}
public void onTimeSet1(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
populateSetExitTime(hourOfDay, minute);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my XML layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="67dp"
android:text="Arrival time" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="30dp"
android:text="Exit time" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="time" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/editText1"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="time" >
</EditText>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_marginTop="51dp"
android:contentDescription="#string/selectarrtime"
android:onClick="selectArrivalTime"
android:src="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/editText2"
android:layout_alignTop="#+id/imageButton1"
android:layout_marginRight="33dp"
android:contentDescription="#string/selectexittime"
android:onClick="selectExitTime"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
If select a arrival time i will shows time in my arrival edit text box and also if i select a exit time it will shows the time in same arrival edit text box not displaying in my exit edit text box how to slove this pls help me thanks in advance.
#SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
EditText arrtime,exittime;
static type=""; //changed
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton b1=(ImageButton)findViewById(R.id.imageButton1); //changed from here
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTime("arrival");
}
});
ImageButton b2=(ImageButton)findViewById(R.id.imageButton2);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTime("exit");
}
}); ///to here
}
public void setTime(String type) { //function changed
MainActivity.type=type;
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void populateTime(int hourOfDay, int minute) { //function changed
if(type.equals("arrival") {
arrtime = (EditText)findViewById(R.id.editText1);
arrtime.setText(+hourOfDay+":"+minute);
}
else if(type.equals("exit") {
arrtime = (EditText)findViewById(R.id.editText2);
exittime.setText(+hourOfDay+":"+minute);
}
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
populateTime(hourOfDay, minute); //changed
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Also delete line android:onClick line from both the ImageButtons in your xml file

Categories