interested in the question of how to divide the array into two rows in the final mapping? At the moment the array is displayed in one row.

final String[] mCitiesName ={"Π’Π°ΡΡŒΠΊΠ°", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ"}; 

For example, there is a menu, I threw an array into it, of course it displays 1 name in a column, you need to make 2 columns from this 1 column so that the menu does not scroll. An illustrative example, the image https://fastpic.co/image/hPZHnr , where Button is an array, I would like to see an array in about this format

  • What does "two rows in the final display" mean? Write the approximate result you want to get. - Bakhuss
  • For example, there is a menu, I threw an array into it, of course it displays 1 name in a column, you need to make 2 columns from this 1 column so that the menu does not scroll. A vivid example, an image of fastpic.co/image/hPZHnr , where Button is an array, I would like to see an array in about this format - Inna Ahtina
  • @InnaAhtina Now it is clear that your rows are our speakers. OK. Now I’ll clarify what your menu means (List, Action, Popup ...). It is better if you give a link to the material that you are studying. By the way, two columns are already a table. [сities - cities, kitties - kitties] - oshatrk
  • @oshatrk AlertDialog - Inna Ahtina

2 answers 2

You can create your own .xml layout and assign it using builder.setView() . In the layout, create two ListView that fill with the halves of the original array of names through adapters.

(Directly using builder.setItems() will not succeed, as this method creates its own ListView , and adds it to the layout itself).

You get this result:

enter image description here

// activity_alert_dialog_custom_view.xml :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView2" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> 

// MyDialogFragment.java :

 import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MyDialogFragment extends DialogFragment { private String[] catNames = {"Π’Π°ΡΡŒΠΊΠ°1", "Π Ρ‹ΠΆΠΈΠΊ2", "ΠœΡƒΡ€Π·ΠΈΠΊ3", "Π Ρ‹ΠΆΠΈΠΊ4", "ΠœΡƒΡ€Π·ΠΈΠΊ5", "Π Ρ‹ΠΆΠΈΠΊ6", "ΠœΡƒΡ€Π·ΠΈΠΊ7", "Π Ρ‹ΠΆΠΈΠΊ8", "ΠœΡƒΡ€Π·ΠΈΠΊ9", "Π Ρ‹ΠΆΠΈΠΊ10", "ΠœΡƒΡ€Π·ΠΈΠΊ112"}; private AlertDialog alertDialog; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΊΠΎΡ‚Π°"); builder.setCancelable(true); final View customView = getActivity().getLayoutInflater().inflate( R.layout.activity_alert_dialog_custom_view, null); builder.setView(customView); List<String> items1 = new ArrayList<>(); List<String> items2 = new ArrayList<>(); if (catNames != null) { for (int i = 0; i < catNames.length; i += 2) { if (i + 2 <= catNames.length) { // Π”Π²Π° ΠΊΠΎΡ‚Π° Π² строкС: items1.add(catNames[i]); items2.add(catNames[i + 1]); } else { // ΠžΡΡ‚Π°Π»ΡΡ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΡ‚: items1.add(catNames[i]); } } } ListView listView1 = customView.findViewById(R.id.listView1); ListView listView2 = customView.findViewById(R.id.listView2); ArrayAdapter<String> adapter1 = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, items1); ArrayAdapter<String> adapter2 = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, // здСсь Ρ‚ΠΎΠΆΠ΅ ..._1 ! items2); listView1.setAdapter(adapter1); listView2.setAdapter(adapter2); AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { alertDialog.cancel(); Toast.makeText(getActivity(), "Π’Ρ‹Π±Ρ€Π°Π½Π½Ρ‹ΠΉ ΠΊΠΎΡ‚: " + ((ArrayAdapter) ((ListView) parent).getAdapter()).getItem(position), Toast.LENGTH_SHORT).show(); } }; listView1.setOnItemClickListener(onItemClickListener); listView2.setOnItemClickListener(onItemClickListener); alertDialog = builder.create(); return alertDialog; } } 

// MainActivity.java :

 import android.app.AlertDialog; import android.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void button_onClick(View view) { MyDialogFragment myDialogFragment = new MyDialogFragment(); FragmentManager manager = getFragmentManager(); myDialogFragment.show(manager, "dialog"); } } 

// activity_main.xml :

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:onClick="button_onClick" android:text="Button" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> 

Links: While installing Android Studio and Intel HAXM, managed to read an introduction to programming on Android ( lesson 1 , lesson 2 ) and about AlertList ( lesson 30 ), and google Android Custom Layout AlertDialog Example , Android Layout: width half of parent , Dynamically can I set onClickListener on ArrayAdapter? .

And this is not JavaFX at all ...

  • Thank you, everything works fine and everything is perfectly explained! - Inna Ahtina

maybe the TC meant type it

 Π’Π°ΡΡŒΠΊΠ° Π Ρ‹ΠΆΠΈΠΊ ΠœΡƒΡ€Π·ΠΈΠΊ Π Ρ‹ΠΆΠΈΠΊ ΠœΡƒΡ€Π·ΠΈΠΊ Π Ρ‹ΠΆΠΈΠΊ ΠœΡƒΡ€Π·ΠΈΠΊ Π Ρ‹ΠΆΠΈΠΊ ΠœΡƒΡ€Π·ΠΈΠΊ Π Ρ‹ΠΆΠΈΠΊ ΠœΡƒΡ€Π·ΠΈΠΊ null 

If so, then one of the solutions might be:

 final String[] mCitiesName = {"Π’Π°ΡΡŒΠΊΠ°", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ", "Π Ρ‹ΠΆΠΈΠΊ", "ΠœΡƒΡ€Π·ΠΈΠΊ"}; int count = mCitiesName.length; int m = (int) Math.round(count / 2d); String[][] mCName = new String[2][m]; int k = 0; for (int i = 0; i < mCitiesName.length; i++) { if (i < m) { mCName[0][i] = mCitiesName[i]; } else { mCName[1][k++] = mCitiesName[i]; } } for (int i = 0; i < mCName.length; i++) { for (int j = 0; j < mCName[i].length; j++) { System.out.print(mCName[i][j] +" "); } System.out.println(); }