Java – BarChart Item Click How do I display a pop-up menu nearby?

BarChart Item Click How do I display a pop-up menu nearby?… here is a solution to the problem.

BarChart Item Click How do I display a pop-up menu nearby?

How do I display a pop-up menu near BarChart Item click?

I’ve created an app to display a pop-up menu when a bar chart item is clicked. The problem is that the pop-up menu does not appear near the clicked bar chart item. It is always displayed at the top. So help me display the pop-up menu near the bar chart item click.

Main Activity:

package com.example.saravanakumars.spinnerchart;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    String[] chart = {"Select", "Bar Chart", "Pie Chart"};

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

final Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter aa = new ArrayAdapter(this, android. R.layout.simple_spinner_item, chart);
        aa.setDropDownViewResource(android. R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(aa);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {

String items = spinner.getSelectedItem().toString();
                switch (items) {
                    case "Select":
                        Toast.makeText(getApplicationContext(),"Please Select Bar or Pie Chart",Toast.LENGTH_SHORT).show();
                        break;
                    case "Bar Chart":
                        Intent intent = new Intent(MainActivity.this, barchart.class);
                        startActivity(intent);
                        break;
                    case "Pie Chart":
                        Intent p = new Intent(MainActivity.this, piechart.class);
                        startActivity(p);
                        break;
                }

}

@Override
            public void onNothingSelected(AdapterView<?> arg0) {
                 TODO Auto-generated method stub

}

});
    }

@Override
            public boolean onCreateOptionsMenu(Menu menu) {
                 Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
            }

}

Bar chart .java

package com.example.saravanakumars.spinnerchart;

/**
 * Created by saravanakumars on 9/18/2017.
 */
import java.util.ArrayList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class barchart extends AppCompatActivity {
    BarChart chart ;
    ArrayList<BarEntry> BARENTRY ;
    ArrayList<String> BarEntryLabels ;
    BarDataSet Bardataset ;
    BarData BARDATA ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bar_chart);
        chart = (BarChart) findViewById(R.id.barchart);

BARENTRY = new ArrayList<>();

BarEntryLabels = new ArrayList<String>();

AddValuesToBARENTRY();

AddValuesToBarEntryLabels();

Bardataset = new BarDataSet(BARENTRY, "Projects");

BARDATA = new BarData(BarEntryLabels, Bardataset);

Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

chart.setData(BARDATA);

chart.animateY(3000);
        chart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popup = new PopupMenu(barchart.this,chart);
//
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                popup.show();
//
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(barchart.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
                        return true;
//                    }
//                });
//            }
//        });
        chart.setOnChartValueSelectedListener( new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

PopupMenu popup = new PopupMenu(barchart.this,chart);

popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                popup.show();

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(barchart.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });

}

@Override
            public void onNothingSelected() {

}
        });

}

public void AddValuesToBARENTRY(){

BARENTRY.add(new BarEntry(2f, 0));
        BARENTRY.add(new BarEntry(4f, 1));
        BARENTRY.add(new BarEntry(6f, 2));
        BARENTRY.add(new BarEntry(8f, 3));
        BARENTRY.add(new BarEntry(7f, 4));
        BARENTRY.add(new BarEntry(3f, 5));

}

public void AddValuesToBarEntryLabels(){

BarEntryLabels.add("January");
        BarEntryLabels.add("February");
        BarEntryLabels.add("March");
        BarEntryLabels.add("April");
        BarEntryLabels.add("May");
        BarEntryLabels.add("June");

}

}

Pop-up menu .xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/one"
        android:title="Type-1"/>

<item
        android:id="@+id/two"
        android:title="Type-2"/>
</menu>

Thank you.

Solution

Try MarkerView available in MPAndroidChart. Check this link.

Related Problems and Solutions