I fill in the Linear Layout in the ScrollView buttons. I generate Id for each button and assign Tag to it. Depending on which button is pressed by the user, certain text will be displayed in the AlertDialog. Text is already defined in an array of strings. The buttons are stored in an array of buttons and each has a generated id and Tag. This is the button click handler in the activity from which you want to call AlertDialog:

View.OnClickListener oclBtn1= new View.OnClickListener() { @Override public void onClick(View v) { } 

};

And this is the code of the method in the activity of invoking the dialog, in which I want to determine the content of the 3-x TextView depending on the pressed button in the Activity:

 public void createAndShowDialog(String title, String message1, String message2) { AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog alert = builder.create(); alert.show(); 

}

The question is how to implement a call to this method in oclBtn1, assign your xml markup to the created dialog and attach the input data to the method (strings) from the already created arrays, to the TextView in the xml markup?

I will be glad to any answers, since I am still quite new to Android Studio. If you see that something is implemented is not very rational, then offer your options. The array of buttons is created because in each cell there are 4 buttons, which through lnInflater in a cycle get their id and tag, and processing in oclBtn1.

  • Why keep the buttons in an array? I understand correctly that you need to catch a button click and show a dialogue with the text? Does the text depend on the button pressed? - Eugene Zaychenko

2 answers 2

If you are completely new, I recommend immediately begin to do everything "correctly" through DialogFragment .

The class looks like this:

 public class DialogCustom extends DialogFragment { public static final String TYPE = "type"; public static final String DATA = "data"; //идентификаторы разных диалогов public static final int EXIT = 1; public static final int HELP = 2; // интерфэйс для обратной связи из диалога private DialogCallback dialogCallback; public interface DialogCallback { void dialogCallback(int result, String resultString); //любые ваши переменные } //Два разных метода для приёма разных параметров (по потребности можно больше) public static DialogCustom newInstance(int type) { Bundle args = new Bundle(); args.putInt(TYPE, type); DialogCustom fragment = new DialogCustom(); fragment.setArguments(args); return fragment; } public static DialogCustom newInstance(int type, String data) { Bundle args = new Bundle(); args.putInt(TYPE, type); args.putInt(DATA, data); DialogCustom fragment = new DialogCustom(); fragment.setArguments(args); return fragment; } //выбор нужного диалога @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { switch (getArguments().getInt(TYPE)) { case EXIT: return exitDialog(); case HELP: return helpDialog(); } return super.onCreateDialog(savedInstanceState); } //пример диалога private Dialog exitDialog() { AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); final View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_exit, null, false); adb.setView(v); v.findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //ваш код } }); ((TextView) v.findViewById(R.id.message)).setText("ваш текст"); Dialog dialog = adb.create(); dialog.setCanceledOnTouchOutside(true); dialog.setCancelable(true); return dialog; } //---Инциализация колбэка-— @TargetApi(23) @Override public void onAttach(Context context) { super.onAttach(context); try { dialogCallback = (DialogCallback) context; } catch (ClassCastException e) { e.printStackTrace(); } } "deprecation" @Override public void onAttach(Activity activity) { super.onAttach(activity); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { try { dialogCallback = (DialogCallback) activity; } catch (ClassCastException e) { e.printStackTrace(); } } } @Override public void onDetach() { super.onDetach(); if (dialogCallback != null) dialogCallback = null; } } 

Using. In the right place call

 DialogCustom.newInstance(DialogCustom.HELP,"help") .show(getSupportFragmentManager(),"dialog"); 

If we need to return the results from the dialog, we use the interface in the right place:

 dialogCallback.dialogCallback(1,"success"); 

    I can tell how Dialog is created with its markup:

      public class YourDialog extends Dialog { private Button buttonClose; private TextView textView1; private TextView textView2; public YourDialog(@NonNull Context context) { super(context); setContentView(R.layout.your_dialog_layout); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); buttonClose = findViewById(R.id.buttonClose); textView1= findViewById(R.id.textView1); textView2 = findViewById(R.id.textView2); buttonClose.setOnClickListener(getOnClickListener()); } public void setTextForTextView1(String text){ textView1.setText(text); } private View.OnClickListener getOnClickListener() { return new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }; } }