Need help creating a custom layout for working with Android AlertDialog

I successfully created a working AlertDialog for my Android application:

public class MyClass extends DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ArrayList selectedItems = new ArrayList(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.pick_toppings) builder.setMultiChoiceItems(R.array.my_array, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { selectedItems.add(which); } else if (selectedItems.contains(which)) { selectedItems.remove(Integer.valueOf(which)); } } }); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // do stuff here ... } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // do stuff here ... } }); return builder.create(); } } 

This MultiChoiceItems list is maintained by an array in /res/values/array.xml

 <resources> <array name="my_array"> <item>item 01</item> <item>item 02</item> <item>item 03</item> <item>item 04</item> <item>item 05</item> </array> </resources> 

In my activity, I call AlertDialog as follows:

 MyClass myClass = new MyClass(); myClass.show(getSupportFragmentManager(), "My Dialog"); 

Now I want to use a custom layout with AlertDialog so that I can do things like line rotation, custom buttons, and add an EditText so that I have the β€œother” option with the ability to fill in the β€œother” one.

After doing some Google search, it seems to me that I need to create a new layout and set the AlertDialog view to this layout. So, I created a layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="other" android:textSize="18sp"/> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> 

Then I added this to my DialogFragment class:

 LayoutInflater layoutInflater = getActivity().getLayoutInflater(); View view = layoutInflater.inflate(R.layout.my_new_layout, null); 

then

 builder.setView(view); 

As you can guess, this did not work. New CheckBox and EditText were inserted after my other flags, which were filled from my array, but look awful, and I don't see any control over the appearance of the flags created from the array.

As I said, I would like to add this new combination of CheckBox / EditText, and also be able to customize the look of the entire AlertDialog.

I really want to use the array from /res/values/array.xml , so I don’t need to hard code the new option if I want to add new elements to the list.

Is this what I want to make possible? If so, some tips will be great.

thanks

This is what I would like my AlertDialog to look like this:

+6
source share
3 answers

Well, I finally figured it out myself. Here is my resolution:

New layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="right"> </LinearLayout> 

New class:

 public class MyClass extends DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { String[] theOptions = getResources().getStringArray(R.array.options); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.pick_toppings) LayoutInflater layoutInflater = getActivity().getLayoutInflater(); LinearLayout view = (LinearLayout) layoutInflater.inflate(R.layout.my_layout, null); for(String option : theOptions){ CheckBox checkbox = new CheckBox(getContext()); checkbox.setText(option); view.addView(checkbox); } LinearLayout otherLinearLayout = new LinearLayout(getContext()); otherLinearLayout.setOrientation(LinearLayout.HORIZONTAL); otherLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); otherLinearLayout.setId(R.id.otherLinearLayout); CheckBox otherCheckBox = new CheckBox(getContext()); otherCheckBox.setText("other"); otherCheckBox.setId(R.id.otherCheckBox); EditText otherEditText = new EditText(getContext()); otherEditText.setId(R.id.otherEditText); otherLinearLayout.addView(otherCheckBox); otherLinearLayout.addView(otherEditText); view.addView(otherLinearLayout); builder.setView(view); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // do stuff here ... } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // do stuff here ... } }); return builder.create(); } } 
0
source

You can do something like this, get an array using

 String[] myarray = getResources().getStringArray(R.array.testArray); 

and create new flag objects using each element of the array and set it to an inflated view

 LinearLayout layout2 = new LinearLayout(context); layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

and scroll through the list of arrays

 layout2.addView(new Checkbox(context)); 

and finally, add the parent linear layout to the inflated view inflatedView.addView(layout2);

0
source

Create a custom dialog fragment and add all the elements and checkboxes inside the new layout defined programmatically dynamically. Write the code for checkItemSelectedListener in your code.

0
source

Source: https://habr.com/ru/post/1011723/


All Articles