1

In my application I have some bar charts using MPAndroidChart, when I touch in one bar an activity opens showing some informations about the value selected. I'm using "OnChartValueSelectedListener" to do that.

The problem is which this is too much sensible to touch. When I touch the screen just to scroll it, if I touch in one value, one activity opens.

I'm looking for something like "OnLongClickListener" to avoid the activity to be opened every time I touch the bars. But I could not find anything. There's some way to emulate an "long touch value" on MP Android Chart?

That is part of my code:

mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
            // registra o estado da selecao para chamar esta funcao na funcao onNothingSelected
            entry = e;
            index = dataSetIndex;
            highlight = h;
            // caso algum cliente tenha sido cadastrado no dia selecionado
            if(e.getVal() > 0) {
                ViewUtil.exibirMensagemAguarde(R.string.aguarde, R.string.carregando_dados, OverviewActivity.this);
                // guarda os dados para serem usados pelo metodo onNothingSelected
                entry = e;
                index = dataSetIndex;
                highlight = h;
                // obtem o ano atual
                int currentYear = Calendar.getInstance().get(Calendar.YEAR);
                // obtem o dia selecionado e concatena o ano atual
                dateSelected = xVals.get(h.getXIndex()) + "/" + currentYear;
                // chama a tarefa assincrona que obtem os clientes cadastrados no dia selecionado
                // e abre a activity
                ClientesCadastradosDiaAsync task = new ClientesCadastradosDiaAsync();
                task.execute();
            }

        }

        @Override
        public void onNothingSelected(){
            onValueSelected(entry, index, highlight);
        }
    });
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Breno Macena
  • 449
  • 8
  • 19

1 Answers1

0

Your solution would involve creating a class that implements OnChartGestureListener

Looking at the linked javadocs above, you can immediately see there is a method:

void onChartLongPressed(MotionEvent me);

You would have to implement this method with the functionality you want. It would probably involve getting the raw pixel touch points from the MotionEvent and converting them to x and y values on the chart. Then you could open the appropriate activity as per your requirement. Referring to this answer we can do that like this:

@Override
public void onChartLongPressed(MotionEvent me) {
    float tappedX = me.getX();
    float tappedY = me.getY();
    MPPointD point = mChart.getTransformer(YAxis.AxisDependency.LEFT).getValuesByTouchPoint(tappedX, tappedY);
    Log.d(TAG, "long pressed at: " + point.x + "," + point.y);
    //TODO: check for long presses that don't correspond to a value on the chart
    //launch the Activity as per your requirement
}

There is an example of a custom OnChartGestureListener inside the example project here

David Rawson
  • 20,912
  • 7
  • 88
  • 124