Android Development-Try to display a dialog-

Introduction

Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well.

Target person

--A person who can write Java somehow. --Someone who is ambiguous in Android development but can do it a little.

Display dialog

Dialog configuration

スクリーンショット 2018-11-12 11.15.42.png ## Dialog creation / display procedure The process for creating and displaying the alert dialog is as follows. ** 1) Create a class that inherits DialogFragment. ** **
public class SimpleDialogFragment  extends DialogFragment {

** 2) Describe the dialog body generation process in the onCreateDialog () method. ** **

public Dialog onCreateDialog( ... ) { 
Dialog generation process(See below)
}

** 3) Write the following code when displaying a dialog for activities etc. **

--New the above class.

SimpleDialogFragment dialog = new SimpleDialogFragment();

--Get Fragment Manager.

FragmentManager manager = getSupportedFragmentManager();

--Display the dialog with the show () method.

<The following two arguments.>
1st:Fragment manager
No. 2:Tag string that identifies the dialog(Arbitrary string)
dialog . show ( manager , “ ... ” );

(note) Both DialogFragment.FragmentManager import the v4 package.

○ import android.support.v4.app.FragmentManager 

× import android.app.FragmentManager

Dialog body generation procedure

Follow the procedure below to generate the error dialog body described in the onCreateDialog () method of the dialog fragment class you created.

** 1) Create an alert dialog builder object. ** **

AlertDialog.Builder builder = new AlertDialog.Builder(context);

** 2) Set the display contents. ** **

・ Content area
builder.setMessage( ... );
·title
builder.setTitle( ... );

** 3) Set the action button. ** **

builder . setPositiveButton(“Button label”,Listener object when the button is tapped(See below)); 

There are also * setNegativeButton () * and * setNeutralButton () *.

** 4) Create a dialog object. ** **

AlertDialog dialog = builder.create( ) ;

(note) ʻThe AlertDialog class imports from the v7 package `

Click processing

The process when the action button is tapped in the alert dialog is described in the listener class. The procedure is as follows. ** 1) Create a nested class that implements the DialogInterface.OnClickListener interface. ** **

** 2) Describe the process in the onClick () method. ** **

private class DialogButtonClickListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog (dialog body),int which (constant value of tapped button)) {
            //Click processing
        }
}

(note) If there are multiple action buttons, a listener class may be prepared for each button, but it may be branched using the second argument (int which).

Cooperation with activities

** (1) Get activity **

To get the activity that the dialog body is displayed in the dialog fragment, use the ** getActivity () ** method.

Activity parent = getActivity();

** (2) Data I **

To pass data from an activity to a dialog fragment, use the following method. ** 1) Create a Bundle object. ** **

Bundle extras = new Bundle();

** 2) Describe the process in the onClick () method. ** **

extends.put data type("name",value);

** 3) Set the Bundle object of 2) in the dialog fragment object. ** **

dialog.setArgments(extends);

** 4) In the dialog fragment, get the above Bundle object with the getArgments () method and retrieve the data. → The extraction method is the same as for Intent (null check in advance) **

Sample code

FullDialogFragment.java


public class FullDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Normal dialog");
        builder.setMessage("Is it OK?");
        builder.setPositiveButton("ok", new DialogButtonClickListener());
        builder.setNeutralButton("??", new DialogButtonClickListener());
        builder.setNegativeButton("NG", new DialogButtonClickListener());
        AlertDialog dialog = builder.create();
        return dialog;
    }

    /**
     *A member class that describes what happens when a dialog button is pressed.
     */
    private class DialogButtonClickListener implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Activity parent = getActivity();
            String msg = "";
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    msg = "Normal dialog:OK is selected.";
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    msg = "Normal dialog: ??Was selected.";
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    msg = "Normal dialog:NG has been selected.";
                    break;
            }
            Toast.makeText(parent, msg, Toast.LENGTH_SHORT).show();
        }
    }
}

Activity class (excerpt)

FullDialogFragment dialog = new FullDialogFragment(); 
FragmentManager manager = getSupportFragmentManager();
dialog.show(manager,"FullDialogFragment");

TIPS

Date selection dialog

For the date selection dialog, "new" ** DatePickerDialog ** and ** show () **. The arguments are as follows.

-① Context -② Listener object -③ Initial value "year" -④ Initial value "month" → Java "month" starts * 0 * internally. -⑤ Initial value "day"

The listener object * implements * the ** DatePickerDialog.OnDateSetListener ** interface and describes the process in the ** onDateSet () ** method. The arguments are as follows.

-① Date specification screen parts -② Selected "year" -③ Selected "month" -④ Selected "day"

Sample code

public void showDatePickerDialog(View view){
        Calendar cal = Calendar.getInstance();
        int nowYear = cal.get(Calendar.YEAR);
        int nowMonth = cal.get(Calendar.MONTH);
        int nowDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(DialogSampleActivity.this, new DatePickerDialogDateSetListener(),nowYear,nowMonth,nowDayOfMonth);
        dialog.show();
}

private class DatePickerDialogDateSetListener implements DatePickerDialog.OnDateSetListener {

        @Override
        public void onDateSet(DatePicker view,int year,int month,int dayOfMonth) {
            String msg = "Date selection dialog:" + year + "Year" + (month + 1) + "Month" + dayOfMonth + "Day";
            Toast.makeText(DialogSampleActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
}

Time selection dialog

For the time selection dialog, "new" ** TimePickerDialog ** and ** show () **. The arguments are as follows.

-① Context -② Listener object -③ Initial value "hour" (24-hour unit) -④ Initial value "minute" -⑤ Whether to display 24 hours (true is 24 hours notation)

The listener object * implements * the ** TimePickerDialog.OnTimeSetListener ** interface and describes the process in the ** onTimeSet () ** method. The arguments are as follows.

-① Time specification screen parts -② Selected "time" --③ Selected "minutes"

Sample code

public void showTimePickerDialog(View view){
        TimePickerDialog dialog = new TimePickerDialog(DialogSampleActivity.this, new TimePickerDialogTimeSetListener(),0,0,true );
        dialog.show();
}

private class TimePickerDialogTimeSetListener implements TimePickerDialog.OnTimeSetListener {

        @Override
        public void onTimeSet(TimePicker view,int hourOfDay,int minute) {
            String msg = "Time selection dialog:" + hourOfDay + "Time" + minute + "Minutes";
            Toast.makeText(DialogSampleActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
}

that's all. It was how to display the dialog on Android. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.

Recommended Posts

Android Development-Try to display a dialog-
[Android] Review dialog display
[Android] Implement a function to display passwords quickly
[Android] How to make Dialog Fragment
Sample to display (head-up) notifications on Android
Android Easily give a "press" to a button
Ability to display a list of products
A note about adding Junit 4 to Android Studio
[Android] Inherit ImageView to create a new class
[Android] How to convert a character string to resourceId
How to display a web page in Java
(Android) Try to display static text using DataBinding
[Android] Two ways to get a Bluetooth Adapter
[Android / Java] Set up a button to return to Fragment
Introduction to Android Layout
How to display a browser preview in VS Code
[Introduction to Android application development] Let's make a counter
How to take a screenshot with the Android Studio emulator
I tried adding a separator line to TabLayout on Android
Android app to select and display images from the gallery
How to display a graph in Ruby on Rails (LazyHighChart)
I'm glad to have a method to blink characters on Android
How to implement one-line display of TextView in Android development
I want to use FireBase to display a timeline like Twitter
Introduction to Android application development
How to leave a comment
[Java] How to display Wingdings
Pass a variable to Scope.
Practice to unify Dialog implementation
Knowledge required to display tweets
To write a user-oriented program (1)
[Android] Connect to MySQL (unfinished)
How to insert a video
How to create a method
I tried to create a simple map app in Android Studio
A newcomer tries to summarize the Android view (beginner Android application development)
Source to display character array with numberPicker in Android studio (Java)
What to do if you get a DISPLAY error in gym.render ()