4

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.

Community
  • 1
  • 1
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • 2
    Take a look at [this](http://stackoverflow.com/questions/6804053/understand-the-r-class-in-android) – khakiout Jul 15 '14 at 01:27

3 Answers3

6

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".

matiash
  • 54,791
  • 16
  • 125
  • 154
  • @matiash--Yeah, I went to http://developer.android.com/reference/android/R.id.html but found *no* documentation for `message`, which is what brought me here, looking for more *complete* documentation. You'd think they'd provide a link; it's not obvious what `message` refers to, although I guess I did figure it out eventually. Maybe there's enough documentation there despite so many entries having no documentation other than the constant value. – DSlomer64 Jul 15 '14 at 17:24
3

android.R.id.message refers to the Resource with an id named message.

For example, the TextView here:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button 
    android:id="@+id/backbutton"
    android:text="Back"
    android:layout_x="10px"
    android:layout_y="5px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/message"
    android:layout_x="10px"
    android:layout_y="110px"
    android:text="First Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</AbsoluteLayout>

Excuse me when I tell you that if you didn't know that, you really should read the Android Training Guide: http://developer.android.com/training/index.html

Mikel Pascual
  • 2,202
  • 18
  • 27
  • 2
    `android:id="@+id/message"` This is _not_ the same as `android.R.id.message`. You're defining a _new_ resource id in this layout. – matiash Jul 15 '14 at 01:39
  • In Android, you define the id in the layou, just as I did. Then, you can access it programmatically using Android.R.id.something. You really have to read the Training. You are missing just too many concepts to start coding anything. It'll take you too days, and you'll learn a lot of necessary info. – Mikel Pascual Jul 15 '14 at 02:16
  • 2
    That is not correct. `android.R.id` is [this](http://developer.android.com/reference/android/R.id.html) class, it's defined in the framework itself, it does not contain your own view ids, and is not extensible. I think you're confusing it with the generated R class for your project. While they have the same structure, they are **not** the same. – matiash Jul 15 '14 at 02:18
  • 1
    @MikelPascual--You're excused. I've been so many places getting bits and pieces for the specific things I want to do at a time that I needed the focus. I've been there, but it's a long read, so I've been relying on my "Android Development in 24 Hours" too much--it's pretty superficial, as the title clearly implies. It barely mentions R. At least from an online search and its index. BTW, I "fell" into the `message` trap, too--that's why I renamed the parameter `_message`. Then I got confused. Now I'm not. – DSlomer64 Jul 15 '14 at 17:18
2

@matiash 's answer gives some good insight.

In the simplest terms, suppose you were providing a custom layout for a dialog, you would just access a view using R.id.viewId - an id which you defined in the custom xml layout.

android.R.id.message will let you access views that are predefined by android. Such as in your case, you're using an AlertDialog with a predefined layout. So, using the particular id you'll be able to access the TextView where you can set a message in the AlertDialog.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34