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.
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>
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