0

I'm currently having a problem when I try to use the AlertDialog.setView(View v) function.

When the dialog is rendered, no matter the theme used, it generates a border around the layout, just as if another layout was encapsulating it.

Using builder.show() alone, builder.create() alone or both together yields the same results.

Am I missing something somewhere, or is it a known bug ? Thanks in advance if you can help.

dialog_status.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <Spinner
        android:id="@+id/spinnerStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/editTextStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Status"
        android:inputType="textMultiLine"/>

    <CheckBox
        android:id="@+id/checkBoxStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="@string/save_status"/>

</LinearLayout>

Code inside Activity :

private void showStatusDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
    //AlertDialog.Builder builder = new AlertDialog.Builder(this); (Yields the same results, just another theme)
    View root = View.inflate(this, R.layout.dialog_status, null);

    final EditText input = (EditText) root.findViewById(R.id.editTextStatus);
    final Spinner spinner = (Spinner) root.findViewById(R.id.spinnerStatus);
    final CheckBox checkBox = (CheckBox) root.findViewById(R.id.checkBoxStatus);
    final Preferences p = new Preferences(FChatActivity.this);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            input.setText(p.getDefaultStatusMessage(FCharacter.Status.getIdentifiers().get(i)));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
    adapter.addAll(FCharacter.Status.getLabels());
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    builder.setView(root)
            .setMessage("Update your status")
            .setTitle("Status")
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    setStatus(FCharacter.Status.getIdentifiers().get(spinner.getSelectedItemPosition()),input.getText().toString(), checkBox.isChecked());
                    dialogInterface.dismiss();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });

    builder.create();
    builder.show();
}

Screenshots :

Theme_Holo_Dialog Theme_Diialog

Elcan
  • 814
  • 13
  • 27
  • I think you must change the theme you're using here `AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);` instead of using `android.R.style.Theme_Holo_Dialog` – dotGitignore Oct 11 '17 at 00:58
  • Changing the theme doesn't change anything, I'm adding another screenshot to show you – Elcan Oct 11 '17 at 01:03
  • You can try like this . `String info = cityData.getPointerList().get(position).toString(); AlertDialog alertDialog = new AlertDialog.Builder(CityActivity.this).create(); alertDialog.show(); Window window = alertDialog.getWindow(); window.setContentView(R.layout.dialog_main_info); TextView tv_title = (TextView) window.findViewById(R.id.tv_dialog_title); tv_title.setText("detail info"); TextView tv_message = (TextView) window.findViewById(R.id.tv_dialog_message); tv_message.setText(info); ` – KeLiuyue Oct 11 '17 at 01:06
  • Using setContentView() on the generated AlertDialog doesn't work, as i need to pass a View, and not a Layout ID – Elcan Oct 11 '17 at 01:10

2 Answers2

1

Okay, I found the solution on another completely unrelated question (How to show a Holo (dark) AlertDialog in a themed activity?) :

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
Elcan
  • 814
  • 13
  • 27
0

Create a new style in styles.xml like

<style name="Dialog.NoActionBar" parent="Theme.AppCompat.Dialog">
    <item name="android:windowBackground">@color/colorPrimary</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

Then use the style in your AlertDialog like.

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Dialog_NoActionBar);

hope this can help.

dotGitignore
  • 1,597
  • 2
  • 15
  • 34
  • `android:windowDrawsSystemBarBackgrounds` and `android:windowTranslucentStatus` require API 19, and my minimal target is 16, and I need to target that low – Elcan Oct 11 '17 at 01:17
  • Ohh, I see I think this is what you need https://stackoverflow.com/a/13763132/5693082 Just remove the `android:windowTranslucentStatus` and `android:windowDrawsSystemBarBackgrounds` – dotGitignore Oct 11 '17 at 01:21
  • If you see my answer, I already found a far simpler solution that seems to be the right way, but thanks for your help – Elcan Oct 11 '17 at 01:33