Sunday, February 14, 2016

ExpandableListView

I'm building an ExpandableListView in Android.  It's like making an outline that shows/hides subcategories of main categories.  Here's the expandable list view when it's unexpanded:
  • Coffee
  • Tea
  • Juice
Here's expandable list view when it's expanded:
  • Coffee
    • light roast
    • medium roast
    • dark roast
  • Tea
    • Chamomile
    • Spearmint
    • Darjeeling
  • Juice
    • Pineapple
    • Grapefruit
    • Guava
A click on a category expands the list beneath the heading.

It's simple, right?  Except in Java and Android there's a lot of parts.

Here are my notes:

An ExpandableListView is a list that holds lists.  The main lists are the headers.  They're displayed with some kind of indicator next to them to show that they contain more detail inside.

Parts needed:

  1. XML
    1. Layouts
      1. Main layout - define the main container.
        1. LinearLayout
          1. ExpandableListView
      2. Main header layout - format text/background for categories.
        1. LinearyLayout
          1. TextView
      3. Item header layout - format text/background for items.
        1. LinearLayout
          1. TextView
  2. Java
    1. Class that extends BaseExpandableListAdapter:
      1. class DrinkMenuExpandableListAdapter extends BaseExpandableListAdapter
    2. An Activity or Fragment 
      1. To prepare the lists data.
      2. To instantiate the class that extends BaseExpandableListAdapter, passing it the data.
      3. To connect listeners:
        1. setOnChildClickListener
        2. setOnGroupClickListener
        3. setOnGroupExpandedListener
        4. setOnGroupCollapseListener
        5. setOnItemClickListener
I'll add the XML code here along with the Java.  Will also look at putting it on Github.

Monday, February 8, 2016

Just Posted on My Common Lisp Blog: This Old Android Colors App

Last year sometime, I wrote an app to generate colors and selectors and something else.  I just wrote about it on my Common Lisp blog.  You can find the original article here, but I've pasted a copy below, too:

--------------

I wrote an app last year.  It was written in Common Lisp.  It generated colors and selectors for an Android app.  It had an interface.  It kept colors related by degree.  It worked.  I put it away.  I forgot about it.

I can't imagine how Hemingway wrote in such short sentences.  It makes me nuts to feel all constrained by a single thought and not be able to use a conjunction to extend a thought to something maybe meaningful like...

I forgot what I was saying.  Then again, Hemingway wrote amazing books which have withstood the tests of time and I write marginal software and blog posts (also likely marginal.)

The point, the point... what is the point?  I wrote a color app for Android last year.  I wanted to make buttons with stroke and fill colors and lots of them.  But being a total color spaz, I wanted them to look like they had some kind of relation other than "lots of them".  Because we all know from emacs, hmm, editors in general, that lots of colors ends up looking not brown, like in art and oil painting or pastels, but like three vibrantly colored meals suddenly evacuated from... well, you get the point.

So I wrote an app in Lisp to see if I could write an app in Lisp that might help me work with colors while making said colors look visually appealing.  It worked.  Pretty much.  I used well-defined color relationships to create the color relationships.  I think I should have to find a way to put it on the web.  Because I think other people might want to generate the same thing I did, only for their own super-cool apps.  Not that it's the only app like it or that my apps are any great shakes (yet.)  But it's good to have choice and maybe it could be something somebody might want to use.  Maybe.  I never know what anybody wants anymore.

The app allowed for a variable number of controls, a stroke color and a fill color.  Each could be defined by color relations and then the degree/intensity could be modified:

  • analogous
  • triadic
  • split-complement
  • complementary
  • lightness

There might have been some other color relationship thing in there, like monochromatic.  It was a decent prototype, so I think as soon as I finish this calculator, I'll add it to the short list of projects to get done, because web projects in Lisp aren't difficult.  At least I don't remember them being difficult.

Not that I'm writing a teaser or anything.  I find if I don't write a note about it for public consumption, I forget about it.  Like I did last year whenever it was I wrote this app and forgot to write a blog post about it.

The fact that the app generated something like 200 color specifiers (I'd said 1500 earlier because that was the last index on the generated colors, but went back after thinking about it and calculating how many unique colors it would have created) and scads of other detail is what has me thinking it might be useful.  I think that's why I built it in the first place; I wanted colors, but I didn't have ten years to generate all the xml by hand, and if I was going to do all the xml by hand, because I wanted colors, I knew I was going to end up with black, white, and charcoal grey and a lot of impatience at that.  And my inner artist wanted something less Seattle winter and more Seattle spring.  I'm from Seattle.  If you've been, you know why coffee is big, winters are depressing, and spring is like waking from darkness into lots of fresh berries everywhere and colors that leave you blinking as you wonder where all the amazing colors came from.  Spring is an amazing season, but spring in Seattle is pretty nice, though it's been twenty years since I've visited.

I wax poetic sometimes.  But I can't write like anybody else.  Except I used to channel my inner Hunter S. Thompson a long time ago.  But that's a story for another time.

Friday, February 5, 2016

Clipboard Notes

I just added clipboard code to a project I'm going to release soon.  Strangely, it went in easy and worked without problems the first time.  I don't know if that means I'm better at Android or the Clipboard code didn't need me to unwind and figure out dependency within dependency and special rules for sequencing calls for whatever part of the Activity life-cycle an update was happening inside of, with a fragment, for a certain phone, on a Tuesday.

Whatever...Android.

Some quick notes on how to make the clipboard work for text, so next time I don't have to go "groveling docs", as a former co-worker of mine used to say:

Here's the basic way to get an item to the clipboard using "newPlainText" as a shorthand method:

ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clip = ClipData.newPlainText("simple text""string for the clipboard goes here" );
clipboard.setPrimaryClip(clip); // Set the ClipData to the clipboard. One ClipData instance only.

Here's how to populate a ClipData with multiple ClipData.Item instances, so multiple lines of (plain text) data can be added to the clipboard.  The ClipData constructors need a ClipData.Item, so I put the call to instantiate within the loop after checking for null:

ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = null;

while( it.hasNext()) {
   ClipData.Item item = new ClipData.Item( it.next().toString());

   // Initialize the clip the first time through.  Subsequent items are added to the clip via addItem.
   if( clip == null ) {
      ClipDescription desc = new ClipDescription( "simple text", new String[] { ClipDescription.MIMETYPE_TEXT_HTML } );
      clip = new ClipData(desc, item );
   }
   else {
      clip.addItem( item );
   }
}

// The populated clip with added items is added to the clipboard.
clipboard.setPrimaryClip(clip);

It's nice to have something work without having it turn into a jigsaw puzzle.