Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Saturday, 4 July 2015

MiniBlog: Paint Applications, and Splines

My friend and I recently had a conversation where he and his dad were talking about splines, which reminded me of how my professor taught me a good software application for splines.

What are Splines?

I honestly think the Wikipedia definition is much more clear than whatever way I could define a spline, so here you go:

In mathematics, a spline is a numeric function that is piecewise-defined by polynomial functions, and which possesses a sufficiently high degree of smoothness at the places where the polynomial pieces connect (which are known as knots).
In interpolating problems, spline interpolation is often preferred to polynomial interpolation because it yields similar results to interpolating with higher degree polynomials while avoiding instability due to Runge's phenomenon. In computer graphics, parametric curves whose coordinates are given by splines are popular because of the simplicity of their construction, their ease and accuracy of evaluation, and their capacity to approximate complex shapes through curve fitting and interactive curve design.

Polynomial interpretation is where you have a set of dots/points, and you try to fit ONE polynomial function over the whole thing. Runge's phenomenon is where you try to fit a high-degree polynomial function over a set of equidistant data points, but then near the edges, really bad and big oscillations occur, which isn't really good for data analysis and prediction!

Rutger's phenomenon, where the blue curve is a 5th order interpolating polynomial, and the green which is really oscillating, is a 9th order interpolating polynomial

The problem of most simple drawing applications

When I was a newb at android application development, I tried to make a simple drawing application to learn more about touch gestures of a mobile device. Android has good APIs for gesture detection. In your custom Canvas class that you make for your drawing application, you can override the OnTouchEvent method and handle the incoming user gestures.

To draw what the user draws, you simply have to get the x-y point when the user first presses down on the screen, and then draw a line between that point and the point the user moves to next. This means that as the user moves his finger across the screen, Android is getting x-y coordinates every XXX milliseconds, and each XXX milliseconds, Android draws a line between the previous x-y coordinates the user was at and the current x-y coordinates the user is currently at.

This has some problems, of course. Let's say the user swiped his finger really fast in a short amount of time. Because the Android API only gets x-y coordinates of the user's finger vs. time every XXX milliseconds, the resulting line that the user drew on the drawing application doesn't look smooth. It looks like a piece-wise function of straight lines connected to each other by dots, instead of a smooth curve. Below is an illustration I made about this:

Notice the lack of smoothness due to the C shaped curve being composed of multiple straight lines

So, in order to fix this, all you would have to do is take the history of all these points as the user first initially presses down, until the user releases his finger, and using that list of point objects, you use spline interpolation to make the whole line smoother, and then draw that smooth line onto the canvas!

Monday, 15 December 2014

ADT-1 (Android TV) Development Story - Part 1 (+ Description about my JPHacks App)

Edit: Scroll down to see the description about my JPHacks App.

I made an Android TV application through Android Studio. When it auto-generated the project for me, I got an apparently known issue:

Error:A problem was found with the configuration of task ':tv:generateDebugBuildConfig'.
> No value has been specified for property 'buildConfigPackageName'.

What I did was simply select both TV and Phone/Tablet when I was making the application:

And the automatically generated project gave some kind of internal IDE error.

Around two weeks after this incident however, Android Studio 1.0 was released. I am really glad Android Studio 1.0 was released though. There are some bugs that I found (e.g. the button to add dependencies to your application was gone?!?!), but they fixed that within the same week with a new patch: Android Studio 1.0.1.

After downloading Android Studio 1.0.1, I decided to make an application utilizing my new ADT-1 in a hackathon I was conveniently going to. The hackathon is called JPHacks.

I am the one in the front row, third from the left!

Summary about JPHacks App - "Android x Health"

Me, and two other engineers hacked away at our project. Our project actually uses Android Wear, Android TV, and Android Smartphone! (all 3!) It is certainly an "internet of thing"-like project. This application is like a all-round fitness application that actually encourages you to exercise. It also uses the newest technologies. Android TV is still really new, and Android Wear is pretty new too. If Android Studio 1.0 wasn't released the week of that hackathon, we wouldn't have been able to even set up an Android TV project (though in the end we didn't really need it. More on that later in this post.) We also used the super new Google Fit API, which Google is currently holding a competition for us Android developers to start making apps that include Google Fit API. I am definitely sure that our project has the most recent and innovative technologies because of all the things I have listed.

We had no UI designers in our team, so forgive the bad design.

I will now try to explain what our application does. Basically, the application marks a "geofence" around your home. When you go out of your home boundaries and go on with your day, e.g. go to work, then it will start counting your steps using the Wear's pedometer. On Wear app, you will see your heartbeat and your number of steps. On the mobile app, you will see your number of steps, and a graph that graphs a history of the number of steps you took for that week. There is also a neat feature of our app that is on the mobile phone - a personal avatar feature. We plan for the user to customize the avatar so that it can look like them, and, by analyzing the user's fitness stats through the Google Fitness API, if the user gets fat/does not exercise, then the avatar will get fat. If the user gets skinnier/takes lots of steps, then the avatar will be happy.

So, when the user comes home, if he or she did not exercise enough to meet his/her daily requirements, then a notification will pop up on the Wear and the phone to start exercising, and the avatar will grow a bit fatter and cry. A notification will show up, asking the user to either exercise, by watching an exercise video on their phone or on their TV. This is where the Android TV comes in. But also, this is where (very simple) machine intelligence/learning comes in. Through Google Fitness API and a bit of logic we put into our app, the application will get a recommended video from our database of videos. Also, while the video is playing, if the video is too hard (e.g. if your heartrate is too high), then the application, real-time, will recommend you an easier exercise video, and you can switch to that one.

Anyway, if you choose to show the exercise video on Android TV, you can chromecast a video from your smartphone (the Sender) to your Android TV (the Receiver). Then, the video can play. Not only that, but you can play and pause the video through a button on the smartphone app. While you are wearing your Wear during the exercise video, it will continuously monitor your heart rate.

So yeah. Although I didn't really make an actual Android TV app, I had fun learning how chromecast worked. Next time, I will actually make an Android TV app using Leanback, et cetera. I can't wait!

Also, it is currently 2 days after the hackathon ended, and we got an email saying we will be moving on to the finals with our project! Will we get first place? I hope so!

You can see the source code of our hack here:

Edit: You can see the Japanese Google Slides presentation describing our app here:

Saturday, 29 November 2014

"Dependencies" Troubles with Android Gradle, and How to Even Use Terminal in Android Studio

I have only started Android development during my last internship which was almost a year ago. Since then, I have migrated to Android Studio, and in all honesty, this new IDE is amazing compared to the Eclipse IDE with the Android ADT.

There are a few differences, though getting the hang of this new IDE should be simple enough - except for one thing.

Let's say you had a lot of dependencies - a.k.a., external libraries for your application. This has been honestly the most annoying thing ever for me as a newbie at Gradle, but basically, if one of your library projects has its own dependency called A, and you have the same kind of dependency in your main application project, then those two dependencies will conflict with each other.

When you have this kind of conflict, the error that you will normally get is:

UNEXPECTED TOP-LEVEL EXCEPTION: Multiple dex files define ...

Now, obviously, when one first comes across this issue, one uses StackOverflow - which is what I did, and this should be the best answer:

However, usually, newbies at Gradle such as myself will not realize that these commands only work if you have Gradle installed - I only installed Android Studio, that has a Gradle wrapper.

To summarize, all I and anyone who does not have Gradle installed should do is:

for any gradle command that we see that we can copy and paste in the terminal, replace the word "gradle" with "gradlew", because we do not actually have Gradle installed.

This made my life so much easier.

So, e.g., use:

gradlew -q :projectName:dependencies


gradle -q :projectName:dependencies

I hope this teaches some newbs such as myself about Gradle in terms of Android Studio.

Wednesday, 26 November 2014

ADT-1 Development Story - Part 0

This will be a blog series on my progress of developing on the ADT-1, a media player and game controller by Google! I got it from a very kind person working at Google Japan. He gave it to me, even though I am still just a student.

Sorry if this blog post is short, more will come very soon, as I can't wait to get started on this!

Tuesday, 22 April 2014

A Beginner Android Dev's Guide on How to Import Custom Libraries Into the Eclipse IDE

You will need:
  • the Eclipse IDE with the Android SDK.

General Steps
  • Go to the site (e.g. github) that has the open source library.
  •  Download the files (usually there should be a button that says "download .zip file" or "download .rar file". 
  • Copy and paste the src files into your own Android application's package. I usually like to do this by copying and pasting normally, and then pasting inside the package manager that Eclipse provides.
  • You might get errors. If you copied the individual classes into a package you already pre-made/pre-named, then just change the package name inside each .class file.
  • If there are errors for R., import the one that has your application package's name, and not the other Android.R one.
  • If there are additional things (e.g. the custom library included files in a drawable folder/xml files in the values folder), then be careful not to override your current android application's existing files!!! (e.g. if you already have values/string.xml and the custom library has one as well, then try to manually combine them instead of overwriting YOURS. You will get errors if you do not.)
Now, you can use the custom library. I hope this helps some people.

Friday, 4 April 2014

3 Reasons why your Android Search Dialog Doesn't Work

Today's post will be pretty short. It's about the search dialog in Android.

It's about this annoying thing I've tried to do for a long time. And it's a pretty easy thing to implement, since Android actually has a tutorial on how to implement it on their site. Click here to see the documentation.

It's how to create a search interface for your application.

I have had many newbie problems in just getting this thing to work, and after finally having solved all of my problems I wanted to compile a simple list of the most common reasons why the search dialog doesn't work
  1. You need to use @string/ instead of hardcoding the android:label and android:hint in your searchable.xml (this xml file is something you should have created in your /xml/ folder under res/)
  2. You messed up somewhere in your Android manifest.
Here is how your android manifest should look like.

 Android SearchableActivity
<activity android:name="com.example.lexicav1.SearchableActivity" >
                <action android:name="android.intent.action.SEARCH" />

                android:resource="@xml/searchable" />


the activity you want to use the search dialog in
                <action android:name="android.intent.action.listlexica" />

                <category android:name="android.intent.category.DEFAULT" />
              <meta-data android:name=""
                   android:value=".SearchableActivity" />


3. Put the correct path location of your SearchableActivity.class in android:value. For example, instead of using:


I used instead:


Which is wrong, because I did not put it in that package.

I hope this post helps.