0

I am trying to return layout with chart and some buttons from onCreateView in fragment. I don't know how to combine the two views into one. I can return only the chart or layout without the chart:

    public class ChartFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // LineGraph - class which draws the graph using achartengine
        final LineGraph lineGraph = new LineGraph();
        final GraphicalView chart = lineGraph.getGraphicalView(inflater.getContext());
        final View view = inflater.inflate(R.layout.chart, container, false);


        Button drawChartButton = (Button) view.findViewById(R.id.button_draw_chart);

        drawChartButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // update chart for new data
            }
        });

        return chart; //return the chart only
        // return view; // return layout but without the chart

    }

}

In activity the following code:

 Button chartButton = (Button) findViewById(R.id.button_draw_chart);
    chartButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
        LineGraph lineGrpah = new LineGraph();

        GraphicalView testChart = lineGrpah
            .getGraphicalView(SimpleApp.this);
        layout.addView(testChart, new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            }
        });

and xml:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >


   <LinearLayout
       android:id="@+id/chart"
       android:layout_width="600dp"
       android:layout_height="350dp"
       android:layout_margin="15dp" >

   </LinearLayout>

   <Button
       android:id="@+id/button_draw_chart"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/button_draw_chart" />

</LinearLayout>

works properly but I don't know how to do this in fragment.

rgb
  • 1,750
  • 1
  • 20
  • 35

1 Answers1

0

In onCreateView() just adapt the code you wrote in chartButton's onClick() method, specifically:

view.addView(chart, new LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        }
    });

and then return view.

Venator85
  • 10,245
  • 7
  • 42
  • 57