conquistador
conquistador

Reputation: 693

Remove spaces on the top and the bottom of the items in AlertDialog

I have an AlertDialog with items provided by ArrayAdapter. When I show the AlertDialog, there are spaces on top and bottom of the items. I want to remove them completely. Sure, changing the color of AlertDialog background to match the item's background would do the trick but I want to remove it completely. This is the said spaces:

enter image description here

This is the style for AlertDialog: (colorPrimaryLight is the spaces' color)

<style name="style_alert_dialog" parent="Theme.AppCompat.Dialog.Alert">
        <item name="android:padding">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:background">@color/colorPrimaryLight</item>
</style>

Layout used for ArrayAdapter (template_alert_dialog_item):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/colorTextWhite"
    android:background="@color/colorPrimaryNormal"
    android:textSize="15sp"
    android:padding="10dp">
</TextView>

Code I used:

ArrayAdapter<String> adapter = new ArrayAdapter<>(ReviewMeTooMainPage.this, R.layout.template_alert_dialog_item, 
                        getResources().getStringArray(R.array.menu_items));
                AlertDialog.Builder b = new Builder(ReviewMeTooMainPage.this, R.style.style_alert_dialog);
                View inflatedTemplate = getLayoutInflater().inflate(R.layout.template_textview,(
                        ViewGroup) findViewById(R.id.main_page_display),false);
                TextView textView = inflatedTemplate.findViewById(R.id.template_title);
                b.setCustomTitle(textView);

                b.setAdapter(adapter, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                b.show();

I tried to use:

AlertDialog dialog = b.show();
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

but no prevail.

How can I remove those borders?

Upvotes: 1

Views: 952

Answers (2)

aminography
aminography

Reputation: 22832

If you use a custom layout, this padding will disappeared. It's because of using setAdapter. Because it uses default ListView in AlertDialog builder. So, you can put a ListView in content layout and set it to the AlertDialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View viewInflated = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_people_info, (ViewGroup) mRootView, false);

ListView personListView = viewInflated.findViewById(R.id.people_listView);
ListAdapter listAdapter = new PersonAdapter(getActivity(), R.layout.person_lsit_item, personsInfo);
personListView.setAdapter(listAdapter);

builder.setView(viewInflated);
builder.setNegativeButton(R.string.action_close, (dialog, which) -> dialog.cancel());

AlertDialog dialog = builder.create();
dialog.show();

Upvotes: 1

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Removing:

android:padding="10dp"

In the TextView would probably help since the padding is in the content (TextView) in your case.

If it didn't help, try removing this:

<item name="android:padding">0dp</item>

Upvotes: 0

Related Questions