views:

74

answers:

1

I was following this guide to make a custom AlertDialog. I want to make a dialog with buttons and a title like an AlertDialog, but also with an EditText box, which a regular AlertDialog can't use. So I made the following custom layout containing the EditText box in a file called add_player_dialog.xml in res/layout, which will be called by AlertDialog.Builder's setView():

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/add_player_dialog_layout"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dp"
   >
  <EditText
     android:id="@+id/player_name"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@android:drawable/editbox_background"
     android:textSize="20sp"
     />
</LinearLayout>

Then in the activity (not the root activity, btw) that starts the dialog, I have the following in a switch in onCreateDialog():

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.add_player_dialog,
                   (ViewGroup) findViewById(R.id.add_player_dialog_layout));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Enter player's name")
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
        }
        })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
        })
    .setView(layout);
    AlertDialog alert = builder.create();

The problem is that the activity that is first used when the application is launched uses add_player_dialog.xml for its layout, not main.xml. How do I avoid this?

=================

EDIT: I have found a programmatic way to accomplish this, if anyone is interested:

import android.R.drawable;

EditText et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
et.setBackgroundResource(drawable.editbox_background);
et.setTextSize(20);

=================

If you're interested, here is main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="40dip"
   >

  <LinearLayout
     android:orientation="vertical"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:layout_gravity="center"
     >
    <Button
       android:id="@+id/new_game_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:text="@string/new_game_button"
       />
    <Button
       android:id="@+id/continue_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:text="@string/continue_button"
       />
  </LinearLayout>

</LinearLayout>
A: 

Did you try/have

setContentView(R.layout.main);

in the onCreate method?

If so, sorry, hopefully someone else can better chime in.

James
Yeah, I have that.Thank you for your help tho.
Cnatoli
Can you post your main.xml and the activity your talking about?
James
I edited my post. They're at the bottom.
Cnatoli