Created
November 26, 2013 12:49
-
-
Save dafi/7657781 to your computer and use it in GitHub Desktop.
Android TextView changing color when clicked. It uses the setSelect(); to change color. Show how to change text color and background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ternaryop.phototumblrshare.widget; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.widget.TextView; | |
public class ClickableTextView extends TextView implements OnTouchListener { | |
public ClickableTextView(Context context, AttributeSet attrs, | |
int defStyle) { | |
super(context, attrs, defStyle); | |
setup(); | |
} | |
public ClickableTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setup(); | |
} | |
public ClickableTextView(Context context, int checkableId) { | |
super(context); | |
setup(); | |
} | |
public ClickableTextView(Context context) { | |
super(context); | |
setup(); | |
} | |
private void setup() { | |
setOnTouchListener(this); | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (hasOnClickListeners()) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
setSelected(true); | |
break; | |
case MotionEvent.ACTION_UP: | |
case MotionEvent.ACTION_CANCEL: | |
setSelected(false); | |
break; | |
} | |
} | |
// allow target view to handle click | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.ternaryop.phototumblrshare.widget.ClickableTextView | |
android:id="@+id/title_textview" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignTop="@+id/thumbnail" | |
android:layout_toRightOf="@+id/thumbnail" | |
android:textColor="@drawable/text_link_selector" | |
android:background="@drawable/text_link_selector_bg" | |
android:textSize="15sp" | |
android:textStyle="bold" | |
android:typeface="sans" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:state_pressed="false" android:state_selected="false" android:color="#000000"/> | |
<item android:state_pressed="true" android:color="#0000ff"/> | |
<item android:state_pressed="false" android:state_selected="true" android:color="#0000ff"/> | |
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<!-- not selected has transparent color --> | |
<item android:state_pressed="false" android:state_selected="false"> | |
<color android:color="#00000000"/> | |
</item> | |
<item android:state_pressed="true"> | |
<color android:color="#ff0000"/> | |
</item> | |
<item android:state_pressed="false" android:state_selected="true"> | |
<color android:color="#ff0000"/> | |
</item> | |
</selector> |
👍
Have try it but the function hasOnClickListeners()
return always false and doesn't execute the inside code.
Do you know where might be the issue?
I had to extend the android.support.v7.widget.AppCompatTextView
instead android.widget.TextView
might be there the problem?
Thanks Bro👍
what is depedency
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hfh