Showing posts with label Apps. Show all posts
Showing posts with label Apps. Show all posts

Tuesday, July 5, 2011

Custom title for your apps

The default title bar for the apps might not suit the theme of your app. You also might want to add a few more things to the title bar which the default title bar doesn’t help you with. This example will help you develop your own Title Widget which you can directly embed in your layouts containing a left icon, a title text and a progress bar. You can also control the contents of these three widgets and change it as required in your activities. Here we go.

1. The first thing you should do is to switch off the default title. You can do this easily by specifying in your AndroidManifest.xml file that do not intend to use it.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.beanie.samples.titlewidget" android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".TitleWidgetActivity"
         android:label="@string/app_name">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>
Here, in the application tag, set the theme of your app to use the NoTitleBar theme.

2. Create a title_widget.xml which would be the custom layout for your title bar. You can technically add any number of widgets here and finally display it with your custom title. As an example, we will add an ImageView, a TextView and a ProgressBar here.

3. Create a TitleWidget.java class which extends LinearLayout(you can use any Layout) which loads this layout file and exposes methods to modify the contents of the widgets.

4. Add the TitleWidget class as the first view in all your activity layouts’ files to display your custom title bar.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <com.beanie.samples.titlewidget.TitleWidget
android:id="@+id/titleWidget"
  android:layout_width="fill_parent"
android:layout_height="wrap_content">
 </com.beanie.samples.titlewidget.TitleWidget>
</LinearLayout>
5. Now, in your activity, you can initialize this TitleWidget as any view, since it is a LinearLayout now, and can control the contents of all the widgets.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Initialize title Widget
        TitleWidget titleWidget = (TitleWidget)
                             findViewById(R.id.titleWidget);

        // Call the methods to change the underlying widgets
        titleWidget.setTitle("My Custom Title");
        titleWidget.setIcon(R.drawable.icon);
        titleWidget.showProgressBar();
        titleWidget.hideProgressBar();
    }



Now you have your own custom title. On the surface, it doesn’t actually feel like a title bar, and yes, it isn’t. It’s just a custom view, that you are trying to make it look like a title bar. Remember!! We switched off the titles by specifying that our app’s theme to “NoTitleBar”. :)
You can find the whole source code here.

Monday, September 7, 2009

SMS Blocking in Android

Recently, I tried to develop an app that would block sms from certain numbers that you can choose. It seems that you cannot completely block the incoming sms. These were the problems that I faced while working with this app.

1. There are no APIs supplied, that handles the SMS inbox (I mean, no content providersm which can be directly queried to fetch records from the sms table). However, you have a workaround of finding the tables where the sms is stored, and then querying and updating that table. Well, there is actually a content provider, that exposes this table to other applications, but it is not documented anywhere. So, I have to hardcode the column values. This is potentially dangerous, if in the next Donut release, Google changes the column names, my application would fall apart.

2. On receiving of an SMS, I have 2 things to do. Remove the SMS from the sms table, and disable the notification. Deleting the sms from the table was pretty easy, but I couldn't find a way to remove the Notification. Perhaps, there is no way that we can disable this notification. One workaround might be, to temporarily change the Notification settings, the moment you sense an incoming message from a blocked number. And, after you delete the message from the sms table, you just turn on the notification. I haven't worked on this, but this might just work.

3. If the Messaging application is already open, and an sms arrives, weird things start to happen. I can see the blocked sms being deleted, and the list of sms gets updated. But the next message in the list is marked as "Unread". Probably, this is done by the Notification, I don't know.

4. SMS broadcast, is definitely, an un-ordered one. So, the easiest way, that could have worked, would have been just to abort the broadcast. But in this case, you don't have this option. so, once you recieve this broadcast, you have to manually go and delete the message, by matching the incoming number, timestamp etc. Timestamp method isn't foolproof. Because, the timestamp you receive, is the timestamp of the broadcast, and not the time when the sms is written into the DB. So they wont match. You should pick up the incoming number, and check for the timestamp within a range of about 1 seconds. This should give you the sms that you need to delete.

5. There's another problem. When you recieve the broadcast, the system wont write the sms into the table, before it is sure that all the broadcast receivers have done their work. So, it waits for some time, and then writes the sms into the table. Before this, if you try to delete the sms, obviously, you would not find it in the table. So, make your code sleep for about a second, and then go and delete the SMS. That should do it. But, the most important problem still persists. THE NOTIFICATION. I guess, it can't be removed.