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();
}
}
});
Related
I want to click each slices in the chart and then I have to show a popup based on clicking in each slices. I have implemented onclicklistener, but it is not working.
Code :
Clicking part:
gv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = gv.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
int seriesIndex = seriesSelection.getSeriesIndex();
String selectedSeries = "Income";
if (seriesIndex == 0)
selectedSeries = "Income";
else
selectedSeries = "Expense";
int amount = (int) seriesSelection.getValue();
Toast.makeText(
getActivity(),
selectedSeries + " in " + "" + " : " + amount,
Toast.LENGTH_SHORT).show();
}
}
});
Rendering part:
public GraphicalView createIntent(List<double[]> values1, int[] colors, List<String[]> titles) {
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setApplyBackgroundColor(false);
renderer.setShowLegend(false);
renderer.setShowLabels(false);
renderer.setStartAngle(chartStartAngle); // 0 is 90 degree
renderer.setLabelsColor(Color.BLACK);
//renderer.setBackgroundColor(ContextCompat.getColor(get));
renderer.setScale((float) 1.40);
renderer.setPanEnabled(false);
renderer.setZoomEnabled(false);
renderer.setClickEnabled(true);//
renderer.setSelectableBuffer(100);
return ChartFactory.getDoughnutChartView(getActivity().getApplicationContext(),
buildMultipleCategoryDataset("Project", titles, values1),
renderer);
}
I want to hide more than 60 character of textView and show instead more... or see more...and after clicking on it show less in an item of listView adapter with following code :
if (item.explain.length() > 60) {
txtExplain.setText(Html.fromHtml("<font color='#15304e'>← " + G.currentActivity.getString(R.string.more) + "</font><br/>" + item.explain.substring(0, 40) + "..."));
isMoreThan60 = true;
seeMore = true;
} else {
txtExplain.setText(item.explain);
isMoreThan60 = false;
seeMore = false;
}
and when I want to set OnClickListener I use it in this way :
txtExplain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isMoreThan60) {
if (seeMore) {
txtExplain.setText(Html.fromHtml("<font color='#15304e'>↓ " + G.currentActivity.getString(R.string.less) + "</font><br/>" + item.explain));
seeMore = false;
} else {
txtExplain.setText(Html.fromHtml("<font color='#15304e'>← " + G.currentActivity.getString(R.string.more) + "</font><br/>" + item.explain.substring(0, 40) + "..."));
seeMore = true;
}
} else {
return;
}
}
});
except first Item I've not any problem that I should scroll listView until exit first item from screen and use from it's OnClickListener
first item or others that are shown in first time does not REFRESH
tanks
Hi i am using achartengine's pie-chart to represent my actual sales, now as my app starts one of the chart element in the piechart should get highlighted, like when we do the onclick on pie chart.
Below is the code for my piechart
final DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setPanEnabled(false);// Disable User Interaction
renderer.setLabelsColor(Color.BLACK);
renderer.setShowLegend(true);
renderer.setLegendTextSize(20);
renderer.setInScroll(true);
renderer.setStartAngle(180);
renderer.setChartTitle("Sales By Market Segment- Month");
renderer.setLabelsTextSize(22);
final CategorySeries categorySeries = new CategorySeries("Sales");
categorySeries.add("Craft1", cursor.getInt(1));
categorySeries.add("product1", cursor.getInt(2) );
categorySeries.add("product2", cursor.getInt(3) );
categorySeries.add("product", cursor.getInt(4));
categorySeries.add("product1", cursor.getInt(5));
mChartView2=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(mChartView2);
renderer.setClickEnabled(true);
mChartView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView2.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(getActivity(), "No chart element selected", Toast.LENGTH_SHORT)
.show();
}else {
for (int i = 0; i < categorySeries.getItemCount(); i++) {
renderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
}
mChartView2.repaint();
Toast.makeText(
getActivity(),
"Chart data point index " + seriesSelection.getPointIndex() + " selected"
+ " point value=" + seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
return ChartFactory.getPieChartIntent(context, categorySeries, renderer,null);
}
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
products are getting highlighted when clicking but I want to highlight one product, when app starts.
Found the solution:
mChartView2=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(mChartView2);
renderer.getSeriesRendererAt(0).setHighlighted(true);
mChartView2.repaint();
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.
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.