aChartEngine, GraphicalView OnClickListener not working - android

I'm new to android and am using aChartEngine to create a bar chart. I want to capture the x and y values when a user clicks on the chart. I have looked at the demos from aChartEngine and have my chart creating fine. However the onClickListner does not work when I click on the graphy. I have defined the listener in the OnResume method but it doesn't work. Any ideas would be greatly appreciated. Am I missing something here?
Here is my OnResume method which is taken from XYChartBuilder
#Override
protected void onResume() {
super.onResume();
Toast.makeText(Clickable2.this, "In OnResume", Toast.LENGTH_SHORT).show();
if (mChartView == null) {
Toast.makeText(Clickable2.this, "mChartView is null", Toast.LENGTH_SHORT).show();
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getBarChartView(this,mDatasetMethod(titles,x,values),renderer,Type.DEFAULT);
renderer.setClickEnabled(true);
renderer.setSelectableBuffer(100);
//OnClickListener
mChartView.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View v) {
Toast.makeText(Clickable2.this, "ON CLICK", Toast.LENGTH_SHORT).show();
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
double[] xy = mChartView.toRealPoint(0);
if (seriesSelection == null) {
Toast.makeText(Clickable2.this, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
Clickable2.this,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
+ " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
}
}
});
//LongClickListener
mChartView.setOnLongClickListener(new View.OnLongClickListener() {
// #Override
public boolean onLongClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
Toast.makeText(Clickable2.this, "ON LONG CLICK", Toast.LENGTH_SHORT).show();
if (seriesSelection == null) {
Toast.makeText(Clickable2.this, "No chart element was long pressed",
Toast.LENGTH_SHORT);
return false; // no chart element was long pressed, so let something
// else handle the event
} else {
Toast.makeText(Clickable2.this, "Chart element in series index "
+ seriesSelection.getSeriesIndex() + " data point index "
+ seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
return true; // the element was long pressed - the event has been
// handled
}
}
});
mChartView.addZoomListener(new ZoomListener() {
public void zoomApplied(ZoomEvent e) {
String type = "out";
if (e.isZoomIn()) {
type = "in";
}
System.out.println("Zoom " + type + " rate " + e.getZoomRate());
}
public void zoomReset() {
System.out.println("Reset");
}
}, true, true);
mChartView.addPanListener(new PanListener() {
public void panApplied() {
System.out.println("New X range=[" + renderer.getXAxisMin() + ", " + renderer.getXAxisMax()
+ "], Y range=[" + renderer.getYAxisMax() + ", " + renderer.getYAxisMax() + "]");
}
});
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
boolean enabled = mDataset.getSeriesCount() > 0;
} else {
mChartView.repaint();
Toast.makeText(Clickable2.this, "mChartView is NOT null", Toast.LENGTH_SHORT).show();
}
}

Can you check your code for some definition of an object like XYMultipleSeriesRenderer?
Maybe you forgot to set the setClickEnabled method to true?!
Or maybe you should try:
setOnTouchListener
with the method:
public boolean onTouch(View v, MotionEvent event)
It works for me.

Change the value of parameter of setSelectableBuffer method to a bigger number.

Related

Android Studio PieChart convert String/float to Integer - Toast

I want to make PieChart and when you will press on the your chosen field on PieChart, then you will see Value of Chart (integer 15.. ect.)
I have problem with Toast.maketext when show value of "counter" show value 15.0 insted only 15. I want to have number without .0 at the end.
When I was trying this option below (convert)the app was stopped working:
int x = Integer.parseInt(counter);
int x = Integer.valueOf(counter)
try{ x = Integer.parseInt(counter)}
catch(NumberFormat Exception nfe){}
My code:
pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
Log.d(TAG, "onValueSelected: Value select from chart.");
Log.d(TAG, "onValueSelected: " + e.toString());
Log.d(TAG, "onValueSelected: " + h.toString());
int pos1 = e.toString().indexOf("y: ");
String counter = e.toString().substring(pos1 + 3);
for(int i = 0; i < yData.length; i++){
if(yData[i] == Float.parseFloat(counter)){
pos1 = i;
break;
}
}
String days = xData[pos1];
/*
1.) int x = Integer.parseInt(counter);
2.) int x = Integer.valueOf(counter)
3.)try{ x = Integer.parseInt(counter)}
catch(NumberFormat Exception nfe){}
*/
Toast.makeText(Main2Activity.this, days+ " Day" + "counter" + x, Toast.LENGTH_LONG).show();
//Toast.makeText(Main2Activity.this, days+ " Day" + "counter" + counter, Toast.LENGTH_LONG).show();
//add counter to Toast if you want to show counter
}
#Override
public void onNothingSelected() {
}
});
Thank you for answers :)
Can you try
System.out.print(String.format("%1.0f", counter));
Toast.makeText(Main2Activity.this, days+ " Day" + "counter" + String.format("%1.0f", counter), Toast.LENGTH_LONG).show();
For now I was solving like that:
Log.d(TAG, "onValueSelected: counter num " +counter);
for(int i = 0; i < yData.length; i++){
if(yData[i] == Float.parseFloat(counter)){
pos1 = i;
break;
}
}
String days = xData[pos1];
switch (pos1) {
case 0:
Toast.makeText(Main2Activity.this, days + " Day " + "\n" + "Numbers " +numberBad, Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(Main2Activity.this, days + " Day " + "\n" + "Numbers " +numberGod, Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(Main2Activity.this, days + " Day " + "\n" + "Numbers " +numberNor, Toast.LENGTH_SHORT).show();
break;
default:
break;
}

Use AccessibilityNodeInfo to turn switch on and off

Okay, so I want to know if it is possible to use AccessibilityService/AccessibilityNodeInfo to turn an android.widget.Switch on/off? Here is my current code and it works to click the text on the screen, but it does not turn a switch on/off.
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if (source == null) {
return;
}
Log.v(TAG, "Event happened");
processUI(source);
}
And of course, the processUI does everything like compares package, text, etc. and sends it all to clickScreen where it actually clicks the screen.
protected void clickScreen(AccessibilityNodeInfo source, final String text, final String type, final int length)
{
Log.v(TAG, "Clicking: " + source + " / " + text + " / " + type + " / " + length);
if (text == null)
{
Log.v(TAG, "Text is NULL");
return;
}
List<AccessibilityNodeInfo> list = source.findAccessibilityNodeInfosByText(text);
for (AccessibilityNodeInfo node : list) {
Log.i(TAG, "SHOULD BE CLICKING: " + node);
node.performAction(4);
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
list = source
.findAccessibilityNodeInfosByText(text);
for (AccessibilityNodeInfo node : list) {
Log.i(TAG, "MIGHT BE CLICKING: " + node);
node.performAction(4);
node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}

Click/Touch is not working for donut chart using achartengine

Hi trying to develop donut chart using achartengine library in android. my requirement is when I click on any part of donut chart i need to highlight and display respective label in center.
but when I am using onclick/ontouch listener irrespective of click its returning null.
mChartView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v == mChartView2){
Log.i("view", ""+((GraphicalView) v).getCurrentSeriesAndPoint());
}
Log.i("dfg", ""+((GraphicalView) v).getCurrentSeriesAndPoint());
SeriesSelection seriesSelection = ((GraphicalView) v).getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(context1, "No chart element selected", Toast.LENGTH_SHORT)
.show();
}else {
for (int i = 0; i < categorySeries.getCategoriesCount(); i++) {
renderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
}
mChartView2.repaint();
Toast.makeText(
context1,
"Chart data point index " + seriesSelection.getPointIndex() + " selected"
+ " point value=" + seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});

Detect the coordinates on the screen

I want to detect the coordinates of a map are touched on the screen with a finger. The problem is when I touch the screen with a finger the coordinates are not displayed with a toast.
I am using Google Maps v2. My code:
#Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)
{
CrearLugar();
}
return super.onTouchEvent(event);
}
public void CrearLugar()
{
final GoogleMap mMap=null;
mMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Projection proj=mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"Lat: " + point.latitude + "\n" +
"Lng: " + point.longitude + "\n" +
"X: " + coord.x + " - Y: " + coord.y,
Toast.LENGTH_SHORT).show();
}
});
}
Everything is correct except when I touch the screen with a finger. When I do it, I want to display the coordinates of the place touched, but I can't watch anything, only the map.
What's wrong?
Thanks.
The click listener has to be set in any of the creators of the activity (onCreate, onResume, onPause, ...). Note that you are setting the listener to (listen) for changes.
You can still give some use to the onTouch overriden method. It'll help to get the absolute coordinates where the user clicked in your screen, but it's completely independent from the map. (btw, what do you need the absoluta coordinates for?).
Try that:
#Override
public boolean onTouchEvent(MotionEvent event)
{
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"X: " + event.getX() + " - Y: " + event.getY(),
Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate view
// Inflate map view
mMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng point) {
Projection proj=mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(
MapaLugaresActivity.this,
"Click\n" +
"Lat: " + point.latitude + "\n" +
"Lng: " + point.longitude + "\n",
Toast.LENGTH_SHORT).show();
}
});
}

android: How to change ListViewItem Background on runtime? (Not for selection)

In my android app, I need to change the Background color of each Item of my ListView seperately.
I found no examples or helpful documentation. The background should change if the value of a double is 0. I set the ListView Property: android:drawSelectorOnTop="true" and used following code:
(All of it functions, only the background doesn't change!) How can I solve this problem?
public void onClickButtonOKStand (View view) {
EditAusgabe = (EditText) findViewById(R.id.EditText01);
if (EditAusgabe.getText().toString().length() <= 0) {
Toast T = Toast.makeText(getApplicationContext(), "Eingabe ungültig! Geben Sie einen Betrag ein", Toast.LENGTH_LONG);
T.show();
return;
}
if (EditAusgabe.getText().toString() == ".") {
Toast T = Toast.makeText(getApplicationContext(), "Eingabe ungültig! Geben Sie einen Betrag ein", Toast.LENGTH_LONG);
T.show();
return;
}
Z = Double.parseDouble(EditAusgabe.getText().toString());
if (VArt == "Down") {
if (VStand >= Z) {
VStand = VStand - Z;
if (VStand <= 0.39) {
Toast T = Toast.makeText(getApplicationContext(), "Ihr Guthaben ist aufgebraucht!", Toast.LENGTH_strong textLONG);
T.show();
VStand = 0.00;
****************** The next line is my problem: ******************************
StartListe.getChildAt(Position).setBackgroundColor(color.holo_red_light);
}
}
else if (VStand < Z) {
Toast T = Toast.makeText(getApplicationContext(), "Vorgang nicht möglich! Ihr Konto liegt bei " + FORMAT.format(VStand) + " €.", Toast.LENGTH_LONG);
T.show();
EditAusgabe.setText("");
return;
}
}
if (VArt == "Up") {
VStand = VStand + Z;
}
Stand.set(Position, FORMAT.format(VStand));
Liste.set(Position, (VName + " " + FORMAT.format(VStand) + " € / " + FORMAT.format(VWert) + " €"));
ListeAktualisieren();
}
public void ListeAktualisieren () {
setContentView(R.layout.activity_ausgabenkontrolle);
ArrayAdapter<String> ListenAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Liste);
StartListe = (ListView) findViewById(R.id.listView1);
StartListe.setAdapter(ListenAdapter);
StartListe.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> ListenAdapter, View view, int i, long ID) {
// TODO Auto-generated method stub
Item = view;
Position = ListenAdapter.getPositionForView(view);
VName = Namen.get(Position);
VArt = Arten.get(Position);
VWert = Double.parseDouble(Werte.get(Position).toString());
VStand = Double.parseDouble(Stand.get(Position).toString());
setContentView(R.layout.activity_stand);
if (VArt == "Down") {
if (VStand == 0) {
Toast T = Toast.makeText(getApplicationContext(), "Ihr Guthaben ist aufgebraucht!", Toast.LENGTH_LONG);
T.show();
}
}
}
});
registerForContextMenu(StartListe);
}
I think the way to change the background color in response to a click is to apply the change to the incoming View given to you in your onClickListener implementation. You seem to be messing about with setContentViews, etc. which isn't the way to go. SetContentView sets your overall layout, and there's rarely a reason to call it more than once in an Activity.

Categories

Resources