Search code examples
androidtextviewmpandroidchartmarker

MPAndroidChart Marker's TextView appearance


I'm trying to add a marker when I touch a point on a LineChart. I tried different ways for it to work, but I can't get it to present the text in the TextView.

private IMarker markerView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    mChart = findViewById(R.id.peaks_chart);
    mChart.setTouchEnabled(true);
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setPinchZoom(true);
    mChart.setBackgroundColor(Color.LTGRAY);

    markerView = new CustomMarkerView(getApplicationContext(),R.layout.tv_content);
    mChart.setMarker(markerView);
    mChart.setDrawMarkers(true);

    mChart.getLegend().setEnabled(true);

    XAxis xl = mChart.getXAxis();
    xl.setTypeface(mTfLight);
    xl.setTextColor(Color.WHITE);
    xl.setDrawGridLines(false);
    xl.setDrawLabels(true);
    xl.setEnabled(true);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(mTfLight);
    leftAxis.setTextColor(Color.WHITE);
    leftAxis.setDrawGridLines(false);
    leftAxis.setDrawLabels(true);
    leftAxis.setEnabled(true);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setEnabled(false);

    dataSet_r = new LineDataSet(null,"right nost");
    dataSet_r.setDrawValues(true);
    rChartLineData = new LineData();
}

After I enter entries to the DataSet I call notifyDataSetChanged() and invalidate().

Custom marker view class:

public class CustomMarkerView extends MarkerView implements IMarker
{
    private TextView textView;

    public CustomMarkerView(Context context, int layoutResource)
    {
        super(context, layoutResource);
        textView = findViewById(R.id.tvContent);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight)
    {
        textView.setText(String.valueOf(e.getX())+"777");
        super.refreshContent(e, highlight);
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/battery_full"
android:gravity="center"
 >

<TextView
    android:id="@+id/tvContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="1234"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="14sp" />

</RelativeLayout>

The chart is drawn as expected, but when I press on a point on the chart is shows only the background image and no Text (even if I don't try to change the text there's no text in the TextView).

What am I doing wrong???


Solution

  • Tested your case, the problem is

    android:textAlignment="center"
    

    of your marker, remove it and the text will be displayed.

    Marker has some conflicts with textAlignment and gravity attributes. Just make your TextView width wrap_content and it will be centered.