2

Coming from VB world I learned today that dialogs are not modal in Java. I have the task of converting a program we have written in VB.net for windows based Motorola devices to Java using Android Studio for Android Motorola devices. I am trying to make it as similar to forms in windows. I am at a form that has that has several messageboxes that prompt for input. I tried doing java but noticed that dialogs don't always wait for response. I have read that using dialogfragments can accomplish what I am trying to do. Here is the XML that I had for the first dialog.

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

    android:background="@color/colorBackgroundGray"
    >

    <TextView
        android:id="@+id/theText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Holding Text"
        android:textAlignment="center"
        android:textColor="@color/colorBlack"
        android:textSize="30dp" />


    <Button
        android:id="@+id/buttonYes"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/message"
        android:layout_margin="8dp"
        android:onClick="onClickYes"
        android:text="Yes"
        android:textSize="26dp" />

    <Button
        android:id="@+id/buttonNo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/message"
        android:layout_margin="8dp"
        android:onClick="onClickNo"
        android:text="No"
        android:textSize="26dp" />


</RelativeLayout>

Here is how I was calling the dialog.

 final Dialog dialog = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                                dialog.setContentView(R.layout.dialog_location);
                                dialog.setTitle("New Location Setup");
                                TextView message = (TextView) dialog.findViewById(R.id.theText);
                                message.setText("Is this location a Pick Bin");
                                Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
                                Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
                                dialog.show();

Having question on what changes would need to be done to turn the dialog into a dialogFragment. I appreciate and clarity anyone can shed. Also the calling of the dialogFragments would be done during a loop

Thanks

Nominalista
  • 4,632
  • 11
  • 43
  • 102
Tom
  • 151
  • 1
  • 12

1 Answers1

5

First extend DialogFragment and use AlertDialog.Builder to create the dialog inside method onCreateDialog.

public class MyDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(inflater.inflate(R.layout.dialog_location, null))
               .setMessage("Is this location a Pick Bin")
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {                       
                       // Handle "yes" button click here.
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Handle "no" button click here.
                   }
               });
        return builder.create();
    }
}

Then you can show DialogFragment by using show() method:

DialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "MyDialogFragment")

Small advise - don't use dp in your widget textSize, use sp. You can read the explanation here.

Nominalista
  • 4,632
  • 11
  • 43
  • 102
  • getting a can't resolve method 'setCOntentView(int)' – Tom Jun 13 '18 at 16:48
  • the line public classmydialog is underlined wit message of This fragment inner class should be static. – Tom Jun 13 '18 at 17:51
  • Put this class into separate java file. – Nominalista Jun 13 '18 at 17:52
  • Calling this during a loop. does the loop have to be in class also. Outside the MyDialogFragment program is performing the loop without showing the dialog. Is this meant to show the dialog for each time the loop iterates the next i – Tom Jun 13 '18 at 19:05
  • I am seeing that. Here is whats happening I return two records from database. I need to check if each of them are to be set to a certain status. So I have for ( i = 0; i < mylevels.size(); i++) andI want to call dialogfragment and ask question about each record. If doing this in loop isn't the way to go I am stumped. Do I need to have new activity for each question I need to ask about each record? I know this isn't .net so I am not familiar. – Tom Jun 13 '18 at 19:19
  • I think you should post another question related to that topic. It's hard to understand it here without more code. – Nominalista Jun 13 '18 at 19:37
  • new question under "How to keep program to pause to allow user input" – Tom Jun 13 '18 at 19:54