Search code examples
androidandroid-alertdialog

What is android.R.id.message?


I found two SO threads that tell how to center title and message in an AlertDialog object and faked my way through writing a method that I hope to be able to call to center any AlertDialog. It worked fine on a phone and a tablet to display even multi-line messages, both with and without '\n's.

  public void showCenteredInfoDialog(TextView _title, TextView _message) {

    _title.setGravity(Gravity.CENTER);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    AlertDialog.Builder  builder = new AlertDialog.Builder(this);
                         builder.setPositiveButton("OK", null);
                         builder.setCustomTitle(_title);
                         builder.setMessage(_message.getText());
    AlertDialog dialog = builder.show();

    TextView messageView = (TextView) 
                dialog.findViewById(android.R.id.message);
             messageView.setGravity(Gravity.CENTER);
  }

I did a considerable amount of customizing--i.e., I have SOME clue about what I found and did--but one line has left me wondering:

TextView messageView = (TextView) dialog.findViewById(android.R.id.message);

What is android.R.id.message?

Here is all the documentation I could find about it:

android.R.id
public static final int message = 16908299

Where can I find more documentation for the Android.R.id objects (and more)? This seems to be a possible gold mine.


Solution

  • In Android, views contained in layouts generally (though not always) have an id. The purpose of this id is to be able to identify particular views, for example:

    Button button = (Button)layout.findViewById(R.id.button1);
    button.setOnClickListener(...);
    

    When you create a layout XML file, you're generally creating new ids for your views, the syntax is:

    <Button
        android:id="@+id/button1"
        ...
    

    This will create an integer value in your project's R file (R.id.button1).

    android.R.id, on the other hand, contains the ids of views that are either defined in the Android framework, or must be somehow referenced by it.

    In your example, the AlertDialog.Builder creates a TextView with a fixed id, android.R.id.message. That way, you can take the view hierarchy returned by show(), and find the TextView inside it.

    You can take a look at the full list of predefined ids in the documentation, however this list is not very informative in itself. The ids are generally mentioned in the documentation for each particular feature that uses them.

    As an example of the other use case (marking your own view with a predefined android id), when using a ListFragment, if you provide a custom layout then you must include a ListView with id R.id.list. This is because the ListFragment class inspects the inflated layout to look for this widget. See the documentation:

    ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

    Optionally, your view hierarchy can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:empty".