I tried to get the message and the following line of code works:
TextView dialogMessage = (TextView)dialogObject.findViewById(android.R.id.message);
<
To avoid the code from breaking, when Google decides to change its dialog title view id in the future, here is more reliable solution.
We will simply return the first encountered View
, which its type is TextView
.
//
// Usage: findFirstEncounteredType(getDialog().getWindow().getDecorView(), TextView.class)
//
public static View findFirstEncounteredType(View view, Class<? extends View> klass) {
if (klass.isInstance(view)) {
return view;
} else {
if (!(view instanceof ViewGroup)) {
return null;
}
}
ViewGroup viewGroup = (ViewGroup)view;
for (int i=0, ei=viewGroup.getChildCount(); i<ei; i++) {
View child = viewGroup.getChildAt(i);
View result = findFirstEncounteredType(child, klass);
if (result != null) {
return result;
}
}
return null;
}
You can implement it your self. Create your own class that extend android.app.AlertDialog.Builder. And then create a variable to store your value after using the method setTitle().
import android.content.Context;
import android.support.annotation.StringRes;
public class AlertDialog extends android.app.AlertDialog.Builder {
private Context context;
private String title;
public AlertDialog(Context context) {
super(context);
this.context = context;
}
public AlertDialog(Context context, int themeResId) {
super(context, themeResId);
this.context = context;
}
/**
* Set title using string
*/
@Override
public AlertDialog setTitle(CharSequence title) {
// set the title
this.title = title.toString();
super.setTitle(title);
return this;
}
/**
* Set title using resource string
*/
@Override
public AlertDialog setTitle(@StringRes int titleId) {
this.title = this.context.getText(titleId).toString();
super.setTitle(titleId);
return this;
}
// create public method
public String getTitle(){
return this.title;
}
}
And then use you can use it as ordinary AlertDialog, with implemented method which get its title. Then you can test it:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Crete new alert dialog
AlertDialog dialog = new AlertDialog(this);
dialog.setMessage("Type some message here :D");
dialog.setTitle(R.string.settings_title); // string resource
dialog.setTitle("Dialog1 title"); // string
dialog.show();
// Crete secondary dialog with same title
// using getTitle() method
new AlertDialog(this)
.setTitle(dialog.getTitle())
.setMessage("Copy title from first dialog.")
.show();
}
}
I know this is an old post but I used the accepted answer and added something else useful to me. You can make the Dialog title multiline (default 1 line) using the code below. I also read out the preset title because I added txt asynchronously later on using ContentLoaders
int titleId = getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
TextView dialogTitle = (TextView) getDialog().findViewById(titleId);
if (dialogTitle != null) {
dialogTitle.setMaxLines(4);
dialogTitle.setSingleLine(false);
String txt = dialogTitle.getText().toString();
String txt1 = txt + ":\n\n" + "next line txt";
dialogTitle.setText(txt1);
}
}
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))
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()