How To Get The Alertdialog Title?
Solution 1:
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:
inttitleId= getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
TextViewdialogTitle= (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()
Solution 2:
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))
Solution 3:
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)//publicstatic View findFirstEncounteredType(View view, Class<? extends View> klass) {
if (klass.isInstance(view)) {
return view;
} else {
if (!(view instanceof ViewGroup)) {
returnnull;
}
}
ViewGroupviewGroup= (ViewGroup)view;
for (int i=0, ei=viewGroup.getChildCount(); i<ei; i++) {
Viewchild= viewGroup.getChildAt(i);
Viewresult= findFirstEncounteredType(child, klass);
if (result != null) {
return result;
}
}
returnnull;
}
Solution 4:
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;
publicclassAlertDialogextendsandroid.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
*/@Overridepublic AlertDialog setTitle(CharSequence title) {
// set the titlethis.title = title.toString();
super.setTitle(title);
returnthis;
}
/**
* Set title using resource string
*/@Overridepublic AlertDialog setTitle(@StringRes int titleId) {
this.title = this.context.getText(titleId).toString();
super.setTitle(titleId);
returnthis;
}
// create public methodpublic String getTitle(){
returnthis.title;
}
}
And then use you can use it as ordinary AlertDialog, with implemented method which get its title. Then you can test it:
publicclassMainActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Crete new alert dialogAlertDialogdialog=newAlertDialog(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() methodnewAlertDialog(this)
.setTitle(dialog.getTitle())
.setMessage("Copy title from first dialog.")
.show();
}
}
Solution 5:
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
inttitleId= getResources().getIdentifier( "alertTitle", "id", "android" );
if (titleId > 0) {
TextViewdialogTitle= (TextView) getDialog().findViewById(titleId);
if (dialogTitle != null) {
dialogTitle.setMaxLines(4);
dialogTitle.setSingleLine(false);
Stringtxt= dialogTitle.getText().toString();
Stringtxt1= txt + ":\n\n" + "next line txt";
dialogTitle.setText(txt1);
}
}
Post a Comment for "How To Get The Alertdialog Title?"