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.

<LinearLayout 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">
 
</LinearLayout>

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.

Posted in Geeks In Action, java, Programming Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*