[JAVA] [Android] How to make Dialog Fragment

HogeActivity.java


public class HogeActivity extends AppCompatActivity  {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button = findViewById(R.id.huga_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //Display a dialog
              DialogFragment dialog = new DialogFragment();
              //Note: getFragmentManager()Will result in an error
              dialog.show(getSupportFragmentManager(),"sample");
            }
        });
  }
}

DialogFragment.java


public class BookReviewDialogFragment extends DialogFragment {
    //Method called when dialog is generated
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //Dialog generation Specify and instantiate the AlertDialog Builder class
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
        //Title setting
        dialogBuilder.setTitle("Dialog title");
        //Text settings to display
        dialogBuilder.setMessage("please fill in the value");
        //Input field creation
        final EditText editText = new EditText(getActivity());
        dialogBuilder.setView(editText);
        //Create OK button
        dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Get value from editText
                String returnValue = editText.getText().toString();
                //Get an instance of HogeActivity
                HogeActivity hogeActivity = (HogeActivity) getActivity();
                hogeActivity.setTextView(returnValue);
            }
        });
        //NG button creation
        dialogBuilder.setNegativeButton("NG", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Close without doing anything
            }
        });
        //returns dialogBulder
        return dialogBuilder.create();
    }
}

■ Refer to the character of the OK button of the dialog fragment from @strings

Resources res = getResources();
//Create OK button dialogBuilder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

Recommended Posts

[Android] How to make Dialog Fragment
How to make shaded-jar
How to make Unity Native Plugin (Android version)
How to make an crazy Android music player
Java --How to make JTable
Android Development-Try to display a dialog-
How to "hollow" View on Android
How to make a Java container
How to make a JDBC driver
How to make a splash screen
How to make a Jenkins plugin
How to make an app using Tensorflow with Android Studio
How to make a Maven project
How to make a Java array
How to make a Java calendar Summary
[Android] How to deal with dark themes
How to make a Discord bot (Java)
How to make asynchronous pagenations using Kaminari
How to use ExpandableListView in Android Studio
Introduction to swift practice output Chapter5
Android Development-Try to display a dialog-
[Android] How to make Dialog Fragment
How to deploy
[Android] How to convert a character string to resourceId
How to make rbenv recognize OpenSSL on WSL
[Android] How to detect volume change (= volume button press)
How to make Spring Boot Docker Image smaller
Make software that mirrors Android screen to PC 1
How to make duplicate check logic more readable
How to make a lightweight JRE for distribution
How to write React Native bridge ~ Android version ~
How to make a follow function in Rails
[Java] How to make multiple for loops single
[Android 9.0 Pie] Example of how to call strings.xml other than Activity and Fragment
How to make an image partially transparent in Processing
How to make batch processing with Rails + Heroku configuration
How to make a factory with a model with polymorphic association
How to develop OpenSPIFe
[Android Studio] How to change TextView to any font [Java]
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to make LINE messaging function made with Ruby
How to use with_option
How to use fields_for
How to use java.util.logging
How to make an oleore generator using swagger codegen
How to use map
[Android / Java] Set up a button to return to Fragment
How to make Java unit tests (JUnit & Mockito & PowerMock)
How to make an almost static page with rails
How to organize information to make programming learning more efficient
How to use collection_select
How to make JavaScript work on a specific page
How to adapt Bootstrap
Introduction to Android Layout
[Android Studio] [Java] How to fix the screen vertically
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use TreeSet
How to write dockerfile