Java Question Spinner Text Size...again

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Having an issue with Spinner Text size again. I was hoping to be able to use it in my library this time, but the B4A Spinner just isn't sizing text right. If I tell it to wrap content for height and it has a large text size it just crops it and always draws the view at a set height.

When messing with straight android XML layouts I don't remember having this issue...although I don't remember even being able to change the text size either. Is there something with the additions of being able to change text size by code causing this, or some type of layout issue where the internal layout isn't wrapping to content?

I had tried making my own adapter and setDropDownViewResource, etc before, but it Force Closed when making a selection and I'm guessing when adding items later it would change all the stuff back another way anyway.

What is the adapter type and layouts used by the B4A Spinner Wrapper, and are there any other methods in it that would be messed up if I change them?

I really want to get this to size right this time and not have to go to using a label or button.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4A spinner uses a custom adapter.

This code creates the items:
B4X:
@Override
      public View getDropDownView(final int position, View convertView, final ViewGroup parent) {
         if (convertView == null) {
            convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
            ((TextView)convertView).setTextSize(textSize);
            if (textColor != Color.TRANSPARENT)
               ((TextView)convertView).setTextColor(textColor);
         }
         TextView tv = (TextView)convertView;
         Object o = items.get(position);
         if (o instanceof CharSequence)
            tv.setText((CharSequence)o);
         else
            tv.setText(String.valueOf(o));
         return convertView;
      }
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Size 50 for text gives me this:
 

Attachments

  • SpinnerIssue.png
    SpinnerIssue.png
    20.9 KB · Views: 491

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Ok, I fixed part of it with this in my library (Still too much padding or something and bottom of descenders get cropped):

B4X:
public SpinnerWrapper AddCombobox(String ViewName, String EventPrefix, String Prompt, float TextSize, int TextColor, List Items, int BackgroundResourceID){
   SpinnerWrapper b4aSpinner = new SpinnerWrapper();
   b4aSpinner.Initialize(ba, EventPrefix);
   Spinner NewView = b4aSpinner.getObject();
   if (BackgroundResourceID != 0) NewView.setBackgroundResource(BackgroundResourceID);
   b4aSpinner.setPrompt(Prompt);
   b4aSpinner.setTextSize(TextSize);
   b4aSpinner.setTextColor(TextColor);
   if (Height == getSize_WrapContent()) {
      TextView tempView= new TextView(ba.context);
      tempView.setTextSize(TextSize);
      tempView.setSingleLine();
      ComboHeight= tempView.getLineHeight() + NewView.getPaddingTop() + NewView.getPaddingBottom();
   }
   if (Items.IsInitialized()) b4aSpinner.AddAll(Items);

   AddView(NewView, ViewName);
   return b4aSpinner;
}

I check for ComboHeight > 0 in AddView and have it override the Height in the library to use it instead for the height. Tested from 10-100 Text Size. Greater than 50 starts to mess with the popup list though.

Looking at the Layouts:

simple_spinner_item- Just a Text View with width of Parent and Wrap Content for Height. It is set to single line and marquee. Whatever the spinnerItemStyle does appears to add way too much top padding that even setting your own 9 Patch doesn't remove. No text size or anything specified though, so I guess that is why the Spinner doesn't size right. I guess B4A could do something similar to what I did, or if possible create a new XML Layout or override the Android one to have a size...but I didn't see a way to do that by code or with the inflator like your method above does for the dropdown item.

simple_spinner_dropdown_item- Is a Checked Textview configured almost the same way, but with a layout height of dropdownListPreferredItemHeight which I'm guessing is limiting the height and not allowing it to expand. This could possibly be fixed in the above code by setting it to Wrap to Contents. Some larger devices may need larger text than 50 and at least then it would be consistent.
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
This ended up getting fixed by setting Top and Bottom Padding to 0???

B4X:
public SpinnerWrapper AddCombobox(String ViewName, String EventPrefix, String Prompt, float TextSize, int TextColor, List Items, int BackgroundResourceID){
   SpinnerWrapper b4aSpinner = new SpinnerWrapper();
   b4aSpinner.Initialize(ba, EventPrefix);
   Spinner NewView = b4aSpinner.getObject();
   if (BackgroundResourceID != 0) NewView.setBackgroundResource(BackgroundResourceID);
   NewView.setPadding(NewView.getPaddingLeft(), 0, NewView.getPaddingRight(), 0);
   b4aSpinner.setPrompt(Prompt);
   b4aSpinner.setTextSize(TextSize);
   b4aSpinner.setTextColor(TextColor);
   if (Items.IsInitialized()) b4aSpinner.AddAll(Items);

   AddView(NewView, ViewName);
   return b4aSpinner;
}

It now draws properly when height is set to Wrap Content.

So, what does the getView override in the Spinner Wrapper look like? Is it or some other area adding padding in making it double what it needs to be or something? It is just the Top and Bottom padding that is off too, the sides are fine.

Edit: Spoke too soon. It seems to reset something and make the Spinner a set height which was big enough for my text, but larger text now draws outside the view bounds like it is a view drawn on a view???
 
Last edited:
Top