Friday, December 11, 2009

Making TextSwitcher text bigger

I found that the TextSwitcher doesn't honor the textSize property the way I had hoped. I could put that property as high or low as I wanted in the layout XML page and it didn't change anything. However, I found I was barking up the wrong tree. Apparently the TextSwitcher is just a wrapper for TextView objects. What I needed to do is dig down a little deeper and change the size on the TextView object inside of the TextSwitcher rather than the TextSwitcher itself.

A TextSwitcher has a ViewFactory, which controls what happens when the text is changed. ViewFactory has one public method that needs to be overridden - makeView. This is called internally whenever setText is called on the ViewFactory. My original setText simply created a top-left anchored TextView and returned it. This is the code I had used -

public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.LEFT);
return t;

I figured out that this is where I could sneak in and increase the size of my TextSwitcher's text. I simply added a line in that method that called the setTextSize method on my new TextView object -

public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.LEFT);
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(R.dimen.guide_text_size));
return t;

As you can see, I had it pull in a size that I had defined elsewhere in an XML file. I used a constant defined in TypedValue to specify that I wanted my text size to be in dp. And that's it!

Wednesday, November 11, 2009

Slider Bar - The Solution

My quest for a Horizontal Slider Bar has payed off, though it's not a perfect solution. Google has given us something that gets us half-way there - the SeekBar. Really, the SeekBar is for keeping track of progress in a video or music file. It will show you where you are in your file and allow you to jump around by dragging the progress bar. Luckily, it's easy enough to customize to be a pretty decent horizontal slider.

You can create a SeekBar in your main.xml file just like any other component - a simple will get you started. By default, the SeekBar colors everything to the left of the progress bar a very bright orange color. Since, for my purposes, I am not concerned about highlighting the "past", I found a way to make the whole bar a single color. I did this by setting the progressDrawable property to #00000000 (translucent) and then setting the background property to another color that I defined in another XML file. My SeekBar element ended up looking like this -

The progress property in the SeekBar specifies what the initial value will be.

Next, I wanted to be able to take the present location of the SeekBar and turn it into some kind of meaningful information. To do this, I created an OnSeekBarChangeListener. This class has three methods you need to override - onProgressChanged, onStartTrackingTouch, and onStopTrackingTouch. onProgressChange is where the magic happens. The 'progress' parameter in this method represents the location on the SeekBar with 0 being the far left side and 100 being the far right. I wanted the use my SeekBar to find a number between 0 and 20 and assign it to another text box, so I implemented onProgressChange like this -

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
seekbarConvertedValue.setText(String.valueOf(Math.round(progress / 5)));

With this in place, as I move the progress indicator left and right on the SeekBar, I see my textbox update to reflect the current position.

Sunday, November 8, 2009

Slider Bar

Something I have been surprised that the Android API doesn't have is a slider component. Something similar to this, maybe The first thing I hope to document on this blog is my search for slider and, hopefully, information about it's successful conclusion.

Saturday, November 7, 2009

Got a phone with Android, now what?

I've been playing around with programming for the Android for about a month now and yesterday I decided that I was tired of testing things out on the emulated phone that comes with the SDK and that I wanted to get an actual phone to play with. I bought a Motorola CLIQ and have spent most of the morning playing with it. So far it's been a lot of fun and it's a very impressive phone. I'm still getting used to getting around on it and it doesn't seem nearly as polished as my iPod touch, but it's very usable and intuitive.

As I continue learning the ins and outs of Android development, I plan to post some of my problems and solutions here, both as a way of possibly getting some help with my questions and sharing solutions to what are probably some common problems.

Hopefully it all works out!