I guess I am going slightly mad... But I can't figure it out. I would like to show a custom alert dialog bound to an Onclick event with a list of data which come from a database.
All works perfectly if I use a simple Dialog, but I would like to make it custom in order to set a title, some text views and buttons on the top of the alert dialog (instead of just show a title with the .setTitle() method).
Here is my code:
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
infoListButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom Dialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog_institutionalinfos, null);
TextView text = (TextView) layout.findViewById(R.id.custom_dialog_text);
text.setText("Hello, this is a custom dialog!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
// inflating the custom institutional expandable list layout
LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
alertDialog.setContentView(v);
InstitutionalInfoListView = (ListView) v.findViewById(android.R.id.list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
alertDialog.show();
}
});
}
});
}
the app crashes when I click on the infoListButton and I get A message in the LogCat:
03-16 19:25:22.905: E/AndroidRuntime(5301): FATAL EXCEPTION: main
03-16 19:25:22.905: E/AndroidRuntime(5301): android.util.AndroidRuntimeException:
requestFeature() must be called before adding content
and an error on the line where I call alertDialog.show(); method. Why this happens? How to resolve?
I just want to add a custom title, a button and a text view to my AlertDialog.
How to do that?
EDIT: how can I custom the title and add a button to the top-right of the dialog to bind it with an onclick event that will dismiss() the dialog???
EDIT: I have created a MyCustomDialog class with this code:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog_institutionalinfos);
TextView dialogTitle = (TextView) findViewById(R.id.custom_dialog_text);
dialogTitle.setText("My First Custom Dialog");
}
}
And this XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/custom_dialog_text"
android:layout_width="80dp"
android:layout_height="wrap_content"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"/>
</RelativeLayout>
The code where I instantiate the dialog:
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
// inflating the custom institutional expandable list layout
LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
dialog.setContentView(v);
InstitutionalInfoListView = (ListView) v.findViewById(android.R.id.list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
But there's no Text displayed in the dialog. I only see the list:
What am I missing? UPDATE: MyCustomDialog class:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_title);
TextView dialogTitle = (TextView) findViewById(R.id.custom_dialog_text);
dialogTitle.setText("Hello I am a dialog");
}
}
The custom_title.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#3FD100"
android:orientation="vertical" >
<TextView
android:id="@+id/custom_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The code where I instantiate MyCustomDialog class:
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
InstitutionalInfoListView = (ListView) dialog.findViewById(R.id.custom_dialog_list);
final MasterDetailArrayAdapter adapter = new
MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
The app crashes with a null pointer exception at
InstitutionalInfoListView.setAdapter(adapter);
UPDATE: the TextView is inside the ListView, how to proceed?
Why do you use an AlertDialog
? You can use a simple Dialog
which generally causes less problems and is more appropriate for custom layouts.
EDIT: Details
First create an XML layout file, just like you would do for an Activity. Then create a class that extends Dialog
. Say your custom dialog class is called CustomDialog
. In public CustomDialog(Context context)
call:
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
// find your views and customize them here
EDIT: HOW TO CREATE CUSTOM DIALOG INSTANCE
android:id="@+id/custom_dialog_list"
// custom Dialog MyCustomDialog dialog = new MyCustomDialog(thisContext);
// you DO NOT need to inflate a new view. by dialog.setContentView(v); you *replace* your XML file with another layout.
// inflating the custom institutional expandable list layout
//LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
//dialog.setContentView(v);
// dont forget to check this line
InstitutionalInfoListView = (ListView) dialog.findViewById(R.id.custom_dialog_list); // set a custom id for your list in the layout
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();