Thanks, Jeff.
When user selects a TextView it 'slips' (which is indicated by
background color change) and the user is expected to select(tap)
another view then. Not every tap should lead to the first view losing
'focus'. That's the motivation behind setting background drawable
programmatically.
Back to the question.
--
Drawable bg = getResources().getDrawable
(android.R.drawable.list_selector_background);
bg.setState(new int[]{android.R.attr.state_pressed,
android.R.attr.state_focused});
tv.setBackgroundDrawable(bg);
--
This (and any of those attributes set alone) doesn't change anything.
Do I still get it wrong?

On May 6, 7:10 pm, Jeff Sharkey <jshar...@android.com> wrote:
> So the list_selector_drawable is a stateful drawable, where it's
> default state is black.  (This is why ListView unselected items are
> transparent/black.)  The default button drawable is also stateful, but
> it's default state is an unpressed button.
>
> You could force it into a focused state using Drawable.setState()
> before setting it to the background.
>
> However, keep in mind that you probably don't want to have the
> TextView always appear selected, as it might confuse users to see two
> selected areas on screen at once.
>
> You might instead try setting the TextView to be focusable, so the
> background changes automatically whenever the user moves focus to the
> TextView.
>
> j
>
>
>
> On Wed, May 6, 2009 at 5:57 AM, gsmd <gsm...@gmail.com> wrote:
>
> > Ok, here's a simple 'helloorange' activity:
> > --
> >   �...@override
> >    public void onCreate(Bundle savedInstanceState) {
> >        super.onCreate(savedInstanceState);
> >        TextView tv = new TextView(this);
> >        tv.setText("some text");
> >        tv.setBackgroundDrawable(getResources().getDrawable
> > (android.R.drawable.list_selector_background));
> >        setContentView(tv);
> >    }
> > --
> > When running this, the background doesn't change it's color. When I
> > change to android.R.drawable.btn_default, it works.
> > So, how do I apply orange background drawable to a TextView at
> > runtime?
>
> > TIA.
>
> --
> Jeff Sharkey
> jshar...@google.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to