LikeView (android view) in libgdx - java

I'm trying to place a Like button in my app. After much searching I found that it is impossible to use me own custom button, so I am left only with implementing the default like button from the facebook sdk.
Since this LikeView seems to be a native android like view I don't really know how to put this into my libGDX app.
I would like to use this button only in a specific Screen and set its bounds so that it fits with the rest of my UI. Does anyone have an example of how to create this like button without using the XML (as it is done in all the documentation I have found so far).

Adding the following functions to my application makes it show in the correct position. Unfortunately the LikeView does not get the correct size, but is centered inside the view, which means changing the width/height just moves it.
public void GenerateLikeButton()
{
application.runOnUiThread(new Runnable(){
#Override
public void run() {
float x = 560 * game.global_scale;
int width = (int) (440 * game.global_scale);
int height = (int) (152* game.global_scale);
float y_from_bottom = game.screen_height - ((56+152+70) * game.global_scale + game.ad_height);
Gdx.app.log("like", "from bottom: "+ y_from_bottom);
likeButton = new LikeView(application);
likeButton.setLikeViewStyle(LikeView.Style.BUTTON);
likeButton.setX(x);
likeButton.setY(y_from_bottom-height);
likeButton.setObjectId(LIKE_URL);
likeButton.setVisibility(View.GONE);
application.layout.addView(likeButton,width,height);
likeButton.invalidate();
}
});
}
#Override
public void ShowLikeButton(final boolean visible)
{
application.runOnUiThread(new Runnable()
{
#Override
public void run()
{
if(visible)
likeButton.setVisibility(View.VISIBLE);
else
likeButton.setVisibility(View.GONE);
}
});
}

Related

Simulating click via Android Accessibility Service

I am trying to simulate a click function in an android application utilizing the Accessability Service. I have set up the service in the manifest, and enabled it in my phones settings. I have a class MouseAccessabilityService which contains a the following click function:
// Clikc function that clicks at a specific x and y coordinate
#RequiresApi(api = Build.VERSION_CODES.N)
public static GestureDescription click(float x, float y) {
// for a single tap a duration of 1 ms is enough
final int DURATION = 1;
Path clickPath = new Path();
clickPath.moveTo(x, y);
GestureDescription.StrokeDescription clickStroke =
new GestureDescription.StrokeDescription(clickPath, 0, DURATION);
GestureDescription.Builder clickBuilder = new GestureDescription.Builder();
clickBuilder.addStroke(clickStroke);
return clickBuilder.build();
}
I am calling the click function in the MainActivity via a button press that triggers the click (for testing purposes):
Buttonclick = (Button)findViewById(R.id.clickbutton);
Buttonclick.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onClick(View view) {
//changeButtonText.setText("Second Text");
MouseAccessabilityService.click(centerX, centerY);
}
});
The X and Y is being passed as an integer variable from some other functionality. I can not seem to get the click() function to work from within my app and press on a button.
Am I on the right page? Can this functionality even work? I am at a standstill with this and would appreciate any help.
Thanks.
You can use the dispatchGesture function.
Try replacing your click function's return with :
return dispatchGesture(clickBuilder.build(), null, null);
https://codelabs.developers.google.com/codelabs/developing-android-a11y-service/#7

how to add touch pointer to aplication?

I'm developing simple drawing application and I was wondering if it is possible to add visible pointer when touching canvas? What I mean is something similar to mouse indicator on PC. The reason for that is - when using eraser it is not visible where exactly your touch is registered and with the pointer, it would help (especially when using finger touch, because the pen is quite precise). I've tried with other similar topics but none of them answered the question.
taken from this pos :
How to programmatically enable "show touches" option in Android?
• enable show touches:
Settings.System.putInt(context.getContentResolver(),
"show_touches", 1);
• To disable show touches:
Settings.System.putInt(context.getContentResolver(),
"show_touches", 0);
Remember to add android.permission.WRITE_SETTINGS to your Manifest.
EDIT:
here is a DrawView.java that implements View.OnTouchListener and defines multiple draw modes. however it lacks the indicators.
in main activity inside setListener for the DrawView you override onStartDrawing method: here you can decide whether or not to show an indicator and how to style it depending on DrawView's current mode ( pen ,eraser etc ) and on the other interface methodes move the indicator if its visible and hide it when drawing ends. check the code:
mDrawView.setOnDrawViewListener(new DrawView.OnDrawViewListener() {
#Override public void onStartDrawing() {
canUndoRedo();
// decide whether or not to show an
// indicator and style it depending on
// mDrawView's attributes and current drawing mode
setIndicator(mDrawView.getDrawingMode(),mDrawView.getDrawWidth());
}
#Override
public void onMove(MotionEvent motionEvent) {
if(isIndicatorVisible){
indicatorView.animate()
.x(motionEvent.getX())
.y(motionEvent.getY())
.setDuration(0)
.start();
}
}
#Override public void onEndDrawing() {
canUndoRedo();
//hide indicator here
if (isIndicatorVisible){
indicatorView.setVisibility(View.GONE);
isIndicatorVisible = false;
}
}
here is the edits that i add to DrawView.java :
defined new interface method void onMove(MotionEvent motionEvent)
public interface OnDrawViewListener {
void onStartDrawing();
void onEndDrawing();
void onClearDrawing();
void onRequestText();
void onMove(MotionEvent motionEvent); // added this method
}
called the new method inside onToch:
case MotionEvent.ACTION_MOVE:
onDrawViewListener.onMove(motionEvent); // added this line
mDrawMoveHistory.get(mDrawMoveHistory.size() - 1).setEndX(motionEvent.getX()).setEndY(motionEvent.getY());
if (mDrawingTool == DrawingTool.PEN || mDrawingMode == DrawingMode.ERASER) {
mDrawMoveHistory.get(mDrawMoveHistory.size() - 1).getDrawingPathList()
.get(mDrawMoveHistory.get(mDrawMoveHistory.size() - 1).getDrawingPathList().size() - 1)
.lineTo(motionEvent.getX(), motionEvent.getY());
}
invalidate();
break;
here's setIndicator method :
private void setIndicator(DrawingMode drawingMode, int drawWidth) {
if (drawingMode == DrawingMode.ERASER){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(drawWidth, drawWidth);
indicatorView.setLayoutParams(params);
GradientDrawable border = new GradientDrawable();
border.setStroke(1, 0xFF000000); //black border with full opacity
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
indicatorView.setBackgroundDrawable(border);
} else {
indicatorView.setBackground(border);
}
indicatorView.setVisibility(View.VISIBLE);
isIndicatorVisible = true;
}
}

How can I interact with animations?

I have an animation that slides across the screen, while the animation is playing I want it to disapear if it has been clicked!
I have tried to use OnClickListener() and also OnClickMethods, both only work after the animation has finished playing!
TranslateAnimation animation = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to
animation.setDuration(5000);
Floater0.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (AdPlayer0.isPlaying()) {
answer = rn.nextInt(width) + 1; //useless
TranslateAnimation animation0 = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to
animation0.setDuration(5000);
animation0.setFillEnabled(true);
Floater1.startAnimation(animation0);
Floater1.setVisibility(View.VISIBLE);
Floater0.startAnimation(animation);
}
}
});
I was reading the answer here and something interesting was mentioned:
Animations do NOT effect the 'physical' location of the views on screen. If you want it to actually move then you need to adjust the properties of the view.
I have now tested the above quote, and it seems to be true! I have also looked into how to adjust the position properties of the view after the animation has ended, however I want to change its position before an animation has ended(While its playing)!
Any suggestions for a work around? ideally I would want to change the physical position of the view while the animation is played!
A TranslationAnimation only moves the pixels on the screen, so an OnClickListener does not animate with it. Use ViewPropertyAnimator instead and set an OnClickListener then it should work.
The code would look something like this :
Floater1.animate().setDuration(5000).translationX(answer).translationY(height * -1);
Check out all the available methods in the docs.
Edit/Update :
.translationX(float toX)
means -> animate the view to this point on the x axis.
The animation always starts at the current position of the view, so you don't need a fromX. There are also other methods which you can use :
.translationXBy(float byX)
.translationYBy(float byY)
With these you can translate the view BY the float value, and not TO a certain point on the screen.

Animate Status Bar Color on DrawerLayout - Material Design

I'm writing a simple app that implements a material design compliant nav drawer as described by this Stack post: https://stackoverflow.com/a/27153313/1621380
The ScrimInsetsFrameLayout works great and everything is dandy until I attempt to programmatically change the color of the status bar. My app uses the Palette API to change the toolbar and status bar color dynamically.
I'm using the property animation api to animate the toolbar and it works great! but I try to do the same animation on the statusbar and it doesn't seem to want to animate. Here's an example
Here is the code for my animator:
public static void fadeStatusBar(final DrawerLayout layout, Integer from, Integer to) {
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
layout.setStatusBarBackgroundColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.start();
}
}
Note: That same code is fading the toolbar, so its proven to work.
My question is; does anyone know of a way to get a smooth transition when using DrawerLayout.setStatusBarBackgorundColour()?
Note: I have used the Window method window.setStatusBarColor() method, and it animates fine but breaks the "transparent statusbar" when the NavDrawer is pulled in.
As advised #adneal's comment on my original question the answer was to invalidate the view during the animation.
DrawerLayout calculates the top inset and draws the status bar background itself. So, you just need to make a call to View.invalidate to force it to redraw while the animation is being updated. #adneal
I've updated my code to this
ValueAnimator colorAnimation = ValueAnimator.ofArgb(from, to);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
layout.invalidate();
layout.setStatusBarBackgroundColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.start();
For anyone wondering why this worked, I found this stack post enlightening: When it's necessary to execute invalidate() on a View?
you can do this Easily like below:
1 - set your statusbar color to transparent by using this:
<item name="colorPrimaryDark">#android:color/transparent</item>
2 - define application's view to change it background:
View root = *A_view_on_the_activity*.getRootView();
3 - then add this function to your project:
private void color_change(final View view,int from_color,int to_color){
final float[] from = new float[3],
to = new float[3];
String hexColor_from = String.format("#%06X", (0xFFFFFF & from_color));
String hexColor_to = String.format("#%06X", (0xFFFFFF & to_color));
Color.colorToHSV(Color.parseColor(hexColor_from), from); // from
Color.colorToHSV(Color.parseColor(hexColor_to), to); // to
ValueAnimator anim = ValueAnimator.ofFloat(0, 1); // animate from 0 to 1
anim.setDuration(600); // for 300 ms
final float[] hsv = new float[3]; // transition color
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
#Override public void onAnimationUpdate(ValueAnimator animation) {
// Transition along each axis of HSV (hue, saturation, value)
hsv[0] = from[0] + (to[0] - from[0])*animation.getAnimatedFraction();
hsv[1] = from[1] + (to[1] - from[1])*animation.getAnimatedFraction();
hsv[2] = from[2] + (to[2] - from[2])*animation.getAnimatedFraction();
view.setBackgroundColor(Color.HSVToColor(hsv));
}
});
anim.start();
}
4 - and then change the hole application's background color animation by using the function that Written above:
color_change(root, from_color, to_color);

Getting series from doughnut chart - achartengine

I am using a doughnut chart through achartengine and it is working fine except I can't figure out how to get the values associated with the region a user clicks on. It is my understanding through reading other questions that this is possible but the posted fixes aren't working for me. I have tried an onClick/Touch listener using getCurrentSeriesAndPoint but this always returns null. Here is my code:
final GraphicalView gV = new DoughnutChart().getView(getBaseContext(),
regionNames, regionSizes);
gV.setClickable(true);
gV.setFocusable(true);
gV.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
SeriesSelection selection = gV.getCurrentSeriesAndPoint();
double[] xy = gV.toRealPoint(0);
if (selection == null) {
//update
} else {
//update
}
}
});
Let me know if there is any more information I can add.
My end goal will be to extract the name of the region clicked and then call another activity with that name to display more information about it.
The click and get selection is currently implemented for XYChart(line, bar,...) and for the PieChart, so there is no such support for the DoughnutChart yet.

Categories