Android Themes: A dialog without a title


Most of the times, I have seen developers not leveraging the power of themes and styles. Themes and styles are a great way to easily create UI that are  manageable and compatible for various platforms. Here’s the official documentation that explains styles and themes in details, and I consider this portion of documentation to be equally important to any developer working with Android.

For the example of this post, we will see how to make a dialog, a custom dialog, not to have a title. The easiest way to do this, is through code byt writing this snippet in your custom dialog class.

requestWindowFeature(Window.FEATURE_NO_TITLE);

Dialog with Title on Android 2.2


Dialog with no title on Android 2.2

While this would work anyway, and would also be compatible with all the versions of Android. But, it’s a good idea to use more of your themes and styles in your programming, so that your code base is maintainable and your apps would behave and feel consistently. Using styles and themes makes it very easy to tweak or adapt to various platform versions and/or device sizes and resolutions. So, let’s get in.

The application tag, has a configurable attribute called “android:theme” with which you can set a theme to your application as a whole. You can also specify themes for individual themes for all your activities separately. Sounds nice!!! But, let’s stick to one theme for our application as a whole for simplicity.

For this example, we have a theme (it’s actually called a style), called MyThemeSelector as shown below. This is specified in the styles.xml in your values folder. Notice, your custom theme is a child of the Theme, which is one of the system’s default themes.

<resources>        <style name=”MyThemeSelector” parent=”@android:style/Theme”></style></resources>

Ideally, you should also declare your custom theme to extend one of the basic themes that are available with platforms on or above Honeycomb. For example, here we have created another styles.xml in a folder called values-v11, which looks like this.

<resources>
        <style name=”MyThemeSelector” parent=”@android:style/Theme.DeviceDefault.Light”></style>
</resources>

So, your basic theme is now compatible with both older versions and versions greater than Honeycomb. By this, I mean, that, when you run the app, your app would adapt to the platform that it is running on, and would give a consistent look and feel for your users.

Now, coming back to the main problem. “Creating a dialog without title”. Here also, we would use themes, as against code. Here are the two new themes that you would be declaring.

For values folder: (Platform versions older than Honeycomb)

<style name=”My.Theme.Dialog” parent=”@android:style/Theme.Dialog”>
            <item name=”android:windowNoTitle”>true</item>
</style>
<style name=”MyThemedDialog” parent=”@style/My.Theme.Dialog”></style>

For values-v11 folder: (Platform version for Honeycomb and above)

<style name=”My.Dialog.Theme” parent=”@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar”></style>
<style name=”MyThemedDialog” parent=”@style/My.Dialog.Theme”></style>

There’s a subtle difference between the two versions. Of course, other than the parent classes. In older platform versions, there wasn’t a version of the Dialog theme without a title bar. So, what we do here, is to extend the basic Dialog them, and overwrite an attribute, so that our custom “My.Theme.Dialog” is a dialog without a title bar.

But, for Honeycomb and above, the platform itself provides a version of the Dialog theme, without the title bar (or the Action Bar).

And finally, the last step for getting everything to work is set the theme to your dialogs.

MyDialog dialog = new MyDialog(this, R.style.MyThemedDialog);
dialog.setTitle(“Here’s my title”);
dialog.show(); 

Why didn’t we use “My.Theme.Dialog” directly? Well, we could still try and tweak the theme for older versions, in the values folder, by adding a few more attributes.

Dialog with title on Android 4.1


Dialog with no title on Android 4.1

As you can see, for both older and newer platforms, the app runs by adapting itself and gives a consistent look and feel. You didn’t have to do much with your code. 
15 lines of XML is the MAGIC here!!!!  (I didn’t count though, just guessing)

Sample project here.

Bluetooth on Android : Part I

This is a part of a series of posts in which I will put forward a full working app what uses bluetooth on your Android device to discover, connect, pair, send and receive files. The source code would be tagged with each part of this series.

Part 1: The app should be able to discover and list out the devices (paired/unpaired).

To start off with learning about bluetooth on Android, visit the official documentation. The documentation is quite lucid and clear. Here I will try to explain parts of my code.

The first activity (HomeActivity), for now, will have a single button “Discover Devices”, which will take you to the activity(DiscoverDevicesActivity) where you can see the list of devices that are visible by your device.

This activity does a few things, quite a few if-else conditions.

  • First, you need to check if your device supports bluetooth. If you don’t have the hardware capability on your phone, you won’t be able to run this application. Eh!! Most of the phones would obviously have bluetooth. Ummm…Yes… But the emulators don’t. Arrggghhh!!!!
  • Once you are sure that your device has bluetooth capability, the next thing to check if it is enabled or not. If it’s enabled, move on to the next step, else you will need to turn it on first.
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent, REQUEST_ENABLE_BT);

  • Once, you click on “Yes” on the confirmation dialog that you will see, bluetooth radio will be switched on for you. On the screen, the “Scan” button will become active. Once you tap the “Scan” button, the ListView will show the list of devices that your device can discover.

That’s all for the first part of the series. But, there’s a little more to understand abut discovery.

The process of discovery is asynchronous. The list view, currently shows two kinds of devices.

  1. Devices which your phone already knows about (Paired)
  2. Devices which are discovered (Which are not paired with your device)

Getting the already paired devices is simple. The BluetoothAdapter will give you details about such devices.

// Check already discovered devices

Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
       adapter.addDevice(device);
}
adapter.notifyDataSetChanged();

Now comes the actual discovery part. For this, you will need to register a broadcast receiver which will be called whenever a new device is found. After registering the receiver, you need to trigger the discovery by calling the startDiscovery() method of the BluetoothAdapter.

// Scan for new devices
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(devicesReceiver, filter);

bluetoothAdapter.startDiscovery();

On the receiver’s onReceive() method, we pick up the details about the new device found, and add it to our ListView.

public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      // When discovery finds a device
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
          Log.i(TAG, “Device found”);
               
          // Get the BluetoothDevice object from the Intent
          BluetoothDevice device = intent
                  .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          // Add the name and address to an array adapter to show in a ListView
          adapter.addDevice(device);
          adapter.notifyDataSetChanged();

}

The DevicesAdapter is a custom adapter for the ListView which we will be updating in the subsequent posts to show more information about the devices.

As always, you should unregister your receiver, once your activity is paused. Also, in addition to this, you should also cancel the discovery if at all you have started by calling the cancelDiscovery() method of the BluetoothAdapter.

The part 1 of the project can be checked out by fetching the source code and checking out the v1.0 tag from the repository. The complete source code can be found here.

Alternately, if you want to download the source of Part 1 as a zip, use this link to Part 1.

Sign your Android applications for release

The process of application signing can be very confusing for new developers. But, once you have done it 2-3 times, it will be more like a chore for you after that. It’s not very complicated at all. So, here are the steps that you need to follow.

Note: If you already have a self-signed certificate/keystore, skip to step 2(b):

Step 1: Right click on the Android project. From the context menu, select, Android Tools -> Export Signed application package. In the dialog that will open, you will see the name of your project. If you have selected the wrong project, here is a chance to correct yourself. Click on “Next”.

 
Step 2:  The next screen is the most complicated screen that you will get in this process. Here, you can have two situations.
a. If you don’t have a keystore already, you will need to create one first. Once created, you should preserve this keystore somewhere, safely, so that you don’t accidentally delete it. You will need this keystore everytime you want to sign and update your application to the Android Market. Please, please, please, DO NOT LOSE THIS KEYSTORE.
                                       
Since, we are creating a new keystore this time, select the second radio, and browse to a location where you want you new keystore to be saved. Enter and confirm a password. Remember this password. Click on “Next”.

Enter all the fields in this screen and press enter. Now your keystore will be created. Please rememeber all your passwords. Else, this keystore will be useless for you later.For simplicity, keep both the passwords same.

On this screen, specify a name and location of the apk and click on “Finish”. Your signed application package will be created for you in the location you have mentioned here. You can now take this apk and directly upload it to the Android market, or put it on your phone.
b. When you already have a kestore, it’s more easier to sign your application. On the second screen, select, “Use existing keystore”. Enter the location of the keystore and password. Click on “Next”. On the next screen, select you alias from the drop down, enter the alias password, click on “Next”. On the next screen, specify a name and location where you want the apk to be created and click “Finish”. You are done now.

Isn’t it simple? Just one point to note: If you are using an old ADT version, this option of signing your apps from eclipse might now be there. In such a case, you will have to do it through command line. You can find many tutorials on how to achieve that.
Thanks,
Kumar
 

Market your apps aggresively

You have created a cool app, and have also updated it unto the Android Market. Great!!!! So, do you sit idle and wait for people to buy your app? Is your work done? A big “NO”.

The Android Market has around 20000 apps today. And new apps are pouring in daily in large numbers. How do you tell people/users, “Hey, people, here is a cool app that you should check out”? From among these 20000 apps, people will never bother to enter your app’s name in the search box. They just don’t have the time.

So, how do you go about and spread the word about your app? Well, here are some tips.

1. Create a website for your app and update it regularly.
2. Create a blog for your app and provide as much information as you want about upcoming features, bugs and maybe tutorials.
3. Use twitter to reach to your friends.
4. Facebook also is quite a good medium through which you can broadcast your message on everybody’s walls.
5. Create a forum for your app where people can come in and enter their issues/experiences.
6. Request some Android app websites to publish a review for your app.
7. Submit links to your website/blog to a few bookmarking websites, don’t over-do it though.
8. If you want, you can also submit to alternate websites for the Android Market for people who don’t have access to the market.
9. Make sure that your app has an apt description about what it does when you upload it to the market.
10. Ask for feedback from people who use your application regularly.