Skip to main content

Posts

Showing posts with the label language-java

Theme error in 2010s Android App after AppCompat Migration

I plan on releasing a lot of my old work as GPL open source, but most of it has aged to the point that it no longer functions, or if it does work it’s running in compatibility mode. Basically it’s no longer best practices. Not a good way to start off any new public GPL projects, in my opinion. The current project I’m working on is an Android app that calculates star trails meant to help photographers get or avoid that in their night time photos. For now I’m going to skip some of the import process because I didn’t document it exactly. It’s been mostly trial and error as I poke around Android Studio post import. The Android Studio import process… Removing Admob Google Play code before the project would run at all. After removing dependencies, it kind of worked, but when running it in the emulator it shows a pop-up message saying that the app was developed for an old version of Android. Going through the process of updating code to match current best practices… I had the IDE convert the ...

Android: Using the CountDownTimer class

In this article I go over the Android SDK CountDownTimer class. It’s easy to use and gives you the ability to count for projects like a workout timer. In this case we are extending that class so that we can access the methods we need to make it work. Here is the code on GitHub. Here is our SimpleCountDownTimer: public class SimpleCountdownTimer extends CountDownTimer { public static int oneSecond = 1000; TextView textViewTimeLeftDisplay; public SimpleCountdownTimer(long millisInFuture, long countDownInterval, TextView textViewTimeLeftDisplay) { super(millisInFuture, countDownInterval); this.textViewTimeLeftDisplay = textViewTimeLeftDisplay; } @Override public void onFinish() { textViewTimeLeftDisplay.setText("Finished"); } @Override public void onTick(long millisUntilFinished) { textViewTimeLeftDisplay.setText(String.valueOf(millisUntilFinished / oneSecond)); } } The extended timer class takes in the...

Android: Working with Preferences and Settings

In this article I go over using the built-in Android settings and preference system. I have a settings class as well as an activity that manages user input. A spinner is assigned the task of letting the user turn on or off a vibration feature. When the application is started again, the setting is accessed and assigned to the spinner. It also enables or disables the vibrator based on our saved preference. The code is available on GitHub here. Here is our Settings class: public class Settings { public static final String PREFS_NAME = "twoc_settings_example"; public static final String KEY_SETTINGS_AVAILABLE = "settingsavailable"; // your custom settings in the application public static final String KEY_STATE_VIBRATION = "vibrationstate"; // the object used to access settings for this application stored in a predefined setting area SharedPreferences prefAccessor; public Settings(Context parentContext) { prefAccessor = par...

Android: Basic game loop with scaled graphics.

In this article I go over implementing a simple game loop with a custom view. The standard draw method is used with some timing checks so that we can get an approximately consistent refresh rate. This is a somewhat simple way to get started without having to use something OpenGL. The full source code is on GitHub. The main activity handles the creation of our custom view and passes along screen size information once it’s available. This is a key step because it allows us to get that screen information before the view actually starts being displayed. I have a previous tutorial on just that here so I am going to omit the main activity code and the related xml layout file. If you want an exact game update loop structure, you might need further restrict any numeric changes in your game specifically by time rather than having them in the middle of the draw loop that is approximately updated on a given time frame. I haven’t done any testing to verify if that’s needed or not. Also, read up o...

Android: Get the size of a custom view before display.

When developing applications or simple games for Android where you are drawing or writing to a view, an issue arises upon startup of the view. In my case I had a game where I wanted to calculate screen tile size before the game view starts, but with most tutorials I had seen everything happened after the view was fully running. That turned into a chicken and the egg scenario. I needed some key information before I could start the game view, but the default techniques didn’t allow for that. Here is the code on GitHub for this article. I was able to get around the issue with a few techniques. Have my main Activity linked with an xml layout file that has two empty nested linear layouts that fill the screen. Make a custom view that houses the main draw code. For this example, all I do is fill the screen with black and display the available screen size. Attach a ViewTreeObserver.OnGlobalLayoutListener in the Activity that will be the boot code upon getting the screen’s view size. I’m not sh...

A little rant about Android SDK, Java, and Eclipse

I don’t often rant nor say the work “sucks” very often. However as any programmer knows, some tools just frustrate you to no end. Java, the Android SDK, and the Eclipse IDE are something I need to use, but are frequently on my “sucky tools” list. I’ve used a lot of programming languages and tools in my day, and these have made the frustrating Sh** king-of-the-mountain where things like Cobol and JCL reside. Here is an email rant I made recently: Wasted the whole day trying to get the settings activity working as I initially designed it. I eventually dropped that after wasting a large amount of time “playing” around with the code, so now the unit conversions happen outside of the settings activity. In the settings activity, it is just cut-and-dry select setting and select type of units. There is no unit conversion and re-selection of a setting value in the spinner as I had attempted to do earlier. For example, 10 for the search distance will me km or miles depending on if the use...

Android SDK: String Arrays and Controls like Spinners

Due to how the Android SDK and Java are setup it isn’t really a simple task to link up an array with a spinner control. I’ll go over some basics I just learned of the process. In this example I setup a setting control that will allow the user to select from a list of values. The spinner control will be initialized by the global setting value (from a black box object in this example), and a control listener will be defined that allows the user to change the global setting. To define a list of string values that can be loaded into android controls you would do something like this. You can add an arrays.xml file to your project (in the /res/values directory): <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="settinglist01"> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> </string-a...