Skip to content Skip to sidebar Skip to footer

Listview With A Mpandroidchart(or Whatever) Below

Edit: I changed the title because isn't about MPAndroidChart, the same issue would happen with another view below the ListView. I am trying to set a layout for an activity in m

Solution 1:

Try this code

Portrait Mode

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#22bbcc"android:orientation="vertical"><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="0dp"android:layout_margin="4dp"android:layout_weight="0.50"android:background="#ccbb22" /><LinearLayoutandroid:id="@+id/LinearLayout_chart"android:layout_width="match_parent"android:layout_height="0dp"android:layout_margin="4dp"android:layout_weight="0.50"android:background="#ddaaaa"android:orientation="horizontal"><!--You can have your chart here --></LinearLayout></LinearLayout>

enter image description here

Landscape Mode

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="4dp"android:background="#22bbcc"android:orientation="horizontal"><ListViewandroid:id="@+id/listView"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="4dp"android:layout_weight="0.50"android:background="#ccbb22" /><LinearLayoutandroid:id="@+id/LinearLayout_chart"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="4dp"android:layout_weight="0.50"android:background="#ddaaaa"android:orientation="horizontal"><!--You can have your chart here --></LinearLayout></LinearLayout>

enter image description here

if possible add the PieChart dynamically to the LinerLayout

private PieChart mChart;
privatefloat[] yData = {5, 4, 5, 2}; 
private String[] xData = {"Item_one", "Item_two", "Item_Three","Item_Four"};
linearLayoutTwo = (LinearLayout)findViewById(R.id.LinearLayout_chart);
mChart = new PieChart(this);

    // configure pie chart
    mChart.setUsePercentValues(true);
    mChart.setDescription("");


    // enable hole and configure
    mChart.setDrawHoleEnabled(false);
    mChart.setHoleColorTransparent(true);
    mChart.setHoleRadius(0);
    mChart.setTransparentCircleRadius(0);

    // enable rotation of the chart by touch
    mChart.setRotationAngle(0);
    mChart.setRotationEnabled(false);

    ArrayList<Entry> yVals1 = new ArrayList<Entry>();

    for (int i = 0; i < yData.length; i++)
        yVals1.add(new Entry(yData[i], i));

    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < xData.length; i++)
        xVals.add(xData[i]);

    // create pie data set
    PieDataSet dataSet = new PieDataSet(yVals1, "");
    dataSet.setSliceSpace(0);
    dataSet.setSelectionShift(1);

    // add many colors
    ArrayList<Integer> colors = new ArrayList<Integer>();

    for (int c : newint[]{Color.rgb(124, 185, 232), Color.rgb(178, 132, 190), Color.rgb(201, 255, 229),
            Color.rgb(100, 148, 143)}) {
        colors.add(c);
    }


    colors.add(ColorTemplate.getHoloBlue());
    dataSet.setColors(colors);

    // instantiate pie data object now
    PieData data = new PieData(xVals, dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(9f);
    data.setValueTextColor(Color.BLACK);

    mChart.setData(data);

    // undo all highlights
    mChart.highlightValues(null);

    // update pie chart
    mChart.invalidate();
    linearLayoutTwo.addView(mChart);
    // customize legends
    Legend l = mChart.getLegend();

    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    l.setXEntrySpace(4);
    l.setYEntrySpace(4);

Solution 2:

Finally I found a solutions that make what I wanted.

I post this solution here, maybe helps someone.

What I have done is just insert the chart as a footer of the ListView, so I have just the scroll of the listview and at the end de MPAndroidChart (as I wanted).

To achieve that I have made a new layout file, chartlayout.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="match_parent"><com.github.mikephil.charting.charts.LineChartandroid:id="@+id/chart"android:layout_width="match_parent"android:layout_height="200dp"/></RelativeLayout>

And so in my activity java file I take the layout from chartlayout with the inflater service, then I get the chart view and finally I add it to the list as footer:

//get layout
    chartLayout = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.chartlayout, null, false);

    //get view
    mChart = (LineChart) chartLayout.findViewById(R.id.chart);

    /*  
    *   chart stuff
    */

    ...

    mylistView.addFooterView(chartLayout);

Post a Comment for "Listview With A Mpandroidchart(or Whatever) Below"