[JAVA] Programming from 51 years old Note: Show multiple views in android dialog / with or without xml

To display multiple views in a dialog, inflate the LinearLayout where the views are placed into the dialog. It's a function I want to use later, so a little memo.

This time, note the dialog that uses layout.xml and the dialog that dynamically generated Layout with new LinearLayout without using layout.xml.

Use Layout / xml (use App / res / layout / --- .xml file)

Constitution 1.MainActivity.java 2.MyDialog.java 3.dilog_my.xml

MainActivity


protected void onCreate(final Bundle savedInstanceState){
  ...
  AppCompatDialogFragment dialog = new MyDialog();
  dialog.show(getSupportFragmentManager(),null);
}

MyDialog.java



public class MyDialog extends AppCompatDialogFragment{
   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState){
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      LayoutInflater inflater     = requireActivity().getLayoutInflater();
      //                            getActivity().getLayoutInflater();But I got it
      builder.setView(inflater.inflate(R.layout.dialog_my,null))
             .setPositiveButton("OK",new DialogInterface.OnClickListener(){
                 @Override 
                 public void onClick(DialogInterface dialogInterface,int i){
                        //Processing when the OK button is pressed
                 }
             }).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
                 @Override 
                 public void onClick(DialogInterface dialogInterface,int i){
                        //Processing when the CANCEL button is pressed
                 }
             });
             return builder.create();
      }
}

dialog_my.xml


//Inflatable layout
//Try placing two EditText

<?xml version="1.0" encodeing="utf-8" ?>
<LinearLayout xmls:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   
   <EditText
       android:id="@+id/edit1"
       android:inputType="numberPassword"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

   <EditText
       android:id="@+id/edit2"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</LinearLayout>

Do not use Layout.xml (Do not use App / res / layout / --- .xml file)

Constitution MainActivity.java MyDialog.java

MainActivity


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyDialog dialog = new MyDialog();
    dialog.show(getSupportFragmentManager(),null);
}

MyDialog


public class MyDialog extends AppCompatDialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        //Generate Builder to set Dialog attributes and so on
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LinearLayout linearLayout = new LinearLayout(getActivity());
            linearLayout.setOrientation(LinearLayout.VERTICAL);

        //TextView generated in the layout
        TextView textView = new TextView(getActivity());
            textView.setText("TEXT");
            textView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
            linearLayout.addView(textView);

        //Generate EditText to generate for layout
        EditText editText = new EditText(getActivity());
            editText.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
            linearLayout.addView(editText);

        builder
                .setView(linearLayout)
                .setTitle("sample")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //OK button press process
                    }
                }).setNegativeButton("CANCELL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //CANCEL button press processing
                    }
                });

        this.setCancelable(false);
        return builder.create();
    }
}

Recommended Posts

Programming from 51 years old Note: Show multiple views in android dialog / with or without xml
Programming from 51 years old Note: Checking Android network connection
Programming from 51 years old Note --reference
Programming from 51 years old memorandum android Timer
Programming from 51 years old Note Service --reference
Programming from 51 years old Note Thread summary
(Currently 52) programming from 51 years old Note 3 lines Move focus to android button
Programming from 51 years old Android memo Activity Manager memo
Programming from 51 years old (currently 52) Note: Add jar with VScode Class.forName java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Drive
Programming from 51 years old (currently 52) Note Path and File
Programming from 51 years old Note AsyncTask --reference [copying sutras]
Programming from 51 years old Note: Do not start Android Sevice more than once Service start confirmation
Programming from 51 years old Make FusedLocationProviderClient resident in Foreground Service
Programming from 51 years old Note Android Asynchronous communication callback Processing after AsyncTask ends Further generalization / standardization
Programming from 51 years old Note: Background execution restrictions OverView [copying sutras]
Programming memorandum from 51 years old (currently 52) java.awt javax.swing in VScode // Java 11 or more recent is required to run
Programming from 51 years old [copying sutras] Intent IntentFilter-reference
Programming from 51 years old (currently 52) memorandum VScode javadoc
(Currently 52) programming from 51 years old Note FileOutputStream android.content.Context.openFileOutput (java.lang.String, int)'on a null object reference