Monday, May 30, 2011

Android strings.xml

Here's a strange one - I was working on an Android application, using the strings.xml file for the apps string resources. It so happened that I needed to have two strings in both underlined and non-underlined forms. So I added:
<string name="average_underlined"><u>average</u></string>
<string name="recent_underlined"><u>recent*</u></string>
<string name="average">average</string>
<string name="recent">recent</string>


to the strings.xml file. However, when I ran the app the underlined versions appeared where I expected the regular version to appear. After checking and rechecking I was happy the code was correct. Bizarrely, the fix was to have the non-underlined versions declared first:
<string name="average">average</string>
<string name="recent">recent*</string>
<string name="average_underlined"><u>average</u></string>
<string name="recent_underlined"><u>recent*</u></string>


If anyone can explain this to me please do get in touch!

Thursday, May 26, 2011

Android - Aligning Two Views Side-by-Side

This was harder than I thought it would be - I tried playing about with relative views plus alignment settings, table views, gravity and so on.

The answer turned out to be use a LinearLayout with orientation set to horizontal and for each view (buttons in my case) set width to fill parent and layout_weight to "1":


<LinearLayout
  android:id="@+id/Table01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_alignParentBottom="true">    
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="reset">
</Button>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="delete">
</Button>
  </LinearLayout>

Monday, May 2, 2011

Android ListViews

I've been working on an Android ListActivity which needs to have two TextViews anchored to the bottom of the screen. The anchoring was fairly simple, but when the list extended to the bottom of the screen, it obscured the anchored TextViews.

The solution is to declare the ListView last in your xml layout file, and specify what you want to be above it and what you want to be below it. I.e.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android
    ....>

    <TextView
        android:id="@+id/my_screen_header"
        .......>
    </TextView>

    <RelativeLayout
        android:id="@+id/my_screen_footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <TextView
            android:id="@+id/first_footer_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp">
        </TextView>
        <TextView
            android:id="@+id/second_footer_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/first_footer_text_view"
            android:textSize="15sp">
        </TextView>
    </RelativeLayout>
    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/my_screen_footer"
        android:layout_below="@id/my_screen_header"/>
</RelativeLayout>

Thanks to http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/