Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,742,696 Members 37,552 Now Online
XDA Developers Android and Mobile Development Forum

[Q] Set BackgroundColor but keep onTouch

Tip us?
 
Auroratic
Old
#1  
Auroratic's Avatar
Member - OP
Thanks Meter 0
Posts: 41
Join Date: Jun 2013
Location: Austria
Default [Q] Set BackgroundColor but keep onTouch

Hello,
I want to set the BackgroundColor of ListView items, but I want to keep the color which appears when I click the items.

How can I do that?
 
deanwray
Old
#2  
deanwray's Avatar
Senior Member
Thanks Meter 417
Posts: 1,107
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by Auroratic View Post
Hello,
I want to set the BackgroundColor of ListView items, but I want to keep the color which appears when I click the items.

How can I do that?
Rather than replace a "stateListDrawable" with a color or static drawable, replace it with another state list that has the "states" you require
 
Auroratic
Old
(Last edited by Auroratic; 22nd March 2014 at 01:22 AM.)
#3  
Auroratic's Avatar
Member - OP
Thanks Meter 0
Posts: 41
Join Date: Jun 2013
Location: Austria
Quote:
Originally Posted by deanwray View Post
Rather than replace a "stateListDrawable" with a color or static drawable, replace it with another state list that has the "states" you require
I've got it, but I have one problem: The colour on state_pressed should be the default system colour (this one: android.R.drawable.list_selector_background)
How can I do that?
 
deanwray
Old
#4  
deanwray's Avatar
Senior Member
Thanks Meter 417
Posts: 1,107
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by Auroratic View Post
I've got it, but I have one problem: The colour on state_pressed should be the default system colour (this one: android.R.drawable.list_selector_background)
How can I do that?
I'm not sure what your asking, if you have read about stateListDrawabes and have understood it, also maybe even looked at examples, then you should know ? Open that resource, or even clone it and edit it how you want.
 
Auroratic
Old
#5  
Auroratic's Avatar
Member - OP
Thanks Meter 0
Posts: 41
Join Date: Jun 2013
Location: Austria
Quote:
Originally Posted by deanwray View Post
I'm not sure what your asking, if you have read about stateListDrawabes and have understood it, also maybe even looked at examples, then you should know ? Open that resource, or even clone it and edit it how you want.
I have the following:
Code:
Select Code
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
   android.R.attr.state_pressed
}, new ColorDrawable(android.R.drawable.btn_default));

stateList.addState(new int[0], new ColorDrawable(Color.YELLOW));
textView.setBackgroundDrawable(stateList);
The background is yellow, but when I press on the textView, the background turns into transparent (it should be like this when i press:
)
 
deanwray
Old
#6  
deanwray's Avatar
Senior Member
Thanks Meter 417
Posts: 1,107
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by Auroratic View Post
I have the following:
Code:
Select Code
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {
   android.R.attr.state_pressed
}, new ColorDrawable(android.R.drawable.btn_default));

stateList.addState(new int[0], new ColorDrawable(Color.YELLOW));
textView.setBackgroundDrawable(stateList);
The background is yellow, but when I press on the textView, the background turns into transparent (it should be like this when i press:
)
well fyi you should look at the view type "states" as not sure you want android.R.attr.state_pressed ... neways you probably want state_focused and state_enabled for an editText view, but really google should provide you with simple absolute answers

hope that clears it up neways
 
Auroratic
Old
(Last edited by Auroratic; 22nd March 2014 at 02:26 PM.)
#7  
Auroratic's Avatar
Member - OP
Thanks Meter 0
Posts: 41
Join Date: Jun 2013
Location: Austria
Quote:
Originally Posted by deanwray View Post
well fyi you should look at the view type "states" as not sure you want android.R.attr.state_pressed ... neways you probably want state_focused and state_enabled for an editText view, but really google should provide you with simple absolute answers

hope that clears it up neways
I have that now:
Code:
Select Code
StateListDrawable stateList = new StateListDrawable();
        stateList.addState(new int[] {
            android.R.attr.state_pressed
        }, context.getResources().getDrawable(android.R.drawable.list_selector_background));
but when I press on the TextView, the background goes orange
 
deanwray
Old
#8  
deanwray's Avatar
Senior Member
Thanks Meter 417
Posts: 1,107
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by Auroratic View Post
I have that now:
Code:
Select Code
StateListDrawable stateList = new StateListDrawable();
        stateList.addState(new int[] {
            android.R.attr.state_pressed
        }, context.getResources().getDrawable(android.R.drawable.list_selector_background));
but when I press on the TextView, the background goes orange
now your setting a state with a state "list" so not sure that will ever work.. needs to be a simple drawable

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


XDA PORTAL POSTS

Slide Dial Replacement Dialer Lets You Make Calls from Anywhere

If you frequently make phone calls, your dialer (aka phone app)is one of the … more

Navigate with Your Samsung Gear 2 Using DMA Navi Watch

Whether you’re travelling to a new destination or exploring some previously … more

Xperia Z1 Stereo Speaker Mod, Cell Phone Unlocking Legal Again! – XDA Developer TV

Cell Phone Unlocking is legal again!! That and much … more

[OTA Captured] Verizon LG G Pad 8.3 Finally Gets Official KitKat Update

It’s been almost nine months since the LG G Pad … more