android_guy
android_guy

Reputation: 236

Want textview to change color with click just like on a button

I want to make the user feel when clicked on a text field just like when we click on a button and and after releasing the button it goes to orange color for a very short time a blink and again turns into it's first color. When I clicks on it does work well but no color blinking appears.

Layout file

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_below="@+id/button5"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="28dp"
        android:clickable="true"
        android:onClick="onClick"
        android:text="Click Me"
        android:textAppearance="?android:attr/textAppearanceLarge" />

mainActivity Code

textV.setOnClickListener(new OnClickListener() {

    @Override
            public void onClick(View view) {
        startActivity(new Intent("com.DRMS.help"));
            }
        }); 

Upvotes: 2

Views: 5001

Answers (6)

Samadhan Medge
Samadhan Medge

Reputation: 2049

property for button

android:background="@drawable/focus_color.xml"

//focus_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item 
        android:state_pressed="true"
        android:drawable="@drawable/gradient_focus" 
        /> <!--pressed  -->
    <item 
        android:state_focused="true"
        android:drawable="@drawable/gradient_titlebar" 
        /> <!-- focused -->    
    <item 
        android:drawable="@drawable/gradient_paint" 
        /> <!-- default -->
</selector>  

//gradient_focus.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#FFFFFF"
        android:startColor="#e8ebef"
        android:type="linear" />

    <stroke
        android:width="0.3dp"
        android:color="#000000" />

    <corners android:radius="0.5dp" />

</shape>  

//gradient_titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <gradient
        android:startColor="#E2EEFC"
        android:endColor="#BFCBD9"
        android:type="linear"
        android:angle="90"/>
    <corners android:radius="2dp" />
</shape>  

//gradient_paint.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <gradient
        android:startColor="#94D7F2"
        android:endColor="#65A2DB"
        android:type="linear"
        android:angle="90"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    <corners android:radius="2dp"/>
</shape>

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

You can use OnTouchListener or you can use a selector.

textV.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // change color
    }
    else if (event.getAction() == MotionEvent.ACTION_UP) {
            // set to normal color
    }

    return true;
}
});

Edit:

You can use a selector also. Borders and rounded rectangle. Customize the same.

bkg.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/pressed" />
    <item  android:state_focused="false" 
        android:drawable="@drawable/normal" />
</selector>

normal.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"> 
  <solid android:color="#0AECBF"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>  

pressed.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff33ffff" />
 <padding android:left="5dp"
             android:top="5dp"
             android:right="5dp"
             android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"
             android:bottomLeftRadius="7dp" 
             android:topLeftRadius="7dp"
             android:topRightRadius="7dp"/> 
</shape>

Now set the background

     android:background="@drawable/bkg"

Upvotes: 3

KBusc
KBusc

Reputation: 663

What you can try is setting the onTouchListener. Something like this

myButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){

        // Change your color here
        return true;
    }
    return false;
  }
});

the onTouchListener should fire before the onClickListener

Upvotes: 0

Bald bcs of IT
Bald bcs of IT

Reputation: 892

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#61ab69" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#009933"
                android:endColor="#00bf33"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />
        </shape>
    </item>
</selector>

save it as btnclick.xml in drawable folder and use it as

android:background="@drawable/btnclick

in that text view

it will works fine

Upvotes: 0

user2041902
user2041902

Reputation: 593

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true" android:drawable="#002233"/>
    <item android:state_pressed="true" android:drawable="#112233"/>
    <item android:drawable="#001122"></item>
</selector>

Create a xml file (say text_bkgrnd) in layout folder add the above code in it.

Now in main.xml set textView's background attribute to this the created xml file

Hope this helps. Change the hash value of states as required

Upvotes: 0

Opiatefuchs
Opiatefuchs

Reputation: 9870

Just set an selector as background drawable. Create a new Android xml file with "selector" into the drawable folder:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <color android:color="@android:color/black" />
</item>
<item>
    <color android:color="@android:color/white" />
</item>
   </selector>

and then, in Your xml layout, set this to Your textView:

    android:background="@drawable/your_selector"

This is only a poor example, You can create shapes.xml for different backgrounds etc.

Upvotes: 0

Related Questions