Wednesday, December 16, 2015

libGDX Android Text Input

libGDX has its ups and its downs. It isn't perfect by any means, but I still believe it has a ton of potential especially with RoboVM at its side which makes it work wonderfully with iOS and still keeps it working great for Android and Desktop. I love the make it once, run it everywhere aspect that it provides. Also the same reason I love Java.

However the problem I was facing today was dealing with Android text input. I am amid creating my own library add-on for libGDX that provides a lot of the same GUI menu like items provided with Android, however they are directly for libGDX and use within games and game menus. I have been working on Buttons, Layouts, Text Inputs, Labels, Custom Widgets, Loading Bars, you name it. However when it came to text input from the soft-keyboard provided in Android, there were bugs that were all dependent on device, or if they weren't using Google keyboard. For a game that is supposed to run flawlessly on any device on which it can be installed, this wouldn't cut it. So I did a bit of popping around on the internet and came up with this way of creating a popup for Android that would take input.


Input.TextInputListener inputListener = new Input.TextInputListener() {
    @Override
    public void input(String input) {
        text = input;
    }

    @Override
    public void canceled() {

    }
};
             
Gdx.input.getTextInput(inputListener,  hint, text, "");

This worked excellently. It gave me a little popup that allowed for me to input text, then it would make the text in the widget, which I call EditText after Android EditText widget, change to be whatever I put in the popup. Everything was great. Just one problem. The popup looked almost 'prehistoric' or at least it was Android Froyo styling. Not exactly my taste. I like Lollipop much better, but I also supported back to Android Jelly Bean. So I did the next best thing.

In my Android part of the project I went into styles.xml and changed one part so that the android Dialog would look how I wanted it to.


Before:
<resources>
    <style name="GdxTheme" parent="android:Theme">
 <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


After:
<resources>
    <style name="GdxTheme" parent="android:Theme.DeviceDefault">
 <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>


Changing the parent of GdxTheme from android:Theme to android:Theme.DeviceDefault will make it so that the popup matches whatever OS you're running, be it KitKat, JellyBean, or Marshmallow.

That's all for today. Hope this helps anyone else struggling with this problem.

Wednesday, July 29, 2015

First Internship

I finally turned 18 and I'm out in the world, I landed my first internship, and let's just say that has been quite the experience.
A few things I've learned.
I already grew to love Android studio after a struggle to switch from Eclipse with Android Developer Tools. But it was well worth the switch, my productivity doubled, I felt more comfortable in the environment and everything went much better with different themes and a newer fresher more modern look.
The switch to Android studio happened back in January, but getting used to the IntelliJ platform was definitely useful in my first week as I was put to work using a framework I'd never even heard of.
My first project was on a framework called Spring Boot. It took me quite a few days to get used to Spring Boot and how it all connected, there's a lot being done in the background and I wasn't quite used to the way it used annotations yet. It's an amazing framework with lots of potential and lots of quick development. I learned an important lesson with that, you don't need to understand completely how something works, just understand that it does work and go with it.

Friday, October 24, 2014

Android Grow View Dynamically

My problem was I wanted to add animation to my app in order to make it more appealing, and to more along the lines of material design.
I did a couple quick Google searches, but didn't find much. Instead I came up with this quick trick that made it possible.
public class ResizeAnimation extends Animation {
    private int startHeight;
    private int deltaHeight; // distance between start and end height
    private View view;
    /**
    * constructor, do not forget to use the setParams(int, int) method before
    * starting the animation
    * @param v
    */
    public ResizeAnimation (View v) {
        this.view = v;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
        view.requestLayout();
     }
    /**
    * set the starting and ending height for the resize animation
    * starting height is usually the views current height, the end height is the height
    * we want to reach after the animation is completed
    * @param start height in pixels
    * @param end height in pixels
    */
    public void setHeightDifference(int start, int end) {
        this.startHeight = start;
        deltaHeight = end - startHeight;
    }
    /**
    * set the starting and ending width for the resize animation
    * starting width is usually the views current width, the end width is the width
    * we want to reach after the animation is completed
    * @param start height in pixels
    * @param end height in pixels
    */
    public void setHeightDifference(int start, int end) {
        this.startWidth = start;
        deltaWidth = end - startWidth;
    }
    /**
    * set the duration for the hideshowanimation
    */
    @Override
    public void setDuration(long durationMillis) {
        super.setDuration(durationMillis);
    }
    @Override
    public boolean willChangeBounds() {
        return true;
    }
}
LinearLayout clickableLayout = (LinearLayout) findViewById(R.id.click able_layout)
clickableLayout.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View V){
        RelativeLayout v = (RelativeLayout) V;
        // getting the layoutparams might differ in your application, it depends on the parent layout
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();
        ResizeAnimation a = new ResizeAnimation(relativeLayout);
        a.setDuration(500);
        // set the starting height (the current height) and the new height that the view should have after the animation
        a.setParams(lp.height, newHeight); relativeLayout.startAnimation(a);
        v.startAnimation(a)
    }
}
       
The difference between this and a scale animation is you can actually have one line of text on a linear layout, grow to three lines, but not in a scale like way of squished text. With the XML scale setting you would actually end up having the space all pop up there, but the animation would slowly fill it, with this, it will create the space every time it grows making it more smooth and clean.

Friday, July 18, 2014

Passing Data Between Threads (Java)

Currently I'm working on a new desktop app for a family reunion. I started out hoping this would be a two day and done, but when I actually started coding I realized I was in a bit deeper than I had previously thought. So here's to explaining my app.

This app requires 2 windows to be constantly open and running, however both of these windows need constant communication.

The audience cannot see the control window, but rather only the pretty display, this allows for the audience to never see an annoying mouse moving around. I couldn't figure out any other way to do this the way I want with libgdx without multi threading.

I started this project by trying to create two windows using libgdx (however I didn't actually want libgdx for control) but I wanted to see if it was possible. I quickly learned that openAL does not allow libgdx to do that, so I quickly switched over to Swing to make sure that I could in fact actually still accomplish 2 windows (and not "pop ups").

So I started working at this app got a bit of the GUI out of the way then went on to the next challenge of communicating between these threads. Now the way that libgdx works you can't manage the threads a whole lot from the original class to start the threads, or at least in terms of passing around data.

The solution? A class I call DataPassage, with all variables being static. The static variables allow me to access the data from anywhere without having to directly pass it to each class. DataPassage nicely handles all variables inside. This helps in two ways.

#1) Cuts down in data on the heap.

#2) Organizes all variables and keeps it all in sync.

Now would I recommend this for all variables in every situation? No, but definitely useful in a lot of situations. Would I recommend this if you are struggling in a situation like mine? Yes. Just be sure to not be storing the variables in all 3 classes, and rather only in the DataPassage class.

Monday, July 14, 2014

MinerMan

https://play.google.com/store/apps/details?id=com.coppercow.minerman

Here's the link to my latest game I've released called MinerMan!

If you enjoy it please tell you're friends and family!

Thursday, February 27, 2014

OK Google!

Yesterday Android developers announced a very long awaited launcher called the Google Now Launcher available at no cost on Google Play. This launcher gives a more current look and feel to a stock Google phone. This launcher also gives accessibility to "OK Google" from the home screen. This means that from the home screen if you say "OK Google" aloud Google will pop up so you can tell it what to do. Google Now launcher made its first appearance on the Nexus 5 device from Google, but did not come stock with Android 4.4 so this is rather a big deal for any Android users who do not want to pay for a Nexus 5, but still want the look, feel, and capabilities(within reason) of the Nexus 5. The new Google Now launcher is available for download here: https://play.google.com/store/apps/details?id=com.google.android.launcher

Wednesday, February 5, 2014

Python Inherit Pygame Surface

Learned new thing about python classes recently. In order to create a class that is a surface you have to initiate the surface. This is done by doing the following.

class MySurface(pygame.Surface):
    def __init__(self, width, height, image):
        pygame.Surface.__init__(self, width, height)
        self.blit(image, (0, 0, width, height))

Monday, February 3, 2014

Python IDE

Kind of a cool Python IDE(Integrated Development Environment) if you have internet. Most especially useful for Python development with the new Google Chromebook.  http://pythonfiddle.com/

Introduction

I'm a teenager in high school and started coding when I was 15 using Python and a Raspberry Pi. In all of my free time I do lots and lots of coding. Recently I have started a new language, Java, in order to expand my knowledge and move on to more Android  development. I believe everyone who wants to, deserves a chance to program. You can find me on StackOverflow at http://stackoverflow.com/users/2605424/duhprogrammer13