Showing posts with label Activity. Show all posts
Showing posts with label Activity. Show all posts

2012/04/07

Finishing touches

As I said last time, I reckon there are two things left to do before I have a module that is worth testing.

First - I want to notify the user when a date is entered successfully. Now, this is not essential in the grand scheme of things, because when the whole app is complete, closing SSA will lead the user to a screen which shows the date (among other things). Whether or not I decide to retain it then, I want to have it now, just so that I know it's working when I test it!

The easiest way to do that is with what is called Toast - I imagine because when it is ready, it pops up! Toast is a kind of message that appears in a little box floating on the screen for a set duration.
Toast has three key components (or 'arguments', in this instance). The Context, being the current state of the application; the Text, being what you want your Toast to display; and the Duration, being how long to display it for.

As usual with Android's imaginative variable names, we get something like this:

Context context = getApplicationContext();
        CharSequence text = "You are getting married on " + WedDate + "!";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
As you can see, here we will tell people "You are getting married on []!", for a duration of Toast.LENGTH_LONG. I don't know exactly how long that is, but it's a few seconds; certainly long enough to read this message.

Alright, so on to the second thing I wanted to add - and this is probably entirely temporary: a message on WLA to confirm that the app knows the user's wedding date.
I've put exactly the same Toast, just replacing "You are getting married on " with "Your wedding date is ", so that if I close SSA and get back to WLA at the same time as a Toast appears, I will know which Activity is responsible for it.

In both these cases, I already had the import statement - import android.widget.Toast - otherwise I would have to add it before Eclipse would be happy.

Now, bearing in mind that 1) This is one small corner of the planned app, and 2) That it may not even begin to work when tested - it's time to start testing this thing, and see what breaks!

2012/04/05

Slow Improvement

Aha! It gets better - I have not only discovered that it is possible to make the calendar appear, not just the day/month/year spinners; I have also discovered a method called onDateSetListener. I've even found out how to make a value (like "Yes, a date has been picked") persist from one session to the next.

The calendar thing I'm going to just make note of for now - remembering as I said back here that as long as it works, prettiness comes later.

The method onDateSetListener might explain some of the problems I've had with onClickSetListener - it wasn't quite the right method for what I was doing. I'll try replacing it based on what I've found at the Android Developers' forum and Stack Overflow.

So from
WeddingDate = (DatePicker)findViewById(R.id.datepicker) 
WeddingDate.setOnClickListener(this);
I now have:
WeddingDate = (DatePicker)findViewById(R.id.datepicker);
        mDatePicked = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub } 
I think that's progress, of a sort? At least, Eclipse isn't flagging any major errors, and I can put something into the method stub to cover the last point I mentioned up top - making sure that once a date is picked, the user doesn't have to do it again.

That's where this comes in. Now, the example given isn't quite right - it's for a setting that the user might change at any time, so the setting is re-saved every time the application closes. I want mine to save the setting solely when the user finishes with SSA - if they ever say they want to change the date, I'll get the app to just bring them back to SSA.

So, in WLA I have put
        SharedPreferences settings = getSharedPreferences(WedPrefs,0);
        String WedDate = settings.getString("WedDate", "");
which is aimed at getting together an object called "settings", which in addition to knowing the Wedding Date can also hold other information in future. I've picked my variable names and properties such that they match up with the if block I created here.
Coming back to SSA, where it said //TODO above, I've now got
SharedPreferences settings = getSharedPreferences(WedPrefs,0);
SharedPreferences.Editor editor = settings.edit();
WedDate = String.valueOf(year) + String.valueOf(monthOfYear) + String.valueOf(dayOfMonth);
editor.putString(WedDate);
This still isn't quite right - the string WedDate operates, and becomes year + monthOfYear + dayOfMonth [for example the 19th May 2012 would appear as 2012419* which I think is the easiest way to grab it back out afterwards] but then Eclipse isn't keen on editor.putString(WedDate); and I'm not sure why.

Well, I wasn't. I tried typing it again, and when the tips popped up:
I saw that the putString method requires two separate arguments.
By analogy with the method in WLA, the first argument has to be "WedDate".
Because of the name I've given in SSA, the second argument has to be WedDate. Fortunately one is in "" and the other is not, so that isn't confusing at all!
May want to change one of those names, I'll think about it.


In any case, this has taken a while, but I've nearly finished the initial stage. There are two immediate steps remaining - one, a message to inform the user whether the date is entered successfully, and two, something on the WLA to state whether a date already exists.
Then to build this into the full app, I'll need something to direct the app back from SSA to WLA - if I understand Android convention correctly, that will be automatic as long as I close down SSA correctly.
Then I can start testing it.

*2012419, you say? Yes. 2012 for the year; then Java counts months from 0 to 11, so in spite of being the fifth month May comes out as 4; then 19 for the 19th. I think that the month will show as 4, not 04...
Some systems counting from 0 and others from 1 is the reason for fence-post errors!

2012/04/04

Do we have a date, dear?

I have this ongoing problem of how to persuade my app to listen out for word from the calendar on the front page [for a quick reminder, that looks something like the below:]
I need this page to notice when a date has been entered; save the date somewhere accessible for all other parts of the app; then call up the next page of the app.

As my last few posts have alluded to, this is where not having worked with Android or Java before is really kicking my arse - because this is basic stuff, and I'm having to do a lot of reading and looking at example code to figure it out.
As a result, some of my code may be more apt for an entry on The Daily WTF than Google Play [as the name suggests, the Daily WTF is a blog exposing code that makes programmers go WTF!?]. That in itself doesn't trouble me, as long as it works.

So, with no further ado, here's the next bit I've been working with.



        if(WedDate.matches("")) {
        Intent intent = new Intent(this, SplashScreenActivity.class);
        startActivity(intent); } 
To give a little background, this is in WLA, the main view for the app - and before loading, it is checking whether there is a value for WedDate. If one exists, then great - the app can continue in this page. If however the app doesn't know the date of the wedding, it loads SSA to ask the user for it.
As best I understand it, this is the best way to do things - I don't want to load WLA and then force new users to navigate away from it, and I don't want to make regular users faff about with the date before going on to do useful things.

So far so good - no errors being marked by Eclipse here! Doesn't mean there won't be a problem at run-time, of course, but...baby-steps.
On to SSA, and what we do when the user inputs a date.

2012/03/28

Java Adventures

So, this is where I've hit the problem that Java is not a language I'm familiar with.

So that's why this post is a bit late - I've had to delve into some Java resources to figure out how to distinguish my arse from my elbow.

Anyway, I already had:

public class WeddingLogisticsActivity extends Activity {


/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


I'm fairly sure I need to declare a DatePicker (tell the app to expect a DatePicker, and what to call it), so I added
DatePicker WeddingDate;
This means when I refer to WeddingDate, the app knows what I'm talking about.
Then, from looking at example code, (mostly from hackaday which I mentioned before), I need to tell the app what to do with WeddingDate. And...I can't quite figure that bit out yet. More reading required!

2012/03/27

Code: Import, Activity, Extend

This next bit is focussed in the source-code folder (src), in a section named WeddingLogisticActivity.java

Eclipse has automatically added in what you see on the left here.
First of all we have the name of the package, which will eventually uniquely identify Wedding Logistics on Google Play.
Then we have the import statements. One analogy for these is academic references - if you write an essay which doesn't show the sources for your statements of fact then your readers will think you're making it up. Well, think of your app as a very sceptical reader - unless every statement is ultimately backed up in a reference [unless every method is in an imported library], it won't believe you [won't be able to execute that method].

The import statements I have already just cover me for the basic operation of the Android Operating System, and for the initial Activity that I'll get to in a moment, so I'll be adding a few, to deal with the Context (the situation on your Android), the View (what's on screen), an OnClickListener (to notice when, say, the user clicks on a button), deal with Buttons, Toast (messages that appear temporarily on screen. So called because they pop-up, I think), and a DatePicker, &c.
I've got the outline of my list from the excellent hackaday.com Android Tutorial and then added one for the DatePicker. Eclipse helpfully highlights all of the import statements I'm not using at the moment
(see the little row of symbols on the left?) so I can remove any unnecessary imports once the code is finished. Equally, as I expand the app, I'm likely to need to add more imports to cover new features.

Then we declare a public class called WeddingLogisticsActivity (henceforth abbreviated to WLA), and that it extends the core Activity.
Just to unpack that a bit - public means that other processes can refer to WLA; class - well, for a full definition of that I'll give you the wikipedia article, but the short version is that it means I could have several examples of WLA, based on the definition of it given here.
As to the bit about extending Activity, well, the Activity is a core Android class which handles some basic stuff for me - saying WLA extends Activity is a way of telling the system I want WLA to start with all the attributes of Activity, and then I want to give it some more.

At present, all WLA does which is apparent to the user is load the screen from last time; however, if the user attempts to enter the date, nothing will happen. If they click Done, nothing will happen. Unfortunately, I'm out of time, so I'll get to rectifying that next time!