Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the AlertDialog title?

I tried to get the message and the following line of code works:

TextView dialogMessage = (TextView)dialogObject.findViewById(android.R.id.message);

But when I try to get the title using the following line it returns null

TextView dialogTitle = (TextView)dialogObject.findViewById(android.R.id.tittle);
like image 488
alitha Avatar asked Mar 20 '15 08:03

alitha


People also ask

How do I get view from AlertDialog?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is the use of AlertDialog?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.


2 Answers

I checked up the code of the AlertDialog. Internally they use R.id.alertTitle to initialize the AlertDialog title's TextView. You can use getIdentifier to retrieve it:

int titleId = getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
   TextView dialogTitle = (TextView) dialogObject.findViewById(titleId);
   if (dialogTitle != null) {

   }
}

Edit: for AppCompat, the third argument of getIdentifier should be the package name of your app. You can retrieve the latter with context.getPackageName()

like image 58
Blackbelt Avatar answered Sep 21 '22 07:09

Blackbelt


I know the question mentions AlertDialog, but if you came here through a Google search and looking for the answer for a DialogFragment:

getDialog().findViewById(android.R.id.title))
like image 41
friederbluemle Avatar answered Sep 24 '22 07:09

friederbluemle