0

I want to show a bar graph in my Activity in a fragment but it is showing inflating issue. So, I changed my Activity to FragmentActivity it worked my application is running but Fragment part output is not displaying.

Here is my Code I am unable to find out what mistake I have done:

MainActivity:

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <fragment
            android:id="@+id/fghis"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            class="com.anu.actualdemobargraph.HistoryFrag" />
</LinearLayout>

HistoryFrag:

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.echo.holographlibrary.Bar;
import com.echo.holographlibrary.BarGraph;
import com.echo.holographlibrary.BarGraph.OnBarClickedListener;

public class HistoryFrag extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.historyfrag, container,false);
        BarGraph g = (BarGraph)view.findViewById(R.id.bargraph);
        ArrayList<Bar> points = new ArrayList<Bar>();
        Bar d = new Bar();
        d.setColor(Color.parseColor("#c6c6c6"));
        d.setName("Test1");
        d.setValue(10);
        Bar d2 = new Bar();
        d2.setColor(Color.parseColor("#c6c6c6"));
        d2.setName("Test2");
        d2.setValue(20);
        Bar d4 = new Bar();
        d4.setColor(Color.parseColor("#c6c6c6"));
        d4.setName("Test2");
        d4.setValue(20);
        Bar d5 = new Bar();
        d5.setColor(Color.parseColor("#FFBB33"));
        d5.setName("Test2");
        d5.setValue(20);
        Bar d6 = new Bar();
        d6.setColor(Color.parseColor("#c6c6c6"));
        d6.setName("Test2");
        d6.setValue(20);
        Bar d7 = new Bar();
        d7.setColor(Color.parseColor("#c6c6c6"));
        d7.setName("Test2");
        d7.setValue(20);
        Bar d8 = new Bar();
        d8.setColor(Color.parseColor("#c6c6c6"));
        d8.setName("Test2");
        d8.setValue(20);
        Bar d3 = new Bar();
        d3.setColor(Color.parseColor("#c6c6c6"));
        d3.setName("Test3");
        d3.setValue(50);
//        d3.setStackedBar(true);
//        d3.AddStackValue(new BarStackSegment(2, Color.parseColor("#FFBB33")));
//        d3.AddStackValue(new BarStackSegment(4, Color.RED));
        points.add(d);
        points.add(d2);
        points.add(d3);
        points.add(d4);
        points.add(d5);
        points.add(d6);
        points.add(d7);
        points.add(d8);


        g.setUnit("meters");
        g.appendUnit(true);
        g.setBars(points);

        g.setOnBarClickedListener(new OnBarClickedListener(){

            @Override
            public void onClick(int index) {

            }

        });
        return view;
    }
    public HistoryFrag() {
        // TODO Auto-generated constructor stub
    }
}

historyfrag.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.echo.holographlibrary.BarGraph
        android:id="@+id/bargraph"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

</LinearLayout>

In historyfrag.xml, the BarGraph is taken from library there is no problem in the library.

Thanks in Advance.

1 Answers1

0

You need to attach the Fragment to your Activity using the FragmentManager. Depending on whether you want to use the framework Fragment or the support Fragment this process differs slightly.

Framework Fragment

This is the Fragment class which is built into the Android Framework and is available in API 11 and above.

To use the Fragment class which is part of the Android framework you need to change the import statement in your HistoryFrag class from:

import android.support.v4.app.Fragment;

to

import android.app.Fragment;

Then in your Activity onCreate method you need to add:

getFragmentManager()
    .beginTransaction()
    .replace(R.id.fghis, new HistoryFrag())
    .commit();

Support Library Fragment

If you want to support API levels <11 then you would need to use the Fragment class from the Support Library. This backports the Fragment functionality to API 4.

If you wanted to use the Fragment class from the Support Library then you would need to make sure you are extending FragmentActivity and leave the import statement in your HistoryFrag class as android.support.v4.app.Fragment.

Then in your Activity onCreate() method add something like:

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.fghis, new HistoryFrag())
    .commit();

That will then attach the Fragment to the Activity.

Jahnold
  • 7,623
  • 2
  • 37
  • 31