I have a time registration app and some users requested to change to first day of the week according to their preferences. I know I can set the first day of the week on a calendar object, but I'm working with a lot of them and I don't feel like setting the first day on all of them. So is there a way to set the first day for my entire app?
I don't feel like setting the first day on all of them
There's a golden rule on software programming that says "Don't repeat yourself", also known as DRY rule. So you shouldn't write the same code again and again to set the first day of week, but some code still should do it for all of them, so....
On those cases, the common way is to build a factory.
class CalendarFactory {
public static void setFirstDayOfWeek(Context context, int value){
// use context to save value in SharedPreferences
}
public static Calendar newInstance(Context context) {
// read the value from SharedPreferences
// create new GregorianCalendar
// setFirstDayOfWeek
// return
}
}
then it's just make sure to acquire instance of calendar ALWAYS through this method.
Related
Updated to describe the requested code; the updates are at the end.
I'm trying to do something that seems like it should be simple but the Android Life Cycle doesn't seem to be working as described. I expect this is a misunderstanding on my part, not a bug in Android!
I am new to Android and am just getting familiar with the Android Life Cycle after developing Java apps for Windows so I'm still struggling with a lot of new ideas, especially the Life Cycle.
I have nearly finished my first app, which is based on a RecyclerView. It shows a bunch of sales records (each representing one sale) of a small company. Users can click on a FloatingActionButton to report a new sale or click on edit or remove icons on the individual records to change the details of a sale or delete it entirely. Each of these things has its own activity. Also, there is a SettingsActivity to show settings.
One of the settings lets the user select from amongst three different date formats for displaying the date of the sale. When I back out of Settings (by hitting the back button), I want to see the visible rows using the newly-changed date format immediately but this is NOT happening. Up until now, I would just click on the buttons to take me to one of the other activities, then click the cancel button when I get there; on returning to my main activity, I would see the new date format.
I don't feel my user should have to go to that other activity; simply returning from Settings should change the dates immediately. I started looking at the Android Life Cycle. As I read the documentation, I should be overriding the onResume() method and issuing a notifyDataChanged() to the Adapter that controls the RecyclerView to get it to rebind the visible rows, using the new date format.
Unfortunately, that has no effect whatever that I can see. Here's my onResume:
#Override
protected void onResume() {
super.onResume();
mAdapter.notifyDataSetChanged();
}
Am I doing the notifyDataSetChanged() in the wrong method? If not, why doesn't it work? If it is the wrong method, which method should I be overriding?
Is there any particularly good text or video tutorial explaining the Life Cycle that I should be reading or viewing?
Updates
The date format is set in my SettingsActivity, which is the default one provided in the SettingsActivity template, tailored to meet my needs. All I've touched is the General settings. I replaced all of the ones from the template with six of my own settings. One is called Date format and defaults to YYYY-MM-DD; there are two other choices.
My main activity, which is the one that invokes SettingsActivity via a menu option, gets the value of the date format from the default SharedPreferences. This is because I don't want to look up the value of the Date format once for every row in the ArrayList but just once for all the rows in the ArrayList. I pass the value of the Date format to the Adapter via its constructor. The format gets used in OnBindViewHolder() to control the appearance of the date on the various sales records.
Define a set method to your adapter,
public class MyAdapter extends RecyclerView.Adapter<AdapterContactList.ViewHolder> {
private String dateFormat;
.....
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
}
After user change the dateformat; set it to your adapter, then notify datas for change. That means, you need set new dateformat before notifyDataSetChanged method; because adapter doesn't know the new value.
#Override
protected void onResume() {
super.onResume();
String newDateFormat = getFromSharedPreferencesOrInstantly();
mAdapter.setDateFormat(newDateFormat);
mAdapter.notifyDataSetChanged();
}
In my Android application for my school project I want to make it such that:
Every day, when you first open the application, it will start an activity. However, if you open the application again even after closing it from the multitasking view, it will not start the activity again.
I want it to be much like Elevate (https://www.elevateapp.com/) where on first startup it will say "Your training session for the day is ready" but never display this again if you open the app at another time in the day.
This is a screenshot of the activity:
I have tried using AlarmManager in this link Alarm Manager Example and searching for answers but it did not work for me and I couldn't find any.
Is there a way to make it possible? Thanks in advance.
We can use SharedPreferences to store the system date when the app is launched and verify if it is the same date or a different one every time the app s run.
If the date is different, store the new date into the SharedPreferences handle that you used.
To understand how to use SharedPreference to store data you can look at my answer here for an example.
First declare this two method on global level
public static void commitPref(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String readPref(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
now maintain your logic..
if (!readPref("CHECK_IF_RUN_TODAY", getApplicationContext()).equals(new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date()))){
//YOUR LOGIC
commitPref("CHECK_IF_RUN_TODAY", new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date()), getApplicationContext());
}
but dont forget to upadte your prefrence after your logic
Hope this helps..
I have asked a question before 2 days about daily notification and i put my code and i didn't get any useful answer so i need someone write a code that show notification every 1 hour .
caution (i have write a code doing that but i was have a problem that is every time i open the activity it gives me a notification i don't need that i just want it give a notification at the first time i run app or press a button to start it then the service runs to give notification every 1 hour ) .
Hope anyone can help me .
Thanks.
Well this seems like a two part question, for one the effect that you want to wait and be able to check to start the notifications and to save that value. SharedPrefence's can do this, you could save a boolean value to see whether or not a alarm should be set. Easy enough to access.
public boolean saveSharedBoolValue(String key, Boolean value, Context localContext) {
SharedPreferences sp = localContext.getSharedPreferences("FILE_NAME", 0);
Editor edit = sp.edit();
edit.putBoolean(key, value);
return edit.commit();
}
public Boolean getSharedBoolValue(String key, Context localContext)
{
SharedPreferences sharedPreferences = localContext.getSharedPreferences("FILE_NAME", 0);
Boolean value = sharedPreferences.getBoolean(key, false);
return value;
}
Something like this. With alarm notifications, that start on startup I had this similar issue. What I noticed was that I was creating an alert for earlier than my present time and it created a single notification automatically.
That may not be your issue though,
calendar.setTimeInMillis(System.currentTimeMillis() +5*1000);
this line from your code. Isn't it essentially creating an alarm just right after it starts? 5000 milli seconds aren't much after present time.
I am porting one of my iOS apps over to android, and one of the layouts uses the UIDatePicker with the mode UIDatePickerModeCountDownTimer. I have searched and searched and searched for something even close in android, and the only thing I have come across is the TimePicker widget which only seems to do time of day.
Does something like UIDatePickerModeCountDownTimer exist in android, or do I have to fenagle my own solution for entering this type of time.
There is no one stop shop that includes a date picker and a time picker, both are seperate
so basically you need a date field that shows a DatePickerDialog and a time field that show a TimePickerDialog when clicked
As far as I am aware you will either need a 3rd party library or you will need to create a custom version of the TimePicker.
EDIT :
Basically i've done something similar like your requirement, i have a time picker dialog in dialog fragment, without am/pm indicator. You only have to set the DateFormat to 24 hour format in onCreateDialog method. Something similar like this :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
int hour = 0;
int minute = 0;
// Create a new instance of TimePickerDialog without am/pm and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
I hope this answer help you :)
How can I measure time that ListView/ListActivity takes for rendering its list?
I have a ListActivity that takes a long time to show (on older devices it's ~3s) and I'd like to optimize that.
EDIT:
I already have time between Activity1 and Activity2. During this time, Activity2 is doing some stuff (initializing). Among other things, it renders its list. I want to get time this activity takes to render that list. If total time between activities is 3s, I want to know whether rendering list takes 2.9s or 0.5s....
You could simply ouput the time. For example you could use the logcat
final long t0 = System.currentTimeMillis();
// code to measure
Log.w(TAG, "TEXT" + System.currentTimeMillis()-t0);
Of course you could use any other system for the ouput like a dialog or stuff. Just use what you like.
EDIT:
If you don't want to use a debug message in your code all the time you could do it like this:
Create a class called settings:
public class Settings {
public static final boolean DEBUG = true;
// If you prefer you could do use an enum
// enum debugLevel {SHOW_EVERYMESSAGE, ERRORS, IMPORTANT_MESSAGES, ...}
// In your classes you would have to check that DEBUG is less or equal than
// the debugLevel you want
}
In classes where you want to use a debug message simply do this
import xxx.yyy.Settings
class foo {
final static boolean DEBUG = Settings.DEBUG;
if(DEBUG){
// Debug messages
}
}
Now if you want to disable DEBUG messages you could simply set DEBUG = false in your Settings class.
If you want to measure between two activities you could use intents and send t0 with an intent to the other activity to compute the time. Of course you could include this with if(DEBUG){ /* code */ } statements to spare the sending of the intent in the final release. The if statements should not increase the computation of your code too dramatically.
I cannot tell if Java offers a better implementation using System.currentTimeMillis() or System.nanoTime(). Nevertheless, you should give the TimingLogger class a try. Take a look at this article describing the usage of the TimingLogger helper class.