Question

new day - new problem :

i want to have a button, which starts an AlertDialog, in which the user can change the range for a random number

my problem is : it only shows one edit, and i don't know why

(btw : the app doesn't crash : i get a random number, i just don't know where it comes from )

below is my code :

String s;

public void btn_own(View view) {
    int a, b, c;        // a : untere Grenze , b : obere Grenze


    AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Enter range");

        final EditText inputa = new EditText(this);
        final EditText inputb = new EditText(this);
        alert.setView(inputa);
        alert.setView(inputb);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    int aa = 0;
                    int bb = 0;
                    int cc = 0;
                    int e = 0;
                    try {
                        aa = Integer.parseInt(inputa.getText().toString());
                    }   catch (NumberFormatException f) {
                        System.out.println("not a number: " + inputa.getText().toString());
                        f.printStackTrace();
                    }

                    try {
                        bb = Integer.parseInt(inputb.getText().toString());
                    }   catch (NumberFormatException g) {
                        System.out.println("not a number: " + inputb.getText().toString());
                        g.printStackTrace();
                    }
                    cc = bb - aa;
                    Random rand = new Random();
                    int d = rand.nextInt(cc);
                    e = d + bb;
                    Integer out = new Integer(e);
                    s = out.toString();

                    new AlertDialog.Builder(Decider.this)
                            .setMessage(s)
                            .show();

                }
            });
        alert.show();



}   

hope you can help me .. i am new in development and that's my first real project

Was it helpful?

Solution

the second view it s overriding the first in AlertDialog you put only one view! so use a linearlayout trick as here below:

String s;

public void btn_own(View view) {
    int a, b, c;        // a : untere Grenze , b : obere Grenze


AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Enter range");
     LinearLayout linearLayout = new LinearLayout(context);
     linearLayout.setOrientation(1);
     linearLayout.setGravity(Gravity.CENTER);


    final EditText inputa = new EditText(this);
    final EditText inputb = new EditText(this);
     linearLayout.addView(inputa );
     linearLayout.addView(inputb );

    alert.setView(linearLayout );


        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                int aa = 0;
                int bb = 0;
                int cc = 0;
                int e = 0;
                try {
                    aa = Integer.parseInt(inputa.getText().toString());
                }   catch (NumberFormatException f) {
                    System.out.println("not a number: " + inputa.getText().toString());
                    f.printStackTrace();
                }

                try {
                    bb = Integer.parseInt(inputb.getText().toString());
                }   catch (NumberFormatException g) {
                    System.out.println("not a number: " + inputb.getText().toString());
                    g.printStackTrace();
                }
                cc = bb - aa;
                Random rand = new Random();
                int d = rand.nextInt(cc);
                e = d + bb;
                Integer out = new Integer(e);
                s = out.toString();

                new AlertDialog.Builder(Decider.this)
                        .setMessage(s)
                        .show();

            }
        });
    alert.show();

}

OTHER TIPS

by "one edit", I assume you mean "one edittext". the reason why you are getting only one edittext is that you are using setView() method twice with two different edittext components. setView() method is actually used to set the view of the dialog, so the solution to your problem is that you can use a layout file with two edittext's wrapped inside a linearlayout. here is how you can do this -

define a layout file in your layout folder like below and give a name to this layout file of your choice

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/set_default_header"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/lem_background_small"
     android:orientation="vertical"
     android:padding="10dp" >
     <EditText
        android:id="@+id/randomno1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:layout_gravity="bottom"
        android:inputType="text"
        android:text=""
        android:hint="Enter Budget"
        android:textAppearance="?android:attr/textAppearanceSmall" />
     <EditText
        android:id="@+id/randomno2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:layout_gravity="bottom"
        android:inputType="text"
        android:text=""
        android:hint="Enter Budget"
        android:textAppearance="?android:attr/textAppearanceSmall" />
      </LinearLayout> 

the in the setView() method write setView(R.layout.yourlayoutfilename) instead of

    alert.setView(inputa);
    alert.setView(inputb);

and then inside setPositiveButton() method of your dialog you can access these edittexts by their id and do what you want to.

I hope this solves your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top