Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, December 15, 2009

Customize Toast Messages in Android

Since I've started developing for the Android platform, I've found Toast messages to be extremely useful. They are simple to instantiate, and look very sleek. Some instances though, wouldn't work with a Toast message because of the nature of their message. An error or notification doesn't seem adequate for a Toast message. Primarily because those types of messages need to grab the users attention, and most often use colors like red or yellow.

I wanted to use the Toast message for a notification, but wanted a yellow background instead. So, I wrapped the static toast method, makeText, with another static class that looks like this..

...


public static void show(Context ctx, String message, boolean notify) {

     Toast t = Toast.makeText(ctx, message, Toast.LENGTH_LONG);
     // if the message is a notify, modify the Toast view

     if (notify) {
            int offset = 100;

            // get the layout, center it, and change the background
            LinearLayout layout = (LinearLayout) t.getView();
            layout.setGravity(Gravity.CENTER);
            layout.setBackgroundResource(R.drawable.toast_frame_yellow);
            // get the text view, change the color, and make it narrower
            TextView tv = (TextView) layout.getChildAt(0);
            tv.setTextColor(Color.BLACK);
            tv.setGravity(Gravity.CENTER);
            tv.setMaxWidth(ctx.getResources().getDisplayMetrics().widthPixels - offset);
      }
                                                                     
      t.show();
                                                                      

                                                                        
}                                                                     

                                                                                                                                           

...


Most of this explains itself. The new background drawable that I used is the btn_default_pressed.9.png image, which is available in the Android source code. I chose this in order to save time. It's already a yellow/orange tone, and it's a 9-patch, so it'll stretch nicely. The maxWidth was changed in order to make the Toast view narrower...just a personal preference.

Here's the end result...


Monday, October 26, 2009

My First Android Experience...meh.

My current interest is Android development. I've put all of my other projects on hold for this too! This isn't the first time I've done Java, in fact I have done a lot of Java before. The thing is though, that I don't remember the last time I had to use sooo much XML! Maybe never. JSON has been apart of my development life for years now, and it's always given me a feeling of resentment towards XML. It's too clunky and bulky, like a rusty old machine.

Maybe I'm just looking at it with the wrong set of eyes. As a web developer, XML means extra overhead, no built-in data types, large file sizes, extra parsing, and non-native support (in most cases). So beginning my first serious Android project, I'm hating XML before I get the chance to understand it's true potential. Truth be told, I'm starting to see how great it can be for certain instances.


Is it just me, or do Java developers love them some XML??? They may be the only ones keeping it alive; they're also keeping Java from getting buried, so they may be used to it. (jk)

I will say that it is very cool to be able to create an entire layout in one XML file! It's extremely versatile, and super flexible. One can change the Look & Feel without having to go into any code. Great! The major headache with this though, is the learning curve. For instance; the widgets, their attributes, layout types, and all of the different pieces used to put these applications together. Like; activities, intents, providers, services, and adapters. Mamma mia!!

Okay, these concepts are not completely foreign to me, having done Java before, but I guess it's been awhile. The structure and elegant design patterns have always intrigued me. In fact, I apply a lot of those patterns within my web applications. Though, sometimes enough is enough. Don't make it complex for the sake of being complex.

So far though, I do prefer iPhone development over Android. The tools that Apple gives you are insanely easy to use. I'm not a designer, so I don't want to spend all of my time on the UI, and using the interface builder, I can easily get most of the Look & Feel taken care of, so that I spend my time where it matters...coding!

Also, one can harness the power of C/Objective-C for their apps. Mmmmmm, speed. While going through Android tutorials, I was a bit discouraged when I saw articles titled, "How to optimize your Android loops", or "How to design for performance". Seriously?!?! I haven't even started yet, and I have to worry about that? I already optimize the hell out of anything that I make, but telling me off the bat that the application is going to need to be optimized is a bit sad. Oh well, it's Java, so what should I have expected?

I'll keep updating this as the project progresses. Perhaps my opinions will change!


PS - I'm aware that Android !== Java, so don't waste your time.