the Android thread

I'll start of this thread with a review of Android Studio

TL;DR - It's fantastic and terrible, but it's not at fault.


Android Studio now has an official release and is no longer in perpetual beta.

Pros:
It's free, so the price is right.

It's feature filled. Just because it's free doesn't mean bare-bones. It performs many of the tasks you'd expect of a top integrated environment. Code completion is dynamic, so it guesses what you're looking for and updates the top items. It generates tons of code: getters, setters, override ancestors, etc. Navigation is great - Ctrl+Click and a reference in another file is shown. Refactoring is a mystery to me, but I can say that renaming works amazingly well.

It's automated. There are so many details to keep up with, so the environment helps find potential problems and mark them on the scroll bar with colors. You can see the location of TODOs, errors, warning, and etc. More than just warning, it can offer suggestions to automatically fix the code. There's also tools for automation. The best one handles downloading and installing all those SDKs. In code, it shows icons in the columns for referenced colors and graphics. It also automatically shows the value of XML references and colors the code. You'll see "53dp" instead of "@dimen/how_tall_is_this".


Cons:
It's complicated. I still don't know what "Gradle" or "Maven" is, but it's there and it's important. There are so many files and folders that have special meanings that it's impossible to keep up with. It will take a few weeks of constant use to get comfortable in the environment.

Designing UIs is all XML based. The design is very similar to HTML development. The UI is so divorced from code that you can't click on a button and have the program create the "onClick" code for you. The WYSIWYG preview isn't a reality - it only sometimes works.

It can freeze up for a few seconds when editing code. All the automation has a price and it seems it does housekeeping work at the worst moment.

and...

The Android environment is flexible and complicated. The environment can only do so much to help out. All the terrible parts seem to be related to how Android applications are designed. You can't just jump into the IDE and expect to create a simple program without doing a lot of research first.

It would be a nightmare trying to develop the code by hand. Being a sometimes painful IDE is a much better option.
 

Ben_H

Ars Scholae Palatinae
1,059
Just as a note, if you hate XML, you don't actually have to do everything in it. You still can do everything programmatically with Java (IE: do the usual Button button = new Button() thing). I usually end up doing a bit of both. I use XML for a rough layout, and then fine-tune everything with Java.

I actually quite like Android Studio, but that's probably because I've used IntelliJ IDEA a fair amount, and Android Studio is built upon that. If a person has only used Eclipse or something, I can see it being a bit more complex but it isn't that bad. It just takes some getting used to.

The UI builder for Android apps does seem hit or miss at times, which is why I use it how I described above. I will say it is better than the JavaFX Scene builder. What that shows in the UI builder and what shows when you actually run the application can range from being identical to two entirely different things.
 
I just spent a few hours fighting positioning a view because the Preview showed it correctly, but it didn't work at all when run. It just refused to align to parent top, but would align to parent bottom. The "fix" was to set both "top" and "bottom" and it would then alight to the top.

Using code seems to work better for some things than others. Having many different layouts is terrible to maintain when just a line or two of code does the trick. Setting an item to "Gone" in design mode makes it just about impossible to get an idea of the eventual rendering.

I keep running into "gotcha's" about obscured situations. There are still many details that Android Studio doesn't keep up with that it could easily warn about. For example, ListViews seem to grow until the end of the screen no matter what content may follow. The only "fix" I've found is always having a ListView last. Since the view must wrap content, setting a fixed size just won't work.
 

Ben_H

Ars Scholae Palatinae
1,059
There are a number of ways to limit the length of ListView. The most common way I've seen is to use LayoutParams and setting the height (I think that's the one you can set). LayoutParams do take a bit of understanding to figure out completely. For a bit I had issues with them causing my application to crash but it turned out I was setting them at the wrong place in relation to how I was setting up my application.

Another quick and dirty way of limiting list length so it doesn't take of the whole screen is simply to pad it from the bottom. This is super janky but it does work in a pinch. I used bottom padding in conjunction with getting the screen size and resolution and made it so the list dynamically changed size to fit the screen such that it only took up about 30% of the height of the screen. It worked rather well across 5-6 different screen sizes. This seems rather iffy though.

I imagine there are much slicker ways of doing this type of thing. I'm fairly new to Android development and this is what I figured out in the few hours I've done it.
 

kwatz

Wise, Aged Ars Veteran
170
No matter how bad Studio may be, it's leaps and bounds above Eclipse and ADT, at least.

Trying to do large amounts of UI in Java over XML is a recipe for pain, imo. There are things that you must configure in Java, but I generally try to stick to doing as much in XML as possible. Even if XML is distasteful, it's at least declarative and easier to maintain. You also have the resource qualifiers at your disposal, which would be a huge pain in Java.

Dealing with UI states like "gone" is pretty annoying, though. Even worse, try making a ViewFlipper and trying to edit the various views. It'd be handy if there was a little shortcut panel to toggle certain attributes (not to mention a current view attribute for ViewFlipper).
 
I should have kept a list of how many landmines I ran into making my app. If it wasn't for stackoverflow, I'd still not be done. The last issue was checkboxes barely being visible. The fix was a simple line of code, that I never would have thought of myself (thanks stackoverflow).

I ran into a several landmines dealing with ListViews and they're recycled views. You can change the layout of a view item in the list and it won't ever refresh if it's too close to the original layout (requestLayout did squat). They optimized the shit out of ListViews to the point that they're quite difficult to make even seemingly trivial changes.

ViewPager also required quite a bit of work. Fragments are created and destroyed on a whim, so you can't relay information to them. I ended up making my own fake swipe control to keep from re-writing everything with the parent Activity managing all the data. Writing and updating an event listener for each fragment wasn't too fun. Learning and designing at the same time caused only a few of these issues.

---

I'm still quite happy with the results, however. Designing for a touch interface with animations lends for some very intuitive interfaces.
 
[url=http:/civis/viewtopic.php?p=28404137#p28404137:1r3i7fnm said:
kwatz[/url]":1r3i7fnm]...
Trying to do large amounts of UI in Java over XML is a recipe for pain, imo ...

The design is very similar to X-Windows, the XML is really designed to be generated by tools. That being said, quite a bit of time was spent in Text mode over Design mode. The good news is that designing layouts is way easier than HTML/CSS.

The "dream" of a perfectly divorced UI from code is still a dream, but Android is closer than most. Since Android is designed to be so closed off, it seems to be the only way to do it.
 

Eamonn-Dunne

Seniorius Lurkius
2
[url=http:/civis/viewtopic.php?p=28424835#p28424835:1g0bkv4x said:
Jackass JoeJoe[/url]":1g0bkv4x]

ViewPager also required quite a bit of work. Fragments are created and destroyed on a whim, so you can't relay information to them. I ended up making my own fake swipe control to keep from re-writing everything with the parent Activity managing all the data. Writing and updating an event listener for each fragment wasn't too fun. Learning and designing at the same time caused only a few of these issues.

There are several discussions from Jake Wharton from Square among others about ditching fragments completely. For precisely the issues you have described, I've implemented tab based navigation and ViewPager designs using only views and found it worked very well and was much easier to manage.

Also to echo kwatz, I can't recommend EventBus enough for dealing with the mess of lots of call backs.
 
I was talking about issues I had with Fragments in a ViewPager specifically. Once I ditched the ViewPager, most of my problems went away. For each Fragment, I ended up creating an inner "messages" class that the Activity would used for communication. One listener per Fragment, with multiple methods, was used for communication in the other direction. Since the "events" are almost purely user triggered, it wasn't too messy.

I read the blog on Square and my code used to look like theirs before I ditched the ViewPager. I did end up using one ViewFlipper for two of the Fragments, the other three Fragments are manually switched between visible and gone. I had all the same crash issues they did using the ViewPager+Adapter method.


---

On a Side Note: I'm now stuck trying to find a dirty trick method for detecting when the soft keyboard is visible from a service. If I could magically have a tiny View that behaved like a TYPE_APPLICATION window, I could tell when it was moved. I don't have access to a context that would generate the needed tokens to allow me to use WindowManager to add this view to the screen. All I can create are toasts, overlays, and alerts. None of these receive any events when the keyboard is shown. API 21 has some AccessibilityEvent stuff that might help, but that's not an option.

At this point, I can only tell when a view is clicked and inspect the class name for known controls that accept text input.
 
Android Studio 1.1.0 Stable

I can't find a changelog for this release to save my life. I'm noticing only a few small things, but they're good.

Design mode has been updated. The property editor automatically defaults to "dp" units. For things like margins, the "..." button show better categories and includes a Dimensions category. Now, there's no excuse for hardcoding spacing.

The logcat has delete as the first button. It's pressed way more than any other button, so hiding it at the bottom made no sense.
 
Pitfall - Warning

Using the onClick XML attribute for a fragment behaves differently in Marshmallow (and 5.x?).


For my ArsClip Mini app, I have a button in a fragment that notifies the main activity when it is pressed. The "cheap hack" way to implement this at design time is just to set the onClick attribute in the Layout to a public method in the main activity. In Android Studio, it would even offer to use a matching functions it detects in classes.

Using an onClick handler this way will now crash a program. Something has changed in the context of Fragments so that some other view (that I don't recognize) than the Activity is used to execute the onClick event. This causes a crash without a "Report" option.

The workaround is to obviously just assign the handler at runtime, but I was wondering if anyone knows why this happens. Is it a change to security?


Pitfall - Warning 2

Calling findAccessibilityNodeInfosByViewId() with null will cause a crash and reboot in Marshmallow. Previously, just a null was returned. More changed behavior, so expect any AccessibilityNodeInfo method behavior to be different. I'm unsure what context change results in this symptom.
 
3 years later and Android Studio 3.2 is out.

The best feature, by far, is the hardware accelerated emulator for both AMD and Intel chips. Google is no longer provding ARM system images for newer Android versions, so for a period of a few months I had no way to test for newer versions of Android. On Windows, the emulator requires Windows 10 Hypervisor Platform to be installed for non-Intel systems. What used to take minutes to emulate now only takes seconds, so this is a welcome change. Using a phone view in Landscape screws up the display, so it's still got issues. Using a tablet view correctly displays Landscape view all the time.

As usual, most of the support technologies have be updated - like Gradle. I still really don't know what Gradle is, but I know it defines how to build the app. Updating my app to compile took a few hours of research. The errors it produces are cryptic, so I didn't know exactly why it was suddenly unable to find resources. Editing text files is no way to work with a modern environment - automation should have done a much better job updating these generated files.

---

Android itself is breaking stuff again. Like I mentioned earlier, API call will suddenly change behavior and code that has worked for months will fail. This time, using startIntentSenderForResult would crash with a "java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode". To this day, I still cannot find any documentation on this, but Google uses this code for all requestCode responses:

Code:
    static void checkForValidRequestCode(int requestCode) {
        if ((requestCode & 0xffff0000) != 0) {
            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
        }
    }

The Icons returned from getApplicationIcon are no longer DrawableBitmap, which resulted in a nice and nasty crash when trying to cast the result. AppCompatActivity now replaces ActionBarActivity - which was deprecated but now it no longer is usable. The getSupportActionBar call now returns null. This one is my fault, but there were no cases where my apps windows didn't return an actionbar before.

Some permissions now must be obtained at runtime (Android 6.x). TYPE_SYSTEM_ALERT is now an illegal view flag (Android 8) and replaced by TYPE_APPLICATION_OVERLAY, but only for certain versions of Android. So, more spaghetti code for dealing with version changes and more flags for APIs that aren't in your minimum supported version.

The minimum targetSdkVersion for uploading to Google Play is now 26. This means even more behavior changes. My only issue is that they changed this two months before they released Android Studio 3.2. This means I wasn't unable to even run a v26 system image during that time.