2

Possible Duplicate:
Show AlertDialog in any position of the screen

I making an application in which I display an alert dialog.I want use a custom layout for my alert dialog.How can I set its position according to my need in a view/layout.

My code:

LayoutInflater inflater=getLayoutInflater();
View view = inflater.inflate(R.layout.actionbarmain,null);
view.setMinimumWidth(200);
view.setMinimumHeight(400);

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(view);
alertDialog.show();
Community
  • 1
  • 1
Naseeb Sheoran
  • 423
  • 2
  • 8
  • 21

2 Answers2

0

Create an activity with your required layout and specify theme as dialog in manifest.

This is a sample : You need to change it according to your req

public class EditPropertyActivity extends Activity {

    /** Called when the activity is first created. */
    // @Override
    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog);
        setTitle("");
        TextView propName = (TextView) findViewById(R.id.tv_propName);          

        propName.setText(getIntent().getExtras().getString("prop_label"));

        final EditText ed_propValue = (EditText) findViewById(R.id.ed_propValue);
        ed_propValue.setText(value);
        if (tagName.equals("timeout")) {
            ed_propValue.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
        Button btn_save = (Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
        //onclick
        }
    }

In manifest:

        <activity
            android:name="EditPropertyActivity"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

Layout dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_propName"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="25dip"
        android:layout_marginTop="10dip"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/ed_propValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_save"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/lbl_btn_save" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/cancel" />
    </LinearLayout>

</LinearLayout>
Abhilasha
  • 929
  • 1
  • 17
  • 37
0

to add custom layout in alert dialog,you have to inflate layout in it:

testxml:

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

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test" />
            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test" />
            <Button
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test" />
</LinearLayout>

If you want to inflate this xml in alert dialog than:

                AlertDialog.Builder builder=new Builder(YourActivityName.this);
                LayoutInflater inflater=getLayoutInflater();
                View view=inflater.inflate(R.layout.testxml, null);
                builder.setView(view);
                builder.setTitle("New Project");
                builder.setMessage("knknknknknmknknknk");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create();
                builder.show();
AkashG
  • 7,868
  • 3
  • 28
  • 43
  • i have just taken linear layout having 3 buttons having orientation vertical same you can add as many buttons to it and can change the orientation as per your requirements.i implemented this to clear your problem and set this layout as a view in AlertDialog. – AkashG Jul 26 '12 at 12:35