Adding a Clickable button on surfaceView - android-layout

I am implementing a custom surface view, which is called from other activity by setContentView(new SurfaceViewClass(Context)). This class is extending the surfaceView. In the Draw() method which I have added in the surface view, I am displaying an animation that gets triggered, say, every 200 milliseconds, which means that the surfaceview thread gets triggered every 200 msecs.
My requirement is I want to add a button at the bottom of the animation that can respond to events when user has pressed it. How it is possible to do this?
Thank you!

The idea is to have surfaceView(i.e. your graphics) inside a FrameLayout. See the xml layout in the following link.
In the following questions, i have posted the code for the same.
Android:Crash: Binary XML file line : Error inflating class (using SurfaceView)
Let us know if it works!

Related

Data binding onClick in a fragment using interface

I recently started working with data binding and I ran into a problem that I can't figure out.
I'm trying to use an interface to handle when a button is clicked but it keeps on giving me a cannot find setter attribute for onClick on the button.
In my XML file I have
android:onClick="#{() -> iCalculatorFragment.onButtonClicked()}"
In my onCreateView in my Fragment I have
mFragmentCalculatorBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_calculator, container, false);
mFragmentCalculatorBinding.setICalculatorFragment(this);
My interface method is
void onButtonClicked();
Side notes:
Inside the xml the autocomplete comes up for other methods of the interface but not the button click one. Not sure if that means anything.
Also building has drastically slowed down since using data binding. Sometimes it takes over 10 minutes to build and it ends due to an error. Any way to fix this?

Move view from top to bottom continuously without animation in Android

I want to know how to move a view from top to bottom continuously without animation. I am asking this because I want to get the position of the view at every step so that I can check that if there is any collision between that view and any other view.
With animation you can move (not exactly move) a view from one position to another position (Animator class), but animation produces an illusion to the user that it is moving but it's position is fixed all the time. So this can't be done using animation?
Second approach is incrementing position of view. I applied this method in onCreate(). If I used it without Thread.sleep(50) then the activity doesn't show the view, if I applied it with Thread.sleep(50) then activity doesn't start for some period.
Property animation (subclasses of Animator class) actually move the view, as they update the actual property of the view. It is the view animations (subclasses of Animation class) that don't move the actual view and instead just where it appears to the user.
Source:http://developer.android.com/guide/topics/graphics/prop-animation.html
Quote: With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified.
You also shouldn't start moving things around in the onCreate method as things are still initializing (onwindowfocuschanged is recommened). Also if you call thread.sleep, you are going the sleep the main UI thread, hence freezing the application for a time.
Solved the problem using ValueAnimator :-
CodeSnippet :-
va=ValueAnimator.ofFloat(0.0f,size.y);
va.setDuration(5000);
va.setRepeatCount(va.INFINITE);
va.setRepeatMode(va.REVERSE);
va.start();
va.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
bullet[0].setTranslationY((Float) va.getAnimatedValue());
Rect R11=new Rect(bullet[0].getLeft(),bullet[0].getTop()+(int)bullet[0].getTranslationY(),bullet[0].getRight(),bullet[0].getBottom()+(int)bullet[0].getTranslationY());
Rect R21=new Rect(ball.getLeft(), ball.getTop(), ball.getRight(), ball.getBottom());
if(R11.intersect(R21))
va.cancel();
}
});

Resizing a custom XYChart in an FXML Tab Pane

This question related to Java-FX and its charting capabilities and resizing.
My problem is that I wanted to create a custom candlestick chart and I managed to get that done by going through the Ensemble application source code which was quite helpful.
The code for generating the candlestick is here:
http://javafx-ui-hxzon.googlecode.com/svn-history/r13/trunk/ChartsSampler/chartssampler/CandleStickChart.java
However, when I run my application, I seem to be stuck with a chart size that is fixed and does not resize according to my Tab Pane (the parent) resizing in my GUI. I've gone through the code, fiddled with it, changed the value of the minWidth and minHeight to USE_PREF_WIDTH AND USE_COMPUTE_WIDTH both but I cannot get it to fit to the parent Tab pane.
What am I missing?
I call AdvCandleStickChartSample in my controller as follows:
AdvCandleStickChartSample adv=new AdvCandleStickChartSample();
candleTab.setContent(adv);//candleTab has been predefined in an FXML file
My guess is I need to resize based on an event-handler? Am I correct in assuming this? Or am I not overriding a specific method that handles the resizing? When I create a dummy linechart and fill it with data and "run" it, it resizes fine along with the window.
Please help!
Never mind.
I found the answer. I simply had to override the layoutChildren() method by implementing the Pane superclass.
In the Ensemble application they accomplish this by implementing a "Sample" class which overrides the Pane super-class's layoutChildren() method

How to update a listview from a spinner without blocking the whole application

I'm coding a food-ordering app that relies on an expandable listview. When the user selects a quantity from a spinner inside the childview, a button in the groupview should update (from grey to green) to signify the user has picked a quantity in the group.
My problem is refreshing the expandablistview immediately upon the user clicking the spinner. If I use notifydatasetchanged or listview.notifyallviews, this blocks the whole program because the onitemselectedlistener of the spinner is constantly calling the line. Note this doesn't return an error, it's a legitimate program run but the fact of calling notifyallviews after an onitemclicklistener causes the program to be stuck in an endless view update.
I'm not attaching any code because I don't think anythign I wrote will help you. The above description should be giving you all the elements of the problem.
Looking forward to help!

Saving the states of JavaFX controls on exit

I have a bunch of control objects (TextBoxes, to be precise) that get injected into my code using the #FXML annotation during the FXML load.
I would like to save the states of these controls, specifically the text values, when the user closes the Scene by clicking the close box on the title bar.
However, when I trap the CloseRequest event in an OnCloseRequest handler I find that the values of the control variables are null (the original injection works, so this is something that happens in between the loading of the FXML and the calling of OnCloseRequest).
Can anyone explain this behavior and/or suggest how I can get the functionality that I want?
TIA
onCloseRequest is
Called when there is an external request to close this Window. ...
(from Javadoc). One of the meanings of "external request" is when you close the window through OS native window close button. The closeRequest event is not triggered through the programmatic stage.close() or stage.hide() calls. So consider to handle onHiding or onHidden events.
Otherwise post your OnCloseRequest handler code.

Resources