2

I've come with a problem. I need to highlight the selected item within a Gallery. I've tried changing the appearance of the selected view through this method:

@Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)

The second parameter of that view is the current selected view, and I'm trying in this case, increase the size of the text. However this doesn't work, not even if I call invalidate in the selected item or in the entire Gallery.

This is the code I use to change the textview text size

TextView textview = (TextView)view;             
textview.setTextSize(50, TypedValue.COMPLEX_UNIT_SP);
textview.invalidate();

Do you know how to do this? Thanks

lblasa
  • 6,284
  • 4
  • 27
  • 29

2 Answers2

8

Your implementation works, but it does not return the text size to normal once the item becomes unselected - the text stays larger size.

This should fix it:

    private View lastview;

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

    if (lastview != null) lastview.setBackgroundColor(0xFFFFFFFF);
    lastview = arg1;
    arg1.setBackgroundColor(0xFFFF0000);

}

You can set text size instead of color, or do whatever you want to the style of it.

Li_W
  • 759
  • 1
  • 5
  • 15
0

Try StateListDrawable - looks apt for your situation.

UPDATE: You have reversed the parameters in setTextSize. It should be:

textview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
Rajath
  • 11,787
  • 7
  • 48
  • 62
  • Hi Rajath, yes it seems that I need to define a StateListDrawable. Similar to this other question that already has an answer http://stackoverflow.com/questions/3237566/text-gallery-on-android. Thanks for your help. – lblasa Apr 12 '11 at 09:33
  • I'm not sure about how to set the text bigger using selectors though. Because selector seem to work with drawables, and in my case I just want to increase the size of the text – lblasa Apr 12 '11 at 09:35
  • If your purpose is to only highlight the selected item, why would elect text size increase over using StateListDrawable? – Rajath Apr 12 '11 at 09:50
  • because the Gallery is of TextView items, and the only thing I want to do is increase the TextView size on selection. At the end the results should be similar to the macosx dock effect but with text – lblasa Apr 12 '11 at 09:53
  • Actually I would need to do other tricks with other galleries as well, as for example display the image title on selection – lblasa Apr 12 '11 at 09:58
  • Can you update your question with the relevant code on how you increase the text size. Also, are you validating whether the view you use IS the TextView? – Rajath Apr 12 '11 at 09:58
  • Thanks, actually that works, the text is getting trimmed in the top and the bottom, but I guess I just need to figure out that; maybe is related to the layout – lblasa Apr 12 '11 at 10:23