I have a MPAndroidChart BarChart which is placed inside a HorizontalScrollView. Now, I have multiple bars in the chart view and also the chart width is fixed as per my requirement. Since, there is a default scroll behaviour of chart, I can see all the bars.
The only problem is the scroll behaviour is not smooth and it lags a lot.
My idea is to disable the HorizontalScrollView once I start scrolling the chart and enable it back when I touch outside the chart. Can someone please tell me how I can do it? I hope my question is clear enough and there is no need to share any XML for it. Thanks in advance.
I faced similar issue while using Mpchart inside horizontal scroll view.
I had a workaround with MpChart setOnChartGestureListener.
barChart.setOnChartGestureListener(new OnChartGestureListener() {
#Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
horizontalScrollView.requestDisallowInterceptTouchEvent(true);
}
#Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
horizontalScrollView.requestDisallowInterceptTouchEvent(false);
}
//.....//
#Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
Log.i("GESTURE", "onChartTranslate");
if(barChart.getLowestVisibleX() == barChart.getXAxis().getAxisMinimum() || barChart.getHighestVisibleX() == barChart.getXAxis().getAxisMaximum()) {
horizontalScrollView.requestDisallowInterceptTouchEvent(false);
} else {
horizontalScrollView.requestDisallowInterceptTouchEvent(true);
}
}
});
the horizontalScrollView object is where the chart reside.
Related
I'm new in android development and have now problem and can not understand why and solve it.
Sorry, here is my idea and hope it helps to understand my question:
I would like to develop an app for the kids. I have two car roads. On both roads drive different cars with a certain number (0-10). The cars drive from left to right with the help of animation. A question with a number is asked. The child has to click on a certain car with the number x.
Below I've imageview (imgCarUp1) and try to animate from left to right. During animation I want to make click-event on my image and should run some code.
`
imgCarUp1 = findViewById(R.id.imgCarUp_1);
imgCarUp1.setImageResource(vehicleUpStreetList.get(0).getResId());
imgCarUp1.setTag(vehicleUpStreetList.get(0).getResId());
imgNumberUp1.setOnClickListener(view -> {
Log.d(TAG, String.format("setOnClickListener Tag: %s", view.getTag()));
...
});
TranslateAnimation animation = new TranslateAnimation(0, width - 50, 0, 0);
Animation.AnimationListener animL = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
LogUtils.i(TAG, "onAnimationEnd is stopped!");
}
};
animation.setAnimationListener(animL);
animation.setDuration(15000);
animation.setRepeatCount(5);
animation.setRepeatMode(1);
animation.setFillAfter(true);
imgNumberUp1.startAnimation(animation);
`
Aninmation is working well. But if I try to click on imageview (imgNumberUp1) nothing will work. But if I click on area where animation was started, setOnClickListener works. I should be able to make click during animation. What is here my problem? Can someone please help with code?
Thanks a lot for helping.
Tried to search in internet, without any solution yet.
You are using Translate animation. Translate Animation translates the views position to the animated positions and wont actually change the views original position.
So you will need to change X and Y value to change the original position and then handle clicks wherever the animation is currently positioned
you can do .
view.animate().X().y().start()
while x and y will have the params of where you want to animate
I have to change the font color of text area dynamically from the color menu. But I am not able to change it. Can you please help me ?
final ColorMenu fontColorMenu = new ColorMenu();
fontColorMenu.getPalette().addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
//textarea.getElement().getStyle().setColor("#"+event.getValue()); // Not working
//textarea.getElement().getStyle().setProperty("color", "#"+event.getValue()); // Not working
textarea.getElement().getStyle().setProperty("Color", "red !important"); // Not working
}
});
You can try this:
textarea.getCell().getInputElement(textarea.getElement()).getStyle().setColor("red");
Do not know if this is the best solution, but it works or me.
How to hide a tab bar at bottom.
I used android:windowSoftInputMode="adjustPan|adjustResize". I do not get the desired result. I also added the event bus and when the focus on the edit text disappears tab bar:
public void showTabBar()
{
mTabHost.getTabWidget().setVisibility(View.VISIBLE);
}
public void hideTabBar()
{
mTabHost.getTabWidget().setVisibility(View.GONE);
}
But, it flickers, too, is not suitable. What else can I do?
(source: cs629227.vk.me)
I have already seen : How to set AUTO-SCROLLING of JTextArea in Java GUI?
blackArea = new JTextArea();
blackArea.setFont(new Font("Tahoma", Font.BOLD, 11));
blackArea.setText("Loggend on Administrator...\n" +date);
blackArea.setForeground(Color.RED);
blackArea.setBackground(Color.BLACK);
DefaultCaret caret = (DefaultCaret)blackArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
blackArea.setCaretPosition(blackArea.getDocument().getLength());
scrollPane.setViewportView(blackArea);
This works well. When update to JTextArea, the scroll moved to bottom automatically so I could see the refresh data. But the problem is, when I click the any space in JTextArea, the auto-scrolling is stopped. No more auto scroll works. How to fix it?
SUPPLEMENT : I added text to blackArea calling GUI.blackArea.append("bla bla bla"); GUI is class name where above code included. Thanks for #hovercraft-full-of-eels
Check out Smart Scrolling. It is an improvement over the other scrolling answer.
It the scrollpane is at the bottom when the append occurs it will continue to keep the scrollpane at the bottom. However, if the user has move the viewport from the bottom then the append will not automatically scroll to the bottom.
You don't show where you are adding or appending text to the JTextArea, and this is critical since the changing of the caret position should occur there.
Edit
You state:
Sorry, I just append text in other class, just calling GUI.blackArea.append("bla bla bla"); Should I use SwingUtilities.invokeLater?
I know you've got a decent answer from Rob Camick, a true Swing guru, but I also have to add that you really shouldn't expose your class's fields that way (and hopefully none of your components are declared static as your code suggests that they may be). Instead expose public methods that allow controlled ability to change the state of your fields. For instance your GUI class could have a public method like so
public void blackAreaAppend(String text) {
blackArea.append(text);
// code here to advance cursor
}
Or if this method is always called off of the EDT:
public void blackAreaAppend(String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
blackArea.append(text);
// code here to advance cursor
}
});
}
Or if you're just not sure:
public void blackAreaAppend(String text) {
if (SwingUtilities.isEventDispatchThread()) {
blackArea.append(text);
// code here to advance cursor
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
blackArea.append(text);
// code here to advance cursor
}
});
}
}
I solved this problem. this is the problem of view-point. when I click any space on JTextarea, the location of caret is changed, so view-point is changed too. Following my code, there is no update with view point.
So, I made a method :
public static void addLog(final String log){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
GUI.blackArea.append(log);
GUI.blackArea.setCaretPosition(GUI.blackArea.getDocument().getLength());
}
});
}
I changed blackArea.append("...") to `addLog("...). and I got out of this problem, However, remember that you can't fix caret positon while updating.
I'm using jFreeChart via JFreeChartWrapper in Vaadin and have problems on drawing it.
Data for charts is getting in runtime and it could take some time.
So I want to add progress indicator to notify the user about image loading (before it closes the window).
I tried to resolve this problem by adding temporary layout for ProgressIndicator and using it until receiving some event from charts:
final AbsoluteLayout loadLayout = new AbsoluteLayout();
loadLayout.setImmediate(true);
ProgressIndicator progress = new ProgressIndicator();
progress.setIndeterminate(true);
progress.setVisible(true);
loadLayout.addComponent(progress, "right: 500px; top: 200px;");
setContent(loadLayout);
Meanwhile, chart are added in another layout:
JFreeChart barChart = ChartFactory.createBarChart("Text", "", "", res, PlotOrientation.VERTICAL, true, false, false);
barChart.addProgressListener(new ChartProgressListener() {
#Override
public void chartProgress(ChartProgressEvent arg0) {
if (arg0.getType() == ChartProgressEvent.DRAWING_FINISHED) {
loadLayout.setVisible(false);
setContent(layout);
layout.setVisible(true);
}
}
});
JFreeChartWrapper chartWrap = new JFreeChartWrapper(barChart);
layout.addComponent(chartWrap);
But what I get - is only progress indicator, I never get ChartProgressEvent. And I don't see where this event if fired in JFreeChart.
Is it possible to fix it or I should better use other Vaadin plugin for charts?
To receive such events, you need to add your chart to a ChartPanel, as discussed here.