How to remove TextView top margin?
using android:includeFontPadding="false"
helped me a lot in a similar situation.
AutoCompleteTextView - How to remove margin from top and bottom?
UPDATE
All the credits and efforts back to @MikeM. from the comments. Many thanks to his efforts.
Cause
The padding is coming from the TextInputLayout
itself where its DropdownMenuEndIconDelegate
sets some MaterialShapeDrawables
as the dropdown's background. The value of the vertical padding is 8dp which taken from this resource dimen.
Solution 1:
Override this resource dimen to be 0dp:
<dimen name="mtrl_exposed_dropdown_menu_popup_vertical_padding" tools:override="true">0dp</dimen>
Solution 2:
The previous solution will be applied to all AutoCompleteTextView
's, but if you want to do this for a certain ones: change the default dropDown background drawable, for instance:
autoCompleteTextView.setDropDownBackgroundDrawable(new ColorDrawable(Color.WHITE));
With reflections (Not recommended as it's anti-pattern)
The drop down popup of the AutoCompleteTextView
can be accessed internally by mPopup
field. To get it with reflections:
// Java:
public static ListPopupWindow getPopup(AutoCompleteTextView autoCompleteTextView) {
try {
Field field = AutoCompleteTextView.class.getDeclaredField("mPopup");
field.setAccessible(true);
return (ListPopupWindow) field.get(autoCompleteTextView);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
// Kotlin:
fun AutoCompleteTextView.getPopup(): ListPopupWindow? {
try {
val field = AutoCompleteTextView::class.java.getDeclaredField("mPopup")
field.isAccessible = true
return field.get(this) as ListPopupWindow
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
return null
}
Then remove the padding by setting it to the parent view of the internal ListView
:
// Java:
autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListPopupWindow popup = getPopup(autoCompleteTextView);
if (popup != null) {
ListView listView = popup.getListView();
if (listView!= null)
((View) listView.getParent()).setPadding(0, 0, 0, 0);
}
}
});
// Kotlin
autoCompleteTextView.setOnClickListener {
val popUp = autoCompleteTextView2.getPopup()?.listView?.parent
if (popUp != null)
(popUp as View).setPadding(0, 0, 0, 0)
}
This is done in OnClickListener
because the nullability checks won't match as the list is formed when the user hits it.
Related Topics
Navigate from One Fragment to Another on Click of a Button
How to Make Push Notification With Specific Sound
Send Post Request With Json Data Using Volley
How to Get the Value of a Listview Item Which Is Clicked in Android
Android Refresh a Fragment List from Its Parent Activity
How to Show List of String in Textview on Android
Httpmessagenotreadableexception: Could Not Read Json: Unrecognized Field Using Spring for Android
Cannot Resolve Symbol Appcompatactivity - Support V7 Libraries Aren't Recognized
Disable Ssl as a Protocol in Httpsurlconnection
Sharing Link on Whatsapp from Mobile Website (Not Application) For Android
Imageview of Png Doesn't Appear (Maybe It's Too Big)
Get City Name and Postal Code from Google Place API on Android
How to Scroll Scrollview to Bottom Programmatically in Android
Remove Items from Listview With a Custom Adapter
Onclicklistener Does Not Work in Fragment
How to Reduce the Gap Between Tab Layout Text and Its Indicator
How to Display a Loading Spinner While Getting Data from Firestore
Android Studio Inline Compiler Showing Red Errors, But Compilation With Gradle Works Fine