Currently on an Android 4.3 device. I'm using the excellent answer provided here I was able to reduce the font size of an AlertDialog
's individual items:
<style name="AlertDialogTheme" parent="android:Theme.Dialog">
<item name="android:textSize">13sp</item>
</style>
The dialog itself is built with:
ContextThemeWrapper cw = new ContextThemeWrapper( this, R.style.AlertDialogTheme );
AlertDialog.Builder builder = new AlertDialog.Builder(cw);
However, the problem is that the individual height of the items still stays the same — in fact, there is a fifth element that is just shown when you scroll in the dialog:
I tried setting the dividerHeight
but that just increased the spacing. I also set the layout_margin
property to 0dp
without success.
What's the correct style definition for this dialog to reduce the space between the labels?
Note that I do not want to build the whole dialog using a view again. I'd like to keep the existing AlertDialog.
Here is a tricky solution – override the getView
method for ListView
in AlertDialog
and change TextView
style attributes programmatically:
ContextThemeWrapper cw = new ContextThemeWrapper(MainActivity.this, R.style.AlertDialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(cw);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
ListView listView = ((AlertDialog) dialogInterface).getListView();
final ListAdapter originalAdapter = listView.getAdapter();
listView.setAdapter(new ListAdapter() {
@Override
public int getCount() {
return originalAdapter.getCount();
}
@Override
public Object getItem(int id) {
return originalAdapter.getItem(id);
}
@Override
public long getItemId(int id) {
return originalAdapter.getItemId(id);
}
@Override
public int getItemViewType(int id) {
return originalAdapter.getItemViewType(id);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = originalAdapter.getView(position, convertView, parent);
TextView textView = (TextView) view;
//textView.setTextSize(16); set text size programmatically if needed
textView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 32 /* this is item height */));
return view;
}
@Override
public int getViewTypeCount() {
return originalAdapter.getViewTypeCount();
}
@Override
public boolean hasStableIds() {
return originalAdapter.hasStableIds();
}
@Override
public boolean isEmpty() {
return originalAdapter.isEmpty();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
originalAdapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
originalAdapter.unregisterDataSetObserver(observer);
}
@Override
public boolean areAllItemsEnabled() {
return originalAdapter.areAllItemsEnabled();
}
@Override
public boolean isEnabled(int position) {
return originalAdapter.isEnabled(position);
}
});
}
});
dialog.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With