Android remove padding from Spinner's text - android

How can I remove the padding from around the Spinner's text?
My layout:
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#ff00ff"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_marginStart="8dp"
android:background="#0000ff"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Activity does not do anything special just put some content into the Spinner. Please let me know if you need it, I'll attach it then.
I'd like to make the Spinner's text to be exactly aligned to Label while the purple box also aligned to blue one. I tried to set padding to 0dp, but it does not change anything. Also tried removing the background, but it also does not change this padding.
And of course if I mess with margins, I can make the texts to be in line, but then the boxes will not stay where they are now.
How shall I do this?
Using a custom overridden View seems to work but why are padding on the top and bottom?
I tried even changing all padding to 0, but still.
Here is my actual adapter:
public class MyAdapter extends ArrayAdapter {
public MyAdapter(#NonNull Context context, int resource, String[] params) {
super(context, resource, params);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setPadding(0, 0, 0, 0);
return view;
}
}

You can try adding this to the adapter.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
return view;
}

Related

Toolbar Spinner Dropdown inflating issue

I am passing my custom layout in constructor of ArrayAdapter<String>:
private static class ActionSpinnerAdapter extends ArrayAdapter<String> {
public ActionSpinnerAdapter(final Context context) {
super(context,
R.layout.action_spinner_text, //This is my own layout
//R.id.action_text,
Lists.newArrayList(ACTION_REPLY, ACTION_REPLY_ALL, ACTION_FORWARD));
action_spinner_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="8dip"
android:paddingStart="8dp"
android:paddingTop="10dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:text="#string/reply_all_action"
android:textSize="18sp" />
And these are my getView() and getDropDownView() methods:
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View result = super.getDropDownView(position, convertView, parent);
result.setBackgroundColor(mContext.getResources().getColor(R.color.white));
TextView textView = ((TextView) result.findViewById(R.id.action_text));
textView.setTextColor(mContext.getResources().getColor(R.color.dark_grey));
textView.setText(getDisplayValue(position));
return result;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View result = super.getView(position, convertView, parent);
TextView textView = ((TextView) result.findViewById(R.id.action_text));
textView.setTextColor(mContext.getResources().getColor(R.color.white));
textView.setText(getDisplayValue(position));
return result;
}
But When I click on DropDown from Toolbar it shows text values with black background on right side.
What went wrong?
I had exactly the same problem, and it seems like there's a bug in Pre-Lollipop devices when you specify a size in the dropdown view that is not "match_parent" on "android:layout_width", basically all you need to do is
this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="8dip"
android:paddingStart="8dp"
android:paddingTop="10dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:text="#string/reply_all_action"
android:textSize="18sp" />
Hope it Helps!

default padding or margin for listview

Is there some sort of built-in default margin or padding for a ListView?
I have a simple LinearLayout with an ImageView and a ListView.
The entire ListView appears to have a slight margin or padding.
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/starbuzz_logo"
android:contentDescription="#string/starbuzz_logo"/>
<ListView
android:id="#+id/listView_drinks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/drinks" />
The screenshot is attached. I am not allowed to embed an image. screenshot
The padding or margin probably comes from the default layout for each list item. To get rid or change the size of the padding or margin, create a custom layout for the list items.
item_list_coffee.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
then override getView() in your adapter so that it uses your custom layout
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_list_coffee, parent, false);
}
final String string = getItem(position);
// find textview and set text to string
return convertView;
}

Android Spinner Initial Padding

I am creating an Android app that consists mainly of a form. This form needs to have EditText and Spinners side-by-side (for this I chose a LinearLayout). My final goal is to have the text in the Spinner align vertically with the text in the EditText; however, I also want to have padding on the options in the expanded Spinner. This padding messes up the Spinner alignment as it appears in the form.
Here is my code for a custom Spinner list item:
<TextView
android:id="#+id/transportation_spinner_item_textView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#android:color/black"
android:textSize="18dp"/>
Here is my code for the main form:
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/divider3"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:orientation="vertical">
<TextView
android:id="#+id/ip_minutes_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/ip_minutes"
android:layout_marginTop="4dp"
android:textSize="12sp"/>
<Spinner
android:id="#+id/ip_minutes_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:gravity="bottom"/>
</RelativeLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/ip_charge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:layout_gravity="bottom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:editable="false"
android:hint="#string/ip_charge"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
And here is an image of how it looks:
Image of alignment with above code
I have been Googling and trying to adjust the padding/margins of different views for hours, so if anyone has any suggestions, that would be great. Thanks!
I'm not sure I fully understand, when you say,
This padding messes up the Spinner alignment as it appears in the
form.
Do you mean the DropDownView? that shows when the Spinner is clicked?
If so, simply create a separate layout for the DropDownView.
In your ArrayAdapter you will have the following methods,
public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
public View getView(int pos, View cnvtView, ViewGroup prnt)
Just have them return two different Views, e.g
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomDropDownView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
and
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mySpinner = inflater.inflate(R.layout.custom_dropdown_spinner, parent, false);
TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
main_text.setText(spinnerValues[position]);
return mySpinner;
}

Items are not selectable in spinner (Custom Spinner Adapter)

I used custom spinner adapter for color spinner in android application. The drop down is working fine. But once I select a color(item) from spinner, it is not selectable. Also I do not need to show selected item as it is selected. I only want to identify the selected color without displaying it.
Below is code for my CustomSpinnerAdapter:
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView rowView=null;
if(convertView == null){
convertView=inflater.inflate(R.layout.spinner_layout, null);
}
rowView=(TextView) convertView.findViewById(R.id.spinnerColorview);
rowView.setBackgroundColor(Color.parseColor(itemList.get(position)));
return convertView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView rowView=null;
if(convertView == null){
convertView=inflater.inflate(R.layout.spinner_layout, null);
}
rowView=(TextView) convertView.findViewById(R.id.spinnerColorview);
rowView.setBackgroundColor(Color.parseColor(itemList.get(position)));
return convertView;
}
EDIT: MORE INFORMATION
My drop down list in spinner not selectable. When I clicked on the spinner it is displaying the list. But when I select one item from that list, nothing happen. I cannot identify selected item.
When I print the position inside getView(int position, View convertView, ViewGroup parent) method, it prints all item ids.
I only need to identify selected item and I do not need to display it at the top of spinner as it usually does. This is my spinner_layout.xml
<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="40dp"
android:clickable="true"
android:orientation="horizontal"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/spinnerColorview"
android:layout_width="200px"
android:layout_height="50px"
android:clickable="true"
android:gravity="center_vertical"
>
</TextView>
</LinearLayout>
I added style="?android:attr/spinnerItemStyle" to the textview and it works. Not sure that the best solutions but it's a start and a quick fix.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:id="#+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:ellipsize="end"
android:gravity="left|center"
android:singleLine="true"
android:text="Option One"
android:textColor="#color/Petrol"
android:textSize="#dimen/font_size_big"
android:textStyle="bold"/>

ExpandableListView - How to set divider only between parent elements

I want to put divider only between parent elements. When i set
android:divider="#drawable/divider" android creates divider between parent elements, but creates divider between child elements too. When i add android:childDivider="#color/transparent" android removes the divider between child elements, but the free space between them remains. Why?
I have tried to android:dividerHeight="0dp" but nothing happened.
At all i want to set divider between parent elements, but i do not want any divider or empty space between child elements.
any ideas how to do that??
In order to remove dividers just from the child views and not between the parents in the expandable list:
add android:childDivider="#00000000" in the ExapandableListView attributes in XML:
<ExpandableListView
android:id="#+id/elv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:childDivider="#00000000" />
Refer this page for more information
Give Divider color and its height (dividers will be visible when groups are collapsed)
<ExpandableListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/color_black"
android:dividerHeight="1dp"
android:groupIndicator="#null" />
The trick is to create an extra view for collapse group layout and last child layout.
collapse_group.xml:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"/>
<!-- this is the extra view-->
<View
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="#android:color/transparent"/>
</LinearLayout>
expanded_group.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"/>
<!-- no extra view-->
</LinearLayout>
And apply the same technique to the child view, for the last child view insert the extra view
What I did was to set the ExpandableListView's divider to 0 and then insert "divider" groups into it's ExpandableListAdapter:
// To get dividers between the groups we add 'divider views' as if they were
// themselves groups.
public int getGroupCount() {
// Actual groups are at even groupPositions, dividers at odd
return (this.data.size() * 2) - 1;
}
public long getGroupId(int groupPosition) {
if(groupPosition % 2 != 0) return R.id.divider;
else return R.id.group;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if(groupPosition % 2 != 0) {
View divider = convertView;
if(divider == null || divider.getId() != R.id.divider) {
divider = this.inflater.inflate(R.layout.divider, null);
}
return divider;
}
View group = convertView;
if(group == null || group.getId() != R.id.group) {
group = this.inflater.inflate(R.layout.group, null);
}
return group;
}
expListView.setDividerHeight(10);
expListView.setChildDivider(getResources().getDrawable(
android.R.color.white));
If you want to remove child divider only, One simple trick is You can create a drawable with same color as of your child item's background color. Then set it as child divider.
ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(YOUR CHILD ITEM BACKGROUND COLOR);
mExpListView.setChildDivider(sd1);
Or using xml:
android:childDivider="#color/child_bg_color"
If you want to have divider between parent element of the ExpandableListView (Header) and don't want to have divider between children and parent , which is obviously the most natural case, then you have to do following:
In your parent xml file add one View at the bottom of the layout with divider height and divider color that you need.
Example:
Do same in the child xml file. Add one View at the bottom of the layout with divider height and divider color that you need. Make sure divider color and height are same like in parent xml.
Into your ExpandableListView Adapter method
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
manage the state of the View divider like this:
Example:
if(isExpanded){
headerDividerView.setBackgroundColor(ContextCompat.getColor(context,R.color.darker_grey));
}else{
headerDividerView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
}
BTW : In my case R.color.darker_grey is background color of the parent
Remove from your ExpandableListView xml all dividers and dividers height. Make ExpandableListView to not have dividers and divider height.
You can use attrs like this :
android:groupIndicator="#null"
android:childIndicator="#null"
android:divider="#null"
android:dividerHeight="0dp"
I solved it by adding two views one to header and other to child item like shown below.
Header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rl_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/square_box"
android:padding="#dimen/padding_10">
<ImageView
android:id="#+id/expandableIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_downarrow" />
</RelativeLayout>
<View
android:id="#+id/view_hidden"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#+id/rl_header"
android:background="#android:color/transparent" />
</RelativeLayout>
child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rl_childView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/square_box"
android:padding="#dimen/padding_10">
<TextView
android:id="#+id/tv_startDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:paddingTop="#dimen/padding_5"
android:paddingBottom="#dimen/padding_5"
android:text="Start Date \nSep. 23, 2019"
android:textSize="#dimen/text_16" />
<ImageView
android:id="#+id/iv_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/right_arrow" />
</RelativeLayout>
<View
android:id="#+id/view_hidden"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#+id/rl_childView"
android:background="#android:color/transparent" />
</RelativeLayout>
Now add this code to your Adapter class. The idea is to show/hide the View when group is expanded/collapsed, show on child last item.
#Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
String listCount = "(" + getChildrenCount(listPosition) + ")";
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_header, null);
}
View view_hidden = convertView.findViewById(R.id.view_hidden);
if (isExpanded) {
view_hidden.setVisibility(View.GONE);
} else {
view_hidden.setVisibility(View.VISIBLE);
}
return convertView;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Medication medication = (Medication) getChild(listPosition,
expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_item, null);
}
View view_hidden = convertView.findViewById(R.id.view_hidden);
if (isLastChild) {
view_hidden.setVisibility(View.VISIBLE);
} else {
view_hidden.setVisibility(View.GONE);
}
return convertView;
}
set like this android:divider="#null" no divider for parents android:divider="2dp" set divider for parents
<ExpandableListView
android:id="#+id/expandable_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:divider="2dp"
android:groupIndicator="#drawable/group_indicator" />

Categories

Resources