Saturday, June 11, 2011

Android TableLayout and expanding TextView

Many forms I am used to seeing have the following layout

Label     Input
Label     Input
...

Label     Input
 Save Button

To implement this in android I used a TableLayout inside a ScrollView. Inside the TableLayout I have TextViews and EditTexts. I wanted the EditTexts to take most of the width, so I put android:stretchColumns="1" inside the TableLayout tag. Everything looked fine until I entered a long text. I just couldn't see it. The text is not wrapped and no multi lines appear. It appears that the same column I marked for stretching should be marked for shrinking. So adding  android:shrinkColumns="1" solved my problem. And then I found the explanation in android docs (of course!): 
 If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container. It is important to remember that a column can be both shrinkable and stretchable. In such a situation, the column will change its size to always use up the available space, but never more. 
Here is the layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="1" 
             android:shrinkColumns="1"
            android:padding="3dip">
        <TableRow>
            <TextView android:text="@string/first_name"
                      android:padding="3dip"/>
            <EditText android:id="@+id/first_name" />
        <TableRow>

        <TableRow>
            <TextView android:text="@string/last_name"
                      android:padding="3dip"/>
            <EditText android:id="@+id/last_name" />
        </TableRow>

        <View android:layout_height="2dip"
              android:background="#FF909090"
              android:layout_marginBottom="3dip"/>

        <Button android:id="@+id/save"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/save_btn"
                android:onClick="save"/>

    </TableLayout>
</ScrollView>


Monday, June 6, 2011

Measurements in Android


We are used to designing interfaces in terms of pixels. But when developing for mobile devices we eventually have to deal with different displays. Resolution-independent measurements help to solve the problem of fitting content into different devices.
Android supports the following units:

  • px (pixels): Dots on the screen.
  • in (inches): Size as measured by a ruler.
  • mm (millimeters): Size as measured by a ruler.
  • pt (points): 1/72 of an inch.
  • dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.
  • dip: Synonym for dp, used more often in Google examples.
  • sp (scale-independent pixels): Similar to dp but also scaled by the user’s font size preference.

For making your interface scalable to any current and future type of display, it is recommended you always use the sp unit for text sizes and the dip unit for everything else. Hello Android, 3rd edition

Friday, June 3, 2011

Keeping screen awake when plugged

Here is how to keep the screen awake for certain activity, but only when the phone is plugged.
The provided code should be place inside Activity.

 private BroadcastReceiver powerConnectReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) > 0;
findViewById(R.id.map).setKeepScreenOn(plugged);
}
};

….

@Override
protected void onResume() {
registerReceiver(powerConnectReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
super.onResume();
}

@Override
protected void onPause() {
unregisterReceiver(powerConnectReceiver);
super.onPause();
}

as you estimate...

do not forget the planning phase
Tasks consideration and creation will take a certain amount of time...