0

I have a clickable linear layout that I've generated programmaticly, and I want it to turn green when it is pressed to indicate that it is clickable, like a button would. How would I go about doing this?

Garzahd
  • 5,739
  • 3
  • 18
  • 19

2 Answers2

1

you need to create a selector xml file. See link

How to change the color of button after click?

It's a little confusing at first but not too complicated

Community
  • 1
  • 1
testingtester
  • 528
  • 4
  • 11
1

create a selector.xml file as below...

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ff0000"/> 
    <item android:state_focused="true"
          android:color="#0000ff"/>
    <item android:color="#00ff00"/> </selector>

place this xml file in drawable folder. Then set this selector.xml as background of that linearlayout.

Ishu
  • 5,357
  • 4
  • 16
  • 17
  • When using the setBackgroundResource(R.drawable.selector) to apply, I get an error: 04-03 12:18:15.582: E/AndroidRuntime(13550): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a 'drawable' attribute or child tag defining a drawable – Garzahd Apr 03 '12 at 16:18