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);

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() {
        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() {
        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");
            LinearLayout layout = (LinearLayout)  rootView.findViewById(R.id.graphM);
            //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.


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.

Netbeans 6.5 is out

My favorite IDE – has a new version now available. (Actually it’s been available for a couple days – I just didn’t get around to typing this up.) You can download it here and read about the features here.Read all of the documentation here and take a video tour of 6.5 here.

Netbeans encompasses a wide range of languages and platforms. One that is missing if you look at the features is Python – but there is and early access release available with python support for 6.5. I cannot wait until this is mature – I think I will have reached a point where I can use a single ide for every situation where I think an ide is appropriate.

Netbeans 6.5 Beta

Netbeans 6.5 beta is available for download now. The list of features is just incredible. Every release just impresses me more. I think when I shuffle on out of DBA land, and have time to write more code, I may be able to get away with using netbeans for all development. Nice.

Below this is text from their announcement listing new/major features, etc. It does a lot more than what is in this list – but this hits a lot of things that maybe people don’t realize is there.

Continue reading “Netbeans 6.5 Beta”

Netbeans 6.0 is Final

Netbeans 6.0 is final and if you are a developer – you’d best go get it and check it out. But maybe you are not convinced. Maybe you ask, “What is in this for me, other than a free tool?” Well lets look at a quick list of what technologies are supported by this release of netbeans. Ajax , C/C++ , Databases , Desktop , Editor , GUI Builder , Java EE , Java ME , Java SE , Javascript , Mobile , Profiler , Refactor , REST , Rich Client Platform , Ruby , SOA , SOAP , UML , Web , WSDL , XML.

This is an immensely useful, open, free, multiplatform tool. It is the kind of thing that really gets me excited. (And no – I don’t have the day off – just waiting for some processes to complete and ran into a few interesting things I just had to point out here at me blog.)