​Android number guessing game!

In the continuing adventures of higher education and personal learning, I made a simple little guessing game for Android, as instructed by my online course for Android app development. It is a very basic game, where the user must guess the number Android is thinking of. You can download it here:
https://www.mediafire.com/download/rf7du2ysaddwos5

Android chooses a new number every time you open the app, so it has infinite replay value!

Here is the breakdown of the major components:
androidmanifest.xml

[CODE]

[/CODE]
main.xml

[CODE]

[/CODE]
mainactivity.java

[CODE]

package com.alaskalinuxuser.guessthenumber;
import android.app.*;

import android.os.*;

import java.util.*;

import android.view.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.*;

public class MainActivity extends Activity 

{

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

Random appRandom = new Random();

int hidRandom = appRandom.nextInt(81) + 10;

public void takeGuess(View v)

{

EditText userGuess = (EditText) findViewById(R.id.myGuess);

Double uGuess = Double.parseDouble(userGuess.getText().toString());

if (uGuess hidRandom) {
Toast toast = Toast.makeText(getApplicationContext(), “Too high!”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), “That’s right, you guessed it!”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();
}

}

}

[/CODE]
As you can see, there is not much to this app. I was pleased that I was able to put it together in about 15 minutes (Praise God!), and, while simple, it is somewhat fun, if you’re board!
Linux – keep it simple.

​A currency converter app.

I never realized just how much goes into an app before trying to learn how to make one work! At the same time, I never realized how simple the individual pieces of the app can be.

 

Here is another app that I made for the course:

 

https://www.mediafire.com/download/npzaxxd0p0qzfw1
What I have been coming to understand is that looking at an app is a lot like building a puzzle. At first, you dump all of the pieces out of the box, and it looks overwhelming! Obviously, the scope of the app is similar to the size of the puzzle. If it has hundreds or thousands of pieces, it just seems impossible to put it together. But, as I learn more about making apps, I have come to realize that it is simpler if you break your puzzle down into groups, and then focus on one piece at a time.

 

For instance, if you sort your puzzle out by edge pieces, and put those together, then you can group center pieces by colors or objects from the box, and by working on just one area or group, you can get it done! You don’t have to figure out every piece at the beginning, you can instead focus on just one set of pieces, and everything will eventually hook together.

 

Similarly, with this currency converter app, I managed (after Googling) to take a number, and multiply it by another number from the user, and present it in a toast, per the instructions. I looked at the numbers, however, and realized that I didn’t want six+ decimal places, just 2. So, one piece at a time, back to Google, and now, praise God, it’s working!

 

You can check out the code here:

 

Here is my ANDROIDMANIFEST.XML

[CODE]

   

       

           

               
               

           

       

   

[/CODE]
Then my MAIN.XML

[CODE]

[/CODE]
Finally, my MAINACTIVITY.JAVA

[CODE]

 package com.mycompany.ustoau;
import android.app.*;

import android.os.*;
import android.app.*;

import android.os.*;

import android.view.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.*;

import java.text.DecimalFormat;
public class MainActivity extends Activity 

{

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

public void convert (View view){

DecimalFormat precision = new DecimalFormat(“0.00”);

EditText usDollar = (EditText) findViewById(R.id.myDollar);

Double valueUs = Double.parseDouble(usDollar.getText().toString());

Double valueAu = 1.35;

Double calculatedValue = valueUs*valueAu;

Toast toast = Toast.makeText(getApplicationContext(), “You have: $” + precision.format(calculatedValue), Toast.LENGTH_LONG);

toast.show();

}

}
[/CODE]


Linux – keep it simple.

The magic button that changes the picture!

​This android developer course is actually pretty fun! There are several hard parts, and a few “scratch my head” moments, but Rob does a great job of explaining what to do.
Here’s my latest creation:
https://www.mediafire.com/download/168pmpux75y8w9u

It is a simple app that changes the picture and gives you a popup (toast) when you click the button.
I am a bit proud of myself on this one, because I went above and beyond the task at hand (changing the picture) but also added the toast (from a previous lesson) and changed the icon (which I posted a “how to” on earlier). The great part is, Rob will teach you some principles, and then give you a related task that he didn’t specifically cover.
In this case, he showed us how to display an image, and then asked us to figure out how to change it. Fortunately, there is Google, or I would not have made it work! After you finish the challenge, you can continue through the video and watch Rob explain it, step by step. It’s funny how things that take me an hour he does in about 3 to 5 minutes!
Here is my ANDROIDMANIFEST.XML

[CODE]

   

       

           

               
               

           

       

   

[/CODE]
Then my MAIN.XML

[CODE]

[/CODE]
Finally, my MAINACTIVITY.JAVA

[CODE]

package com.mycompany.images;
import android.app.*;

import android.os.*;

import android.view.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.*;
public class MainActivity extends Activity 

{

Boolean flag=false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}
//mess method is declared in XML file

//This function will call when we click on button

//and we have to pass View object in this method

//which will take id of clicked button
public void change(View v)

{

Toast toast = Toast.makeText(getApplicationContext(), “Changing Picture…”, Toast.LENGTH_SHORT);

toast.show();

ImageView iv=(ImageView)findViewById(R.id.mainImageView1);

//use flag to change image

if(flag==false)

{

iv.setImageResource(R.drawable.ic_launcher);

flag=true;

}

else

{

iv.setImageResource(R.drawable.kdf);

flag=false;

}

}

}

[/CODE]
To God be the glory, it even works! Thankfully, there were several helpful finds on stack overflow and other blogs from my Google search. I certainly do not want to take credit for another’s work! I just put all the pieces together.
Another important part of the equation is to put ic_launcher.png and kdf.png (or whatever you name your icons) into the drawable folder in your source.

Linux – keep it simple.

​Changing your Android app icon.

As I continue my studies in Android app making, I was a bit surprised that we had not yet covered how to change your application’s icon. I suppose making the app actually work is the most important part, so I am glad that we are focused on that. However, the first impression of your app to the user is the icon you use.

Within a few minutes on Google, the answer was readily available. Perhaps it is because it is so simple that it was felt to be too simple for the course. Or, since I have not finished the course, perhaps there is a section for that later.
In either event, the answer is simple and I will share it here.
In your app folder is ./src/main/res/drawable-xxhdpi and similar “drawable” folders. These folders contain the icon in different sizes for different screen settings of different phones. In my case, the icon in each folder is called “ic_launcher.png”. The quick and easy way to change the icon for your app is to delete this icon, and replace it with the icon you want to use, and rename that new icon with the same name as the one you deleted.
After a little trial and error, I found that 128×128 icons display great as xxhdpi icons, but your mileage may vary depending on your system. Now just recompile your app and you can praise God that your icon has changed!
Linux – keep it simple.

Making Toast

As I have mentioned previously, I am continuing my studies with an online course from Udemy by Rob Perceval about how to build Android apps. Here is my latest creation:

https://www.mediafire.com/download/1l8ba54llk4c2y4
It’s a simple app, but I am excited because it is the first truely interactive app that I have built. Not only can the user input something, but the user gets a reply from Android!

The course is really fun and, for the most part, intuitive. The course is designed around using Android Studio, but I have also been using AIDE, the Android equivalent.


It is a little less intuitive than using Android Studio, but it is still a great tool with auto complete and error checking. Not to mention, when I click run, to test my app, it builds the app and installs it on my phone! No emulator needed, and you know exactly what your app will look like when you hold it in your hands.

It is also great for working on the go. It even has a visual editor for layout designing.

Linux – keep it simple.

Error:Gradle version 2.2 is required

In the continuing adventures of Android Studio and app development, I ran into yet another error while trying to import someone else’s work off of github:

[CODE]
Error:Gradle version 2.2 is required. Current version is 2.14.1. If using the gradle wrapper, try editing the distributionUrl in /home/alaskalinuxuser/AndroidStudioProjects/android_packages_apps_Trebuchet/gradle/wrapper/gradle-wrapper.properties to gradle-2.2-all.zip.

Please fix the project’s Gradle settings.
[/CODE]

Sounds simple enough. So I opened /home/alaskalinuxuser/AndroidStudioProjects/android_packages_apps_Trebuchet/gradle/wrapper/gradle-wrapper.properties and this is what I saw:

[CODE]
#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
[/CODE]

Which I edited to say:

[CODE]
#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip
[/CODE]

However, that was apparently not enough. It still didn’t work and said that the project did not have a gradle, and should be migrated to it. Odd…. I still haven’t figured out this one.

Linux – keep it simple.

Setting up Android Studio on a Debian Jessie HP Compaq 6715b AMD Turion X2 Processor

Setting up Android Studio on a HP Compaq 6715b AMD Turion X2 processor was far from simple. I followed numerous guides online, read dozens of articles on Stack Overflow, and yet, I was still stuck. Until I finally got it going (Praise God!). So, what made it so difficult? Well, let me tell you:

It all started when I bought a technical book from Amazon for kindle. Somehow, that book got me a reference to a course on Udemy called “The Complete Android Developer Course” By Rob Percival. Normally, the course was $200 plus, but for some reason it was on sale for a mere $17! The course said that it was designed for what I call “zero to hero” students, or those who knew little to nothing about the subject to train them to know enough to be dangerous, er, uh, useful. Needless to say, I purchased the online course.

In the course, which is a bit older now, Rob is using Android Studio 1.1 and suggests that students could use newer versions, but that they could also use AS 1.1 if they want everything to look and act the exact same way as in the course material. So, I downloaded AS 1.1. There were so many problems getting that to run, that I will not even discuss those here, for fear of nightmarish flashbacks.

So, I downloaded the latest version for Linux, 2.2. It launched first try. I followed instructions from a blog here:

http://ridz1ba.blogspot.com/2015/07/how-to-install-oracle-java-and-android.html

Which worked to get Android Studio to start. However, after following along through the course, it was time to launch my first app, which did not launch. Instead I received the following error:

[CODE]
glxinfo
libGL error: unable to load driver: swrast_dri.so
libGL error: failed to load driver: swrast
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 155 (GLX)
Minor opcode of failed request: 24 (X_GLXCreateNewContext)
Value in failed request: 0x0
Serial number of failed request: 38
Current serial number in output stream: 39
[/CODE]

This lead me on a painful search, from which I would like to save you similar users from doing and afflicting yourselves with. Here was what I did in the end:

I thought that for my model/make of processor, I could not use the x86 version of the emulator, so, when I set up a new device, or create a new virtual device, I needed to click on the “other” tab and choose an arm variant. This did not solve the issue, the same error happened when trying to launch my app.

Then I realized that I needed to install more dependencies. I used aptitude search and found that I also needed to install these:

[CODE]$ sudo apt-get install libgl1-mesa-swrast-dev libgl1-mesa-dev -y [/CODE]

Then, when I pressed launch, the android phone would appear, but it stayed blank. Finally, I found taht I had a graphics issue, which I could resolve by creating a new virtual device, and choosing “advanced features”. In those features is the option to use software graphics instead of hardware graphics. After selecting that, launching my app made the phone appear, AND it began to boot.

Now I can boot using the arm version or the x86 version. However, in the arm version, it is so slow that it is practically unusable. In the x86 version, it is also very slow, but usable, and I receive these errors:

[CODE]
WARNING: The Mesa software renderer is deprecated. Use Swiftshader (-gpu swiftshader) for software rendering.
emulator: WARNING: Host CPU is missing the following feature(s) required for x86 emulation: SSSE3
Hardware-accelerated emulation may not work properly!
[/CODE]

The first start up took a while, but now, when I make changes to my app, from pressing launch to actually seeing my app on screen takes 2:27 (m:ss), which is a bit slow, but reasonable on an older laptop. Hopefully, this will help others trying to use Android Studio on older laptops running Debian 8 with a Turion X2 processor.

Linux – keep it simple.