Writing code at the creation of a fragment

All the examples I’ve seen so far for dealing with programmatically handling things from a view have involved an activity and the layout associated with it. But when I am working with Android Studio, whenever I create a new activity things are a little different and I get two layouts and things are happening in a fragment rather than the first layout. I don’t totally get the whole thing yet, I’m working on wrapping my mind around it. But today I did figure out something that was really giving me fits and I want to document it for later.

I wanted to start a new activity and in the view for that activity display a chart using the Android GraphView. Jonas provides nice documentation with example code on how to do this. But the fragment thing was really messing me up. I couldn’t figure out where to put the code and how to get it to work. The example I was interested in looks like this:

// init example series data
GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
      new GraphViewData(1, 2.0d)
      , new GraphViewData(2, 1.5d)
      , new GraphViewData(3, 2.5d)
      , new GraphViewData(4, 1.0d)
});
 
GraphView graphView = new LineGraphView(
      this // context
      , "GraphViewDemo" // heading
);
graphView.addSeries(exampleSeries); // data
 
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);

But I would put it in different places and crash the app or get compiler errors before I even started. I did a lot of searching and reading before I got it to work and I guess I don’t need to rehash all that. It was the accepted answer on this stackoverflow question that finally put the last piece in place for me though.

When I create a new activity the class for it has a class inside that creates the fragment:

public static class PlaceholderFragment extends Fragment {
 
        public PlaceholderFragment() {
        }
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

This made sense as the place to do my work but I couldn’t figure out how. I had trouble trying to reference the view. But it was right in front of me the whole time. Here is what finally worked.

public static class PlaceholderFragment extends Fragment {
 
        public PlaceholderFragment() {
 
        }
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_show_graph, container, false);
 
 
            //I cannot believe I finally figured this out.
            GraphViewSeries avSeries = new GraphViewSeries(new GraphView.GraphViewData[] {
                    new GraphView.GraphViewData(1, 223)
                    , new GraphView.GraphViewData(2, 221)
                    , new GraphView.GraphViewData(3, 220)
                    , new GraphView.GraphViewData(4, 221)
            });
 
            GraphView monthAverage = new LineGraphView(this.getActivity(), "Jan Data");
            monthAverage.addSeries(avSeries);
 
            LinearLayout layout = (LinearLayout)  rootView.findViewById(R.id.graphM);
            layout.addView(monthAverage);
            //this is the end of my part
 
            return rootView;
 
        }
 
    }

Oh – here’s the xml for the layout. I don’t think it matters a whole lot – but this is where the graphM id is located.

 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/graphM"
    tools:context="org.pyrrhic.eatwatch.showGraph$PlaceholderFragment">
 
>

I was so happy when I ran this and I saw my graph pop up rather than the error message from the app crashing. It was a banner moment in my day.

Setting Up My AndroidStudio Project on GitHub

I started a project to do my homework for the week in the Android class I’m taking. I’ve been doing the bulk of my work on a desktop machine in my office. This week-end I also set up a dev environment on my Fedora laptop at home. I don’t want to have to monkey with copying files and carrying them back and forth so today I set up a GitHub repo so that I could use it to keep things in sync.

I’m no git expert. In the past what I’ve done is create a project in github with a couple pieces in place. Then I pull that down to a directory on my machine, add the files I need and then push all that back up.

Today I took a little bit of a different approach. I created the project. Then in bash I went to the root directory of the project and set things up. It took me a minute to get it all figured out. The git reference on remotes was a huge help. There is also the JetBrains documentation on git with IntelliJ which is what Android studio is built on.

For my own reference – once I got the project built I needed to add everything. First I went to github and made a repo – but I made it empty, not like I usually do. Then I went to the root of my project in bash.

git add .

and then make an initial commit

git commit -m 'initial commit'

I set up the remote

git remote add origin https://github.com/bittercode/learnandplay.git (bittercode is my github user name and learnandplay is the name of the repo I set up.)

Then I pushed the code to the remote

git push -u origin master

And that put it all up at github. Now I should be able to go home and pull it all down there. I also set up AndroidStudio so that it now handles all the git stuff. When I created a new activity it asked me about adding them to git – so I just said that it should default to yes and now I’m on the fast track to happy days.

Messing with App Inventor

I have been so busy I haven’t played with App Inventor in a long time. Yesterday I read an interesting article about MIT taking over the project. That got me wanting to get things set up again and see where it was at.

It was frustrating because I couldn’t get my phone to connect to the software. I was googling and wracking my brain to figure out the issue. I tried on multiple machines with multiple OS’s. After many hours a very simple solution to my problems finally crept into my brain. I went home, got another USB cable and all my problems went away. Annoying but I’m glad I got it figured out.

On a related note, the App Inventor software is run from the browser. One piece, the “Blocks Editor” is written in Java. So the browser downloads a jnlp file and it needs to be run. I was using Chrome. But when I told it to auto-open the file after downloading it wouldn’t give me a choice in what program did the opening. It was using the open-jdk (which the docs say not to use) and I couldn’t figure out how to get it to let me switch to the Oracle jdk. So I went to running it from Firefox, where I had no problem setting this up correctly. There is a lot I really like about Chrome, but when I want to get stuff done, I inevitably end up back on Firefox

Amazon Appstore for Android is Open

I got an email from Amazon today that their Appstore was open. I was able to install the Appstore app without any issues (apparently AT&T customers will have issues or have to do something different, that’s what the install notes said anyway. sucks to be them.) and hopped right in.

They are doing a free paid app a day for an unknown number of days. Today’s free app is Angry Birds Rio. It’s there to grab for the next 10 hours or so. My son is going to be beside himself when I let him know we’ve got new Angry Birds levels to play. This time it’s setting other birds free rather than fighting the pigs. Man I hate those pigs.

Installing Angry Birds Rio was a piece of cake, pretty much like installing it from the regular Android App Store. So not a ton to say other than I’m pretty stoked about this. I do a lot of business with Amazon and this just pulls my mobile and shopping world a little tighter together. I like having everything rolled up together like that.

But – and this is important – I don’t have to have it that way if I don’t want it to be. And if Amazon makes any large missteps, I’ve got other options. Which is why you’ll see me sticking with Android unless something better comes along that is also open.

Amazon App Store for Android

Looks like Amazon will be rolling out an app store for Android. This makes me happy. It’s two of my favorite things coming even closer together, sort of like chocolate and peanut butter I guess.

Which reminds me of something funny, that has nothing to do with Google, Android or Amazon. I was at a conference in Bangkok last week and we were doing this thing where we moved around to different tables to answer various questions with other attendees. Each move put me with different people from all over. There was candy on the tables. At one point I was at a table with a woman from South Africa and she mentioned she was on the look-out for the peanut-butter cups. I asked, “They eat peanut butter in South Africa?” (I know places that don’t.) She said, “Yes, but not with chocolate.” Wow – they’ve been missing out down there. Two great tastes that taste great together.

Android 2.0 is out

Android 2.0 is now available from Google. This puts to bed concerns about Google not releasing the sdk or perhaps being in some kind of exclusivity deal with Verizon around 2.0. The release notes give a nice overview of what is there. Those who already have the SDK can grab the updated tools as SDK components, everyone else will pick up everything when downloading the new SDK.