layout/ui/design

Basic spinner design

SDK Version: 
M3

In android creating spinners - known as comboboxes or Drop-down list in other systems - is easy. However giving them a basic design is a little tricky.

In most application we dont want to add custom design to spinners, but at least we want thicker items in the list. With the default look, people will have trouble hitting the desired item.

The code, one may try at first, to fill a spinner with elements, will probably look like this:

The layout xml 'mylayout.xml':

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6.  
  7.         <Spinner android:id="@+id/myspinner"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content" />
  10.  
  11. </LinearLayout>

In the Activity oncreate method:

  1. setContentView(R.layout.mylayout);
  2. ArrayAdapter myAdapter = new ArrayAdapter(this,

Custom views in a PreferenceActivity

SDK Version: 
M3

Let's take another look at PreferenceAcitivites, and how we can use other views, than the basic PreferenceActivity views.

TabHost outside a TabActivity

SDK Version: 
M3

I found no really easy to understand example on tabhosts, when you don't want to use a TabActivity, so here is one.

How to draw multiline text to canvas easily

SDK Version: 
M3

There are situations where you have to use a Canvas. What do you do if you have more text than you can properly display?
The problem is, that if you want multiline text on canvas, with the drawText method, you would have to measure how much space a single line of text would take up, and also compare it to the width of the screen, and draw each line separately.
Read more to check my solution.

Fancy time and date picker

SDK Version: 
M3

The other day we needed a fancier than the factory default date and time picker, so I looked around the net, what other options are out there.

default datepicker

PreferenceActivity with custom functionality

SDK Version: 
M3

As we previously desribed in PreferenceActivity Basics a preferences screen with basic features can be done with minimal work, only using XML. Now we will inplement some functionality beyond the base ones, this will require some coding in addition to the XML definition.

We will display the actual value in each preferences summary field, like this:

The base XML will be the same as in the previous example, but we dont need to fill summary fields, they will be filled dinamically.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:title="Settings">
  4.         <EditTextPreference android:title="Name"
  5.                 android:inputType="text"

PreferenceActivity Basics

SDK Version: 
M3
The PreferenceActivity class is a very easy way to create a standard looking settings screen for any application.

Minimal coding is required. The values are stored in SharedPreferences automatically by the PreferenceActivity.

The list of settings, default values, keys to stored under are defined in a very simple xml file.

The xml file should be located in res/xml/ directory and look like something like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:title="Settings">
  4.  
  5.         <EditTextPreference android:title="Name"
  6.                 android:inputType="text" android:summary="Set your login name here"
  7.                 android:key="name" android:persistent="true" android:defaultValue=""></EditTextPreference>
  8.                
  9.         <EditTextPreference android:title="Password"

Removing an app icon from launcher

SDK Version: 
M3


Creating an application that does not appear among the launchable applications with an icon is easy.
Just do not put a launcher activity into AndroidManifest.xml

  1. <intent-filter>
  2.   <action android:name="android.intent.action.MAIN" />
  3.   <category android:name="android.intent.category.LAUNCHER" />
  4. </intent-filter>

Removing an application icon after installation programatically is a bit more tricky.
You can not disable the icon itself, but you can disable one component of an application. So disabling the applications launcher activity will result its icon to be removed from launcher.

The code to do this is simple:

  1. ComponentName componentToDisable =
  2.   new ComponentName("com.helloandroid.apptodisable",
  3.   "com.helloandroid.apptodisable.LauncherActivity");
  4.  
  5. getPackageManager().setComponentEnabledSetting(
  6.   componentToDisable,

Remove autofocus from an EditText in Android

SDK Version: 
M3

When we create a layout with an EditText or an AutoCompleteTextView, for some reason, it always gains the focus on starting.

focused

How to create context menu

SDK Version: 
M3

There are two main methods that you must implement to get this thing work.
First of all, implement and override the onCreateContextMenu():

  1. @Override
  2.         public void onCreateContextMenu(ContextMenu menu, View v,
  3.                         ContextMenuInfo menuInfo) {
  4.                 super.onCreateContextMenu(menu, v, menuInfo);
  5.                 menu.setHeaderTitle("Choose an option");
  6.                 menu.add(0, v.getId(), 0, "Add to favorites");
  7.                 menu.add(0, v.getId(), 0, "See details");
  8.         }

Syndicate content